I used to work with MVP pattern until now. However, when Google released nice-to-use components like the
ViewModel along with the Android Jetpack, I have tried to work with MVVM pattern. In this article, we will see how can we use the MVVM pattern with Retrofit, RxJava, and Dagger 2.
What is MVVM?
Model-View-ViewModel architecture consists of 3 parts.
- The View gets user’s actions and sends to the
ViewModel, or listens live data stream from the
ViewModeland provides to the users.
- The ViewModel gets user’s actions from the
Viewor provides data to
- The Model abstracts the data source.
ViewModeluses that on data stream.
We implement Android Lifecycle, Retrofit, RxJava, ButterKnife and Dagger 2 libraries in addition to Support libraries.
We will create packages by features. It will make your code more modular and manageable.
Setting Up Retrofit Interface
We have used Github API for Json source and as you see
Single<> return type in order to observe data with
Setting Up Application Class, Base Activity, Base Fragment
We have to use
DaggerFragment classes for injecting objects with
We have used abstract
layoutRes() function in order to get resource layout id from Activity which extends
Setting Up Dagger 2 Component & Modules
As you can see, we have wrote only
ViewModelModule to the module parameter. We will write other required modules in which Activity or Fragment they needed.
If explain what we have did in that code snippet;
provideRetrofit— Provides Retrofit adjusted with
base url, adapter and converter factories.
addCallAdapterFactory()function gets adapter factory for supporting service method return types, add instance of
RxJava2CallAdapterFactoryfor RxJava support.
provideRepoService— Provides Retrofit interface class for making requests.
Creating Custom ViewModel Factory
ViewModelFactory is a factory that extends
ViewModelProvider.Factory in order to provide
ViewModel instances to consumer fragment classes. We have injected that class with the
Setting Up ViewModel
ViewModel, we will assign the data which loaded with Retrofit to the
MutableLiveData. So, how we can use MutableLiveData? We assign the data to MutableLiveData with
postValue methods, and observe this data in
LifeCycleOwner (Activity or Fragment). When we make any changes on the
MutableLiveData, this change is dynamically declared to the view. Also, It does not oblige us to check whether
View is alive in every transaction.
As you can see, we didn’t use any
component injecting code, because we have injected all required classes in the
MainFragmentBindingModule and also we have did same thing for activities in the
Setting Up Fragment
As you know, fragment is our
View part. We have injected
ViewModelFactory and started to observe
Creating RecyclerView Adapter
In recyclerView adapter, we have only observed
LiveData and bind them to the
- Offline first Android App with MVVM, Dagger2, RxJava, LiveData and Room
- Clean Architecture — Kotlin, Dagger 2, RxJava, MVVM and Unit Testing
- Advanced Android — Architecture with Dagger 2 and more (Udemy)
- New Android Injector with Dagger 2 — part 1
You can find the repository here!