Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern methods and libraries
I found some unanswered questions about using MVP on StackOverflow which encouraged my strong interest to write a series of articles, providing a sample project (i.e a practical experiences of my own…)
Last year I get familiar with MVP, a tuxedo development, and looked at samples and tutorials. I spent a long time processing and connecting different parts of this unknown big puzzle. The most useful website which I can mention was caster.io which is always full of new Android video tutorials.
Explaining about MVP itself might look odd! cause there are many articles explaining how it works and how to break layers and so on, but there are few samples with enough comments which help newer in this methodology.
according to the SOLID article on Wikipedia, it stands for:
- S (SRP): Single responsibility principle
- O (OCP): Open/closed principle
- L (LSP): Liskov substitution principle
- I (ISP): Interface segregation principle
- D (DIP): Dependency inversion principle
MVP somehow try to follow all the five principles. I would try my best to pinpoint one by one in my sample project to make it more transparent.
according to this perfect MVP article, it stands for:
- The Model is the data that will be displayed in the view (user interface).
- The View is an interface that displays data (the model) and routes user commands (events) to the Presenter to act upon that data. The view usually has a reference to its Presenter.
- The Presenter is the “middle-man” (played by the controller in MVC) and has references to both, view and model.
Please note that the word “Model” is misleading!!!
It should rather be business logic that retrieves or manipulates a Model.
For instance: If you have a database storing
Userin a database table and your View wants to display a list of users, then the Presenter would have a reference to your database business logic (like a DAO) from where the Presenter will query a list of Users.
This application is a Marvel’s characters searching app which is a simple Android client for the Marvel.com. This application was created by me, as a part of the technical assessment by the smava GmbH technical team.
Marvel Android Application screenshot
The app must search character, view information and cache last searches.
This project implemented using MVP and contains modern Android development concepts and libraries which can really change your professional life!
I will try my best to explain everything in different parts of this continues article namely: Dagger, Retrofit, RxJava, and Tests.
The project also had Continues Integration(CI) using Circleci.com & Travis-ci.org and code coverage reports using Codecov.io and also using google new Firebase service which you can study by yourself and it is off topic for this article somehow.
Let’s take a look at the project’s structure.
I personally enjoy working on a clean code, so I really enjoy breaking a project into some meaningful parts that help me and the whole team with keeping more clear with tasks.
Project consist of two main and a java console sample modules:
The app module contains the Android View layer of MVP, while the other layers (Model & Presentation) are all placed in the core which is pure java and after compile it would be a jar library.
The result of java console sample module which works the same as Android Application with the core
to make modules look more convenient and nice you can edit settings.gradle file like this:
which gives you this clean look:
and also cause a relevant name for output APK file.
using a Gradle feature could come handy for a clean build.gradle file and avoiding version conflicts and redundant in your project’s modules.
First of all, place all your project’s dependencies inside a Gradle file like libraries.gradle:
then add it to your project main build.gradle file (take care of the last line):
and finally, use it like a charm in your app module build.gradle file (take care of dependencies part):
and your core module build.gradle file:
core module’s files structure
“You wouldn’t wire a lamp directly to your house”, — Novoda
Referencing to Dependency inversion principle of SOLID, “one should Depend upon Abstractions and Do not depend upon concretions” or reference to this fantastic article by Novoda “You wouldn’t wire a lamp directly to your house”!, all links between two modules (app & core) are implemented using interfaces and wired up with dagger.
app module’s files structure
Please clone the project repo from GitHub and get more familiar with it because in next part I will explain more about dagger and how it helps with wiring up modules and layer’s different objects.
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 2: How Dagger helps with the project_Implementing a sample Android Application using MVP, RxJava, Dagger 2, Retrofit2, Test, and all the brand new modern…_hackernoon.com