Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MVP structure which class responsible for keep list items and how to notify data change in this

I trying to refactor one of my activity class to implement mvp(using mvp mosby library) . I have a RecyclerView and in this view there is some items that some changes apply to them during the run time. for example I do some I/O operation and change one row.

I think it's better to keep my items in presenter class; what is the best practice for this? keep this in 1)presenter or 2)activity or 3)only keep view related item in adapter and all other item in presenter.

the activity now keep items directly and change item row in activity and then notify adapter. isn't better to move all this line in adapter and notify adapter in the adapter class? for example i want change icon of some row.where and which class is responsible for that? adapter? activity? now I want to implement it like this in adapter:

changeItemIcon(int position, int iconRes){
    mImages.get(position).setICon(iconRes);
    notifyItemChanged(position);
}

I invoke this method on activity and invoke activity method from presenter.

is it good? what is the best practice to do this?

UPDATE

also I find this question ( Best way to update data with a RecyclerView adapter ) that using adapter method for changing items. but what about modify? Need I keep reference to items in my activity?

like image 342
Siavash Abdoli Avatar asked Jun 06 '16 08:06

Siavash Abdoli


People also ask

Which is true about MVP design pattern?

The Model—View—Presenter(MVP) Pattern The reason why MVP is widely accepted is that it provides modularity, testability, and a more clean and maintainable codebase. It is composed of the following three components: Model: Layer for storing data.

What is MVP in architecture?

Model–view–presenter (MVP) is a derivation of the model–view–controller (MVC) architectural pattern which mostly used for building user interfaces. In MVP, the presenter assumes the functionality of the “middle-man”. In MVP, all presentation logic is pushed to the presenter.

What is presenter in Android?

A Presenter is used to generate View s and bind Objects to them on demand. It is closely related to the concept of an RecyclerView. Adapter, but is not position-based. The leanback framework implements the adapter concept using ObjectAdapter which refers to a Presenter (or PresenterSelector ) instance.


1 Answers

for example i want change icon of some row.where and which class is responsible for that? adapter? activity?

I know it sounds a little bit strange, but changing an element is always the responsibility of your "business logic", even just for "icons".

The workflow should be as follows (unidirectional data flow):

  1. View appeares, tells presenter to load a list of items
  2. Presenter loads items form "business logic" and registers himself as an observer / listener / callback (whatever you want to call it)
  3. Presenter receives result and tells the view to display the list of items (through RecyclerView and corresponding adapter).

so far is what you have implemented I guess, now it comes to the point where you want to change an item.

  1. User clicks on an item in your RecyclerView which then should trigger to change the icon of this item. Therefore View should call: presenter.changeItem()
  2. Presenter is just the man in the middle in this case and will invoke the "business logic layer" to tell that the item should be changed to new state (icon has changed).
  3. "Business logic layer" will change the models state (change the items icon) and then will notify its observer / listeners that the model has been changed.
  4. Since Presenter is still observing / listening to the business logic layer (see point 2.) the Presenter will be notified (see point 6.) with a new (updated) list of items containing the updated item which icon has been changed.
  5. Similar to point 3. Presenter will tell the view to display the new (updated) list of items (through RecyclerView and corresponding adapter).

Do you see the unidirectional data flow? That is very important. Immutability FTW.

like image 194
sockeqwe Avatar answered Sep 20 '22 05:09

sockeqwe