catalinghita8 / android-mvvm-rxjava2-dagger2

This repository contains a detailed sample application that uses MVVM as its presentation layer pattern. Essential dependencies are Dagger2 with Dagger-android, RxJava2 with RxAndroid, Room, Retrofit and Espresso.
65 stars 17 forks source link
android-development dagger-android dagger2 dependency-injection kotlin mvvm mvvm-pattern room-persistence-library rxjava2 rxjava2-dagger2-retrofit2 viewmodel

Android-MVVM-RxJava2-Dagger2

! Sample has been migrated to Kotlin.

This repository contains a detailed sample application that uses MVVM as its presentation layer pattern. The app aims to be extremely flexible to creating variants for automated and manual testing. Also, the project implements and follows the guidelines presented in Google Sample MVVM+RXJAVA-android.

Essential dependencies are Dagger2 with Dagger-android, RxJava2 with RxAndroid, Room, Retrofit and Espresso. Other noteworthy dependencies would be Mockito, Chrome CustomTabs and Guava.

App Demo

This app displays latest earthquakes from all around the world. A number of quakes is fetched realtime upon request. If offline, the app displays the most recent loaded quakes.

content content content

Presentation Layer

MVVM pattern is integrated to facilitate testing and to allow separating the user interface logic from business logic.

As Views were passive in MVP, here the View layer is much more flexibile as an indefinite number of Views can bind to a ViewModel. Also, MVVM enforces a clear separation between Views and their master - ViewModel, as the latter holds no reference to Views. The model layer is completely isolated and centralized through the repository pattern.

Presentation

Model Layer

The model layer is structured on repository pattern so that the ViewModel has no clue on the origins of the data.

The repository handles data interactions and transactions from two main data sources - local and remote:

There are two main use-cases, online and offline. In both use cases, QuakesLocalDataSource has priority. In the online use-case if the local data is stale, new data is fetched from the NewsRemoteDataSource and the repository data is refreshed. In case of no internet connection, QuakesLocalDataSource is always queried.

Decoupling is also inforced within the Model layer (entirely consisted by QuakesRepository). Therefore, lower level components (which are the data sources: QuakesRemoteDataSource and QuakesLocalDatasource) are decoupled through QuakesDataSource interface. Also, through their dependence on the same interface, these data sources are interchangeable.

In this manner, the project respects the DIP (Dependency Inversion Principle) as both low and high level modules depend on abstractions.

Reactive approach

It is extremely important to note that this project is not essentially a reactive app as it is not capitalizing the enormous potential of a fully reactive approach. Nevertheless, the app was intended to have a flexible and efficient testing capability, rather than a fully reactive build.

Even in this case, we are able to notice RxJava's benefits when data is being retrieved from the repository through different sources and then is channeled through the ViewModel and finally consumed in Views.

Unit tests are conducted with the help of Mockito and Instrumentation tests with the help of Espresso.

Strong points

Final notes: