Published in


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 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 , or listens live data stream from the and provides to the users.
  • The ViewModel gets user’s actions from the or provides data to .
  • The Model abstracts the data source. and 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 return type in order to observe data with .

Setting Up Application Class, Base Activity, Base Fragment

We have to use , and classes for injecting objects with annotation.

We have used abstract 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 , and 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;

  • — Provides Retrofit adjusted with , adapter and converter factories. function gets adapter factory for supporting service method return types, add instance of for RxJava support.
  • — Provides Retrofit interface class for making requests.

Creating Custom ViewModel Factory

ViewModelFactory is a factory that extends in order to provide instances to consumer fragment classes. We have injected that class with the .

Setting Up ViewModel

In , we will assign the data which loaded with Retrofit to the . So, how we can use MutableLiveData? We assign the data to MutableLiveData with or methods, and observe this data in (Activity or Fragment). When we make any changes on the , this change is dynamically declared to the view. Also, It does not oblige us to check whether is alive in every transaction.

As you can see, we didn’t use any injecting code, because we have injected all required classes in the and also we have did same thing for activities in the .

Setting Up Fragment

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

Creating RecyclerView Adapter

In recyclerView adapter, we have only observed and bind them to the .

Recommended Sources

You can find the repository here!



The (retired) Pub(lication) for Android & Tech, focused on Development

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