Android MVP is a Library that helps you to easily build Android applications that require some complexity and thus if you do not use any architecture pattern it can easily get unmaintainable and messy.
If you are not familiarized with MVP and its variants, I would strongly recommend that you read this article I wrote some time ago.
Also, I would strongly recommend using this plugin to make the boilerplate of creating all classes and communications less painful. Please read this article I wrote to get started with the plugin and learn how to use it.
Artifacts (Events + Coroutines)
You can include them just by adding the dependencies in your build.gradle file.
//Events oriented MVP Lib
implementation "com.github.marcherdiego.mvp:events:LATEST_VERSION"//Coroutines module to work extend Models
Check the latest versions of each library here.
Let’s create a new Activity called ExampleFeature in which when you tap on a button, you make a request to your server and show the response to the user.
Your activity should look like this. Notice that there is no need to register or unregister to the bus since all this life cycle states are handled by the BaseActivity.
Let’s take a look at the ExampleFeatureView class which is in charge of holding the widgets and forward any interaction to the Presenter(through the bus, obviously).
Now let’s see how the Presenter gets noticed of this event and how it tells the Model that some task should be done.
Now let’s take a look at the Model class which is in charge of any heavy work, business logic, etc. For simplicity, we are going to use an AsyncTask to show that there is some background task being executed, but of course, you can use any library such as Retrofit, Robospice, Volley, etc.
Now let’s add the handlers for the events SomeBackgroundTaskFinishedSuccessfullyEvent and SomeBackgroundTaskFailedEvent
That’s it, you have now a fully testable Presenter and Model, and you can forget the View since it’s not doing any logic, only holding widgets that we assume that are well tested :).
If you have questions or comments, please leave them below or write me at email@example.com and I will answer as fast as I can.