MVP-1: Provide Views with data which is ready to be displayed.


Description

The view layer of Android apps tends to become bloated with responsibilities, and hence becomes harder to maintain. In order to alleviate such problem, Activities and Fragments can be provided with preprocessed data ready to be displayed. This can be achieved by delegating data processing tasks to one or more dedicated components. In such manner, Activities and Fragments are relieved from the task of transforming and filtering domain-specific data, potentially improving the testability and usability of the app.

Example

We created a simple MVP login Application. Let us take a look at the ViewUsersActivity which implements the ViewUsersView.

The only responsibility of the view is too show the data. Which is set in the view function. Whenever the ViewUsersActivity is start up, it asks the ViewUserPresenter to show the users.

The ViewUserPresenter asks the ViewUserInteractor to get the users. If the ViewUserInteractor can find it, the users will be returned. Otherwise the onError() method from the ViewUserPresenter is invoked.

Our DataManager then asks the helper to return the users.

In our DbHelper we return the data in which the view it was specified. Which is a List.

The presenter then just provides the showUsers function from the ViewUsersView with the List

MVVM depicted

In the DbHelper the data is transformed so that the Presenter and the View can use it. Delegating this task of transforming data, improving the testability of Activities.

Check out the Github page to view the complete repository.