How to share data between the two leading in the MVP architecture in Android?

Here is an example script:

I have an activity (presentation) and a presenter for this presentation. The presenter selects a list of users from the network API and holds it in memory using the List object. The activity contains various types of fragments for displaying user content based on User.type. These two fragments (UserType1Fragment and UserType2Fragment) also have their own presenters.

The activity leader decides which type (I or II) of the fragment is shown below. Representing fragments determine the way the user object is displayed and the processing of a button click event called killUser (). This should update the List object in the activity presenter.

Here's the problem:

How does the submitted fragment have a link to the data in the activity presenter? Facilitators should not communicate directly with each other. Maybe I should abstract the list into the repository / interactor? How will the List be shared among the presenters?

+5
source share
2 answers

So, I ended up implementing something like what @Jahnold recommended. (I will post the diagram in the link provided for the idea of fooobar.com/questions/1265735 / ... )

Hannes Dorfmann (the guy who created / managed the famous MVB Mosby library: Github link ) also pointed me in that direction.

enter image description here

Implementation

I have a presenter for the main activity and several fragments that can be used in this activity. Each fragment has its own leader. Then I use the repository (search for the repository template), which basically stores the models and business logic. For my use case, I save this repository as a singleton. The repository provides data in three forms: from an online api, sqlite database, or cache stored in memory (mainly for arraylist elements). I also have some currentitem int indexes and stuff in this repository that are updated depending on the current state.

Therefore, data, state, and business logic are stored in this shared repository. Leading and views are pretty dumb. I don’t have much business logic (application-specific logic) among the presenters. They just have logic related to how data should be displayed (view specific logic) and preprocess them in the logic.

Example

Whenever a fragment and activity should talk to each other (through speakers), when a user presses a button in a child fragment, the fragment requests handleClick from its presenter, the presenters update the current storage data currentItemSelected (or something else) and requests the fragment to run events (for example, onbuttonclick) for an interface listener that implements the action. When an action receives an event, it asks it for its own host to handle it, and in turn, the activity host looks for an update in the repository to get a new currentItemSelected.

Additional information (extended version) :

You can also keep an eye out for clean architecture, which is a kind of more advanced version of the MVP architecture. MVP simply deals with view architecture, where since pure architecture also deals with business logic and data architecture, MVP is just a small part of the clean arch that is used to handle views. Using this, you can break the mega-repo in my case into even more used cases (or interactions) that handle a specific case of using business logic, and the repository simply provides data. Thus, the logical flow is now a view -> presenter -> interactor -> repo and vice versa.

+13
source

you can pass a list link to a fragment through newInstance () of the fragment. I think that facilitators should not communicate directly with each other.

0
source

Source: https://habr.com/ru/post/1265733/


All Articles