Implementing a sample Android Application using MVP , RxJava , Dagger 2 , Retrofit2 , Test , and all the brand new modern methods and libraries Previously on this article… _Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern…_hackernoon.com Yet another MVP article — Part 3: Calling APIs using Retrofit Let’s think that you have studied , and of this article which means you know everything about modules and structure, also get more familiar with usage in wiring up layers and know the use of for connecting to APIs. part1 part2 part3 the sample project’s Dagger MVP Retrofit network now let’s continue… RxJava is a revolution, truth or fiction! During whole my career as a software developer I can say that is one of the best changes after expressions in Java 8. By following the recipes of mixing these two together you can develop software like a . RxJava Lambda magician According to official website , it is which is always required in Android development. RxJava reactivex.io “An API for asynchronous programming” Fortunately, this library documents on its website are perfect and you can find what ever you want to know with lots of graphs. …, so let’s get back to our sample project: In this project, RxJava is mostly being used for retrieving data from the layer, and the layer calls layer’s methods to hand the response. Model Presentation View are being provided with Retrofit library as a result of calling API methods and being observed by layer of . Observables Presentation 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. so we need a link to this thread inside module from module. MainThread) core app How to specify Schedulers from Android to the core! in RxJava are threads in java. To hand response from API to layer, you have to push it into Android . Schedulers View UI MainThread To specify it we use method of . observeOn RxJava RxJava has an extension for Android called which provide android threads for using with RxJava. To make this connection between and modules possible, module implements from within class which has both methods (mainThread/backgroundThread) implemented: RxAndroid core app app SchedulerProvider interface core AppSchedulerProvider “Take care of the wrong abstraction”, — Novoda , and finally by some injection, we can have as its parent ( inside our presenter class, which is another example of in principle foot print in . Dagger AppSchedulerProvider SchedulerProvider) Liskov substitution SOLID MVP RxJava MAP operation comes for help… has many useful operators but I think is the most used operator. RxJava MAP Take a look at this part of code inside which do the character search: SearchPresenterImpl The response from API is not always what we want to hand to layer, and even may some caching is required, therefore, using the operator we can do some tricks before UI knows. View Database MAP There are four MAP operators applied to API response inside SearchPresenterImpl : The 1st map, check if the response code from API is Okay and if not an exception will throw later. Make sure that you use method, otherwise you have to handle the exception right there using try/catch statement!! ‘Exceptions.propagate’ The 2nd map, check if is there any result for the character being searched and if not an exception will throw later… The 3rd map, really maps response model into the database model using Mapper class. and finally, the last map, insert response into the database for a further cache. Using , all of these manipulations over API responses are possible in one line of code! RxJava … and remember that all mapping things happen inside background thread, cause we specified it using inside class, and this is how it would never interrupt the to cause any lag or even . subscribeOn SearchInteractorImpl MainThread ANRs Where else? RxJava is also being used in for notifying items in list click, which I think is easy to understand and you can take a look. Adapters That’s it for now… Please clone the and get more familiar with it because in next part I will explain more about writing test using all dagger things we have done. project repo from GitHub 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… _Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern…_hackernoon.com Yet another MVP article — Part 5: Writing Test using a mixture of Dagger and Espresso