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.

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 ViewModel and provides to the users.
  • The ViewModel gets user’s actions from the View or provides data to View.
  • The Model abstracts the data source. View and ViewModel uses 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.

We have used Github API for Json source and as you see Single<> return type in order to observe data with RxJava.

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.

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 with base url, adapter and converter factories. addCallAdapterFactory() function gets adapter factory for supporting service method return types, add instance of RxJava2CallAdapterFactory for RxJava support.
  • provideRepoService— Provides Retrofit interface class for making requests.

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.

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.

As you know, fragment is our View part. We have injected ViewModelFactory and started to observe LiveData objects.

In recyclerView adapter, we have only observed List<Repo> LiveData and bind them to the recyclerView.

You can find the repository here!

Golang & Android Dev.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store