MVP in Android with Dagger2


This is an introductory for MVP concept in Android development with Dependency injection using Dagger2 library,it will be on 2 parts this the first.

What you should know ?

Before diving into this tutorial you should have knowledge of OOP concepts and dependency injection if you are a beginner Android developer i don’t recommend this tutorial to you.

How is this going to work ?

First will be a simple tutorial for MVP in general and why you as a developer should use it then we will dive into Android code and walk step-by-step till we apply the concept of MVP in Android.

Let the Game Begin :D

As an Android developer you will always have a constant headache about the maintenance of your code, making it testable and later updates as you have thousands of devices and like a million Android versions so you will always have a bug on some device some where on the planet earth and as a developer you should always find away to fix these bugs without creating other bugs
 You always seek separation of concerns in code so when you fix some bug in your layout or in your core it shouldn’t affect other modules of the application so you start searching for different design patterns and architectural patterns to help you in your infinite seek of a bug free program (Good luck with that :P )
and one of the best ways to achieve separation of concerns in code is using MVP

What is MVP ?!

MVP stands for Model-View-Presenter it’s an architectural pattern that’s evolved from the MVC pattern,
in MVP the view simply receives actions from the user, the View notifies the presenter about that action then the Presenter fires the appropriate logic and updates the UI with the data from the Model and processed in the Presenter, or updates the Model with the new data, also any logical actions and business core only relies in the presenter; the view and model should have nothing with you core, so if you took the presenter in any other application it should does exactly the core of the application 
in the MVP the view has nothing to do with the model it doesn’t get data from it nor it updates the data in it both of the View and Model only accessed by the Presenter; all of these interactions are done via listeners (Contractor)

MVP

MVP in Android

To apply MVP in Android the developer should break down the code packages based on the screens are there in the application, each package will contain a class for the View, class for the presenter and an interface for the contractor that notifies the presenter and view with different actions and UI updates; the Presenter should never have any Android based code, it should be pure Java code for the simplicity of testing and for separation of concerns so whenever you want to upgrade your code to a newer Android library or do some enhancements you don’t have the headache of modifying your presenter and your testing classes, but having the presenter does all of the business logic without accessing the Android SDK is very complex for example if you want to get data from the SharedPreferences where you core relies on, thus there must be away to provide the Presenter with the data it needs from the SDK, here you should consider using Dependency Injection Which we will discuss in part 2.

Sample

Google has a great repo on Github for the Android Architecture which is base on the MVP, this repo has many branchs each branch represents a sample of the MVP, you can clone the repo to gain more knowledge about the MVP and you can for sure checkout the todo-mvp branch for a simple MVP Android sample

Notes : the previous style of MVP is called passive view where the view has no idea nor interaction with the Model and we love this style in Android as its more testable
there is another type called > Supervisor View where the View does basic data binding from the Model