Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share data between two presenters in MVP architecture in Android?

Here's an example scenario:

I have an activity (view) and a presenter for that view. The presenter fetches a list of users from a network API and holds it in memory using a List object. The activity contains different types of fragments to display the content about the users based on User.type. The two fragments (UserType1Fragment and UserType2Fragment) have their own respective presenters too.

The activity's presenter decides what type (I or II) of fragment is shown next based. The fragments' presenters decide how the user object is displayed and handle a button click event called killUser(). This should update the List object in the activity's presenter.

This is where the problem lies:

How do the fragment presents have a reference to the data in activity presenter? The presenters shouldn't directly communicate with each other. Maybe I should abstract out the List into a repository/interactor? How would the List be shared among presenters?

like image 978
XenoChrist Avatar asked Mar 21 '17 13:03

XenoChrist


People also ask

What is presenter in MVP Android?

Example of MVP Architecture The role of the Presenter class is to keep the business logic of the application away from the activity. Below is the complete step-by-step implementation of this android application. Note that we are going to implement the project using both Java and Kotlin language.

How MVP pattern works?

MVP is a user interface architectural pattern engineered to facilitate automated unit testing and improve the separation of concerns in presentation logic: The model is an interface defining the data to be displayed or otherwise acted upon in the user interface.

Can I use Livedata with MVP?

Using livedata instead of MVPview in MVP makes the presenter loosely coupled with the view and helps in reusing presenter. We can create different presenter for different components and expose events using livedata, it also encourages reactive architecure.

What is MVP in kotlin?

by Antonio Leiva | Blog, Development, Kotlin | 174 comments. MVP (Model View Presenter) pattern is a derivative from the well known MVC (Model View Controller), and one of the most popular patterns to organize the presentation layer in Android Applications.


1 Answers

So I ended up implementing something like what @Jahnold recommended. (I'll post the diagram in the link provided for an idea stackoverflow.com/a/41966497/568898 )

Hannes Dorfmann (the guy who created/manages the famous Mosby MVP library : Github link ) also pointed me in this direction.

enter image description here

Implementation

I have a presenter for the main activity and multiple fragments that can be used in that activity. Each fragment has its own presenter. Then I use a repository (search for repository pattern) which is basically stores the models and the business logic. For my use case, I keep this repository as a singleton. The repository provides data in three forms, from an online api, an sqllite database, or a cache stored in memory (basically an arraylist of items). I also have some currentitem int indexes and stuff in this repository, that get updated based on the current state.

Hence the data, the state and the business logic are stored in this shared repository. The presenters and the views are pretty dumb. I don't have much business logic (application specific logic) in presenters. They simply have the logic associated with how the data has to be displayed (view specific logic) and preprocessing in logic them.

Example

Whenever the fragment and activity need to talk to each other (via presenters) when the user clicks a button in a child fragment, the fragment asks its presenter to handleClick, the presenters updates the repository's currentItemSelected data (or something else) and asks the fragment to fire an event (say onbuttonclick) to an interface listener which the activity implements. When the activity gets the event, it asks it's own presenter to handle it and in turn the activity presenter looks for an update in the repository to get the new currentItemSelected.

Extra Info (advanced version):

You can also follow Clean architecture which is sort of a more advanced version of MVP architecture. MVP just deals with the architecture of the views, where as Clean architecture also deals with business logic and data architecture, MVP is just a small part of clean arch which is used to handle the views. Using this, you can break down the mega repo in my case into even further use-cases (or interactors) that handle a specific business logic use-case, and the repository just provides data. So the logic flow is now view-->presenter-->interactor-->repo and back.

like image 146
XenoChrist Avatar answered Oct 19 '22 01:10

XenoChrist