This is a boilerplate project Android for start MVP architecture
What is an MVP design pattern?
MVP design pattern is a set of guidelines that if followed, decouples the code for reusability and testability. It divides the application components based on its role, called separation of concern.
MVP divides the application into three basic components:
Model: It is responsible for handling the data part of the application.It is responsible for fetching the data from server, database and file system. DataManager only serves when asked
The Model is broken into few parts: ApiHelper, PreferenceHelper, DatabaseHelper, and FileHelper. These are all helpers to a DataManager, which in essence binds all Model parts
View: It is responsible for laying out the views with specific data on the screen. It delegates all the user interactions to its Presenter.The View never communicates with Model directly. In Android, Activity, Fragment, and a CustomView act as the View part of the application.
Each View has a Presenter in a one-to-one relationship. View communicates with its Presenter through an interface and vice versa.
Presenter: It is a bridge that connects a Model and a View. It also acts as an instructor to the View. Presenter is responsible for delegating View’s requirements to Model and instructing View with actions for specific events. Presenter communicates with the DataManager through an interface.
So,Let’s take a look at the project structuring:
The entire app is packaged into five parts:
- data: It contains all the data accessing and manipulating components.
- di: Dependency providing classes using Dagger2.
- ui: View classes along with their corresponding Presenters.
- service: Services for the application.
- utils: Utility classes.
How to implement a new screen following MVP*
Imagine you have to implement a sign in screen.
- Create new package under ui folder called signin
- Create an new Activity called ActivitySignIn. You could also use a Fragment.
- Define the view interface that your Activity is going to implement. Create a new interface called SignInMvpView that extends MvpView. Add the methods that you think will be necessary, e.g. showSignInSuccessful()
- Create a SignInPresenter class that extends BasePresenter
- Implement the methods in SignInPresenter that your Activity requires to perform the necessary actions, e.g. signIn(String email). Once the sign in action finishes you should call getMvpView().showSignInSuccessful().
- Create a SignInPresenterTest and write unit tests for signIn(email). Remember to mock the SignInMvpView and also the DataManager
- Make your ActivitySignIn implement SignInMvpView and implement the required methods like showSignInSuccessful()
- In your activity, inject a new instance of SignInPresenter and call presenter.attachView(this) from onCreate and presenter.detachView() from onDestroy(). Also, set up a click listener in your button that calls presenter.signIn(email)