XenoChrist
XenoChrist

Reputation: 571

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?

Upvotes: 12

Views: 7840

Answers (2)

XenoChrist
XenoChrist

Reputation: 571

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.

Upvotes: 27

jaspreet singh
jaspreet singh

Reputation: 1

you can pass list reference to fragment through newInstance() of fragment. I think the presenters shouldn't directly communicate with each other.

Upvotes: 0

Related Questions