Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern methods and libraries
Yet another MVP article — Part 3: Calling APIs using Retrofit_Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern…_hackernoon.com
Let’s think that you have studied part1, part2 and part3 of this article which means you know everything about the sample project’s modules and structure, also get more familiar with Dagger usage in wiring up MVP layers and know the use of Retrofit for connecting to network APIs.
now let’s continue…
During whole my career as a software developer I can say that RxJava is one of the best changes after Lambda expressions in Java 8. By following the recipes of mixing these two together you can develop software like a magician.
According to RxJava official website reactivex.io, it is “An API for asynchronous programming” which is always required in Android development.
Fortunately, this library documents on its website are perfect and you can find what ever you want to know with lots of graphs.
In this project, RxJava is mostly being used for retrieving data from the Model layer, and the Presentation layer calls View layer’s methods to hand the response.
Observables are being provided with Retrofit library as a result of calling API methods and being observed by Presentation layer of MVP.
The magic part about RxJava is its possibility of being observed and subscribed in different threads. Cause we always hand results to Android UI thread (i.e. MainThread) so we need a link to this thread inside core module from app module.
Schedulers in RxJava are threads in java. To hand response from API to View layer, you have to push it into Android UI MainThread.
To specify it we use observeOn method of RxJava.
RxJava has an extension for Android called RxAndroid which provide android threads for using with RxJava. To make this connection between core and app modules possible, app module implements SchedulerProvider interface from core within AppSchedulerProvider class which has both methods (mainThread/backgroundThread) implemented:
“Take care of the wrong abstraction”, — Novoda
, and finally by some Dagger injection, we can have AppSchedulerProvider as its parent (SchedulerProvider) inside our presenter class, which is another example of Liskov substitution in SOLID principle foot print in MVP.
RxJava has many useful operators but I think MAP is the most used operator.
Take a look at this part of code inside SearchPresenterImpl which do the character search:
The response from API is not always what we want to hand to View layer, and even may some Database caching is required, therefore, using the MAP operator we can do some tricks before UI knows.
There are four MAP operators applied to API response inside SearchPresenterImpl:
Using RxJava, all of these manipulations over API responses are possible in one line of code!
… and remember that all mapping things happen inside background thread, cause we specified it using subscribeOn inside SearchInteractorImpl class, and this is how it would never interrupt the MainThread to cause any lag or even ANRs.
RxJava is also being used in Adapters for notifying items in list click, which I think is easy to understand and you can take a look.
Please clone the project repo from GitHub and get more familiar with it because in next part I will explain more about writing test using all dagger things we have done.
I look forward to your comments and helping for more improvements with this article.
Share this article if you think it is useful, and follow me for more articles Mohsen Mirhoseini.
To be continued…
Yet another MVP article — Part 5: Writing Test using a mixture of Dagger and Espresso_Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern…_hackernoon.com