Android MVVM with Dagger 2, Retrofit, RxJava, Architecture Components
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 theViewModel
and provides to the users. - The ViewModel gets user’s actions from the
View
or provides data toView
. - The Model abstracts the data source.
View
andViewModel
uses that on data stream.
Project Configuration
We implement Android Lifecycle, Retrofit, RxJava, ButterKnife and Dagger 2 libraries in addition to Support libraries.
Project Structure
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 RxJava
.
Setting Up Application Class, Base Activity, Base Fragment
We have to use DaggerApplication
, DaggerAppCompatActivity
and DaggerFragment
classes for injecting objects with ContributesAndroidInjector
annotation.
We have used abstract layoutRes()
function in order to get resource layout id from Activity which extends BaseActivity
.
Setting Up Dagger 2 Component & Modules
As you can see, we have wrote only AndroidSupportInjectionModule
, ActivityBindingModule
and 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 withbase url
, adapter and converter factories.addCallAdapterFactory()
function gets adapter factory for supporting service method return types, add instance ofRxJava2CallAdapterFactory
for 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 ViewModelModule
.
Setting Up ViewModel
In 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 setValue
or 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 ActivityBindingModule
.
Setting Up Fragment
As you know, fragment is our View
part. We have injected ViewModelFactory
and started to observe LiveData
objects.
Creating RecyclerView Adapter
In recyclerView adapter, we have only observed List<Repo>
LiveData
and bind them to the recyclerView
.
Recommended Sources
- 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!