sockeqwe / mosby

A Model-View-Presenter / Model-View-Intent library for modern Android apps
http://hannesdorfmann.com/mosby/
Apache License 2.0
5.49k stars 841 forks source link

The role of Adapters in MVP #222

Closed BayramInanc closed 7 years ago

BayramInanc commented 7 years ago

hi, I wanna ask you a question. How do you treat adapters in MVP pattern? For example, in this project https://github.com/msahakyan/nested-recycler-view there is a MovieAdapter, https://github.com/msahakyan/nested-recycler-view/blob/master/app/src/main/java/com/android/msahakyan/nestedrecycler/adapter/MovieAdapter.java this guy has an recyclierview as item in this adapter , (if you look at his project, he has nested recyclierview in his home screen.) Because he has such an item, he doing service call and other notify, loading data(fetching from service) etc. operation in this adapter.(No communicaation to related fragment/activity) As you can see, this adapter has lots of to do. If you would do that, how would you implement this in mvp pattern? Specifically, in this context, would you have presenter object in adapter, or have view object to do that calling,notifying and loading?

sockeqwe commented 7 years ago

Adapter is pure View layer. Map all interaction like a click on a button in a RecyclerView ViewHolder back to the Activity / Fragment and then let forward that to the Activity's / Fragment's presenter.

RecyclerView are a little bit trickier because the ViewHolders can be recycled during scrolling etc. By going the road back to the "parent" Activity / Fragment and the corresponding Presenter it is much easier and less error prone to update a ViewHolder (i.e. with animations by using DiffUtils ). Take a ViewHolder just as a way to display a data object but don't add a Presenter for each ViewHolder to coordinate the ViewHolder. Really, just ensure that the ViewHolder gets a data object containing all information the ViewHolder needs to display but don't make this data object be "controlled" by a ViewHolders Presenter. Otherwise, you end up with a mess because one ViewHolder gets update by his Presenter , maybe the ViewHolder has been recycled in the mean time, maybe a screen orientation change has occurred or maybe the "parent" Activity / Fragment Presenter has updated the adapter's whole dataset etc. Do yourself a favor and use only one Presenter "coordinating / controlling" the data ther RecyclerView should display by using "parents" Activity / Fragment Presenter.

Btw. I would really appreciate if you would ask such questions on stackoverflow because github's issue tracker is not really well indexed by Google and therefore this question / answer is super hard to find for others. I regularly answer such Mosby related questions on stackoverflow.

Please copy this question to stackoverflow, so that I can copy my answer to this question there too to make it easier for others to find this question / answer. I'm pretty sure you are not the first developer having this quersion nor the last one. Stackoverflow helps a lot to make that questions discoverable. Many thanks in advance!

BayramInanc commented 7 years ago

ı did it! Here is the link https://stackoverflow.com/questions/42908174/the-role-of-adapters-in-mvp-pattern

sockeqwe commented 7 years ago

Thanks!