Now that we all all the pieces of our big nice jigsaw we can end it with the last tiles, the implementations of the View (MyView), Presenter (MyPresenter), Model (MyModel) and Repository (MyRepository)
MyView
Uses Butterknife (@Bindview) and IcePick (@State) as explained before
Creates the Presenter object and attaches itself to it (so they can communicate)
Does common initialization for RecyclerView (boring!) and sets a refresh list listener (yawn!)
Reestablishes the Subscription so when the activity is re-created, if there is any result pending from a previous Single this gets used
Detaches itself from the presenter on onDestroy()
MyPresenter
It returns immediately data if it has them and the call was not forcing a refresh, otherwise it asks the model to provide fresh data
It implements the callback used by the Model to get the list of people and forward them to the view, removing the loading status
MyModel
It creates its instance of the repository (it could be made better to have it singleton but ‘oh well…’ and tells it to fetch the list of people needed by the Presenter, handling the cases for onCompleted(), onError() and onNext() (RxJava specifics)
MyRepository
It takes two Single meant to return a list of people and merges them together to combine their result in one unique stream of data and adds it to the cache present in the BaseRepository
This allows us to combine the two calls we make (yes, yes, they have hardcoded values and this is bad practice, but for sake of “shortness” let’s assume they are properly done) to receive them as one result, and the caching works for the cases when the user would turn the phone (or change configuration) meanwhile the operation is running, preventing the information from getting lost
Whoa! Guys we reached the end, I hope you all enjoyed these pages and got out of the quest with some bits and pieces more of knowledge or understanding about these wonderful topics.
Do not hesitate to give me feedback or remarks on the article and the app and to comment whether you liked this post or not
Leave a Comment