In the MVP pattern, should adapters hold models or should the presenter hold models and have the adapter reference it?
Asked Answered
A

2

5

Currently I have it so that an adapter has a reference to all the models in it. But is it better to let the presenter just hold the models and the adapter can simply reference them?

So for example:

public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>{

private Presenter presenter;

public Adapter(Presenter presenter){
    this. presenter = presenter;
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    Model m = presenter.getModels().get(position);
    // bind model to view holder
}

@Override
public int getItemCount() {
    return presenter.getModels().size();
}


}

This way when a Presenter fetches more models, it just simply calls getAdapter().notfiyDataSetChanged(); after the fetch.

Abiding answered 23/6, 2016 at 20:44 Comment(0)
C
5

You can really go either way with it. Some would say treat the adapter as part of your view and make it as dumb as possible, but there's definitely a benefit to letting the adapter hold the data if you do it right.

For example, I use an abstract base adapter with generics that holds a list of data objects to drive the recyclerview. It provides all the standard CRUD operations for the list (add, update, delete, move, etc). These methods also handle notifying the adapter of the change, so my client code doesn't have to worry about it. It just hands an object to the adapter or tells it to delete/change one, and the adapter handles the rest.

The big benefit here is a huge reduction in the amount of repeated boilerplate code for CRUD operations and dataset change notifications across the various actors interacting with recyclerviews. If you have more than a screen or two with recyclerviews, this savings adds up quick to make it more beneficial than blindly adhering to a mantra.

Cooperstein answered 23/6, 2016 at 20:51 Comment(1)
Ah I see. Do you have any examples of the abstracted adapter. And if I didn't go the abstracted adapter way, would you say I should let the presenter hold the models?Abiding
F
4

Normally Adapter considered to be an implementation detail of View.

Presenter should not know View implementation details.

The job of adapter is to hold an array of items and to publish it to views. Adapter should not know about Presenter, models, other views, etc.

Data flow for Adapter, as I understand it:

Model -> Presenter -> View -> Adapter-> ItemView

Control flow is opposite, preferably skipping adapter.

Feel free to ask questions in the project's issues.

Funky answered 25/6, 2016 at 18:23 Comment(4)
if an Adapter and Presenter had to communicate what should I do? For example, if its a list of photos, and a user likes a photo. Do I have to implement a method in View just to act as a middle man? All the View would do is call getPresenter().likePhoto();Abiding
In this case I would use a lambda inside View to transmit the onClick event from ItemView to Presenter.Funky
hmm.. do you have an example of how the would look? I am not super familar with lamdas... but even with the lamda, what happens if the like fails (device is offline or server error)? we need to unlike the photo.Abiding
github.com/konmik/nucleus/blob/master/nucleus-example-real-life/…Funky

© 2022 - 2024 — McMap. All rights reserved.