Retrofit review : The sequel
Or how to use MVVM to get some cats so your code is not all over the place
This is the second mem- ehhh part in the “👏👏 Retrofit review 👏👏” series.
You can find part 1 here:
Previously on Meme Review
After copy pasting everything like any self respecting Stack Overflow user, we reached a point where everything works.. but looks kind of.. meh.
Everything is thrown in the activity 😱!
This is the part where I should explain why this is a horrible way to write but there’s enough internet warriors out there defending good coding practices.
Let’s fix that by introducing a ViewModel.
The point of a ViewModel is that he can be paired with an activity and you can throw everything that’s not UI related to him. Tell your activity to observe whatever is interesting that the ViewModel has in it and then do your thing.
Make sure to update the dependencies and give the project a sync afterwards.
Let’s just copy pasta the main bits from MainActivity to a ViewModel since we know the code works already as it is.
The main difference is the MutableLiveData variables and the postValue method associated with them.
Only thing missing is changing the MainActivity to work with MainViewModel.
Give it a run and tap the button. There should still be a toast popping up telling you what happened (good or bad).
The MainViewModel guy is responsible for the grunt work while the MainActivity is responsible for watching, which come to think of it, is a strange metaphor for most software jobs out there.
Stay tuned for the next part where we will finally(!) do something with this list and show these pussycats on the screen with a classic RecycleView and Glide (for image loading).