MVP Android (Part 2).
Much time has passed since it was written MVP Android (Part 1). Nevertheless, it is time to get to work again.
The test application is just one screen, with an editText (for entering the url), a button for requesting rss tape and a list (for RSS feed).
This example (initially) will be only realization of the MVP pattern. Development using lambda, RxAndroid, Retrofit and other features will discuss a little later.
The scheme, which reflects the situation about our future classes listed below:
*** Of course, in the circuit itself is not sufficient and methods are not fully valid but the circuit is shown as an example. ***
Model: It gets us a list of items from an RSS feed.
View: Allows to retrieve data from an editText, to display data and to display an error. It has a link to presenter.
Presenter: It has reference to the model and the view (through interfaces). Pressing the button call the model, and is waiting for data from it. After receiving the data — it displays it in the view. In case of failure — shows an error in the view.
First we create a layout for our Activity:
Our next step is to implement the interface to our Activity, which will look like this:
We implement this interface in our Activity, but the methods have not yet fill the code (do this in the future):
We proceed to a model interface creation and its implementation. Here everything is quite simple — only one method:
Then start adding links to elementsm that we need in the presenter and the view as follows:
It remains to fill our methods. To do this, we can follow the following rules:
1. View can only display data from the presenter. This means calls showError and showData will occur solely from presenter.
2. View must request data through the presenter. This is done very simply. In the method of view onGetButtonClick we call presenter.onGetButtonClicked ().
3. Model should not have a link to view or presenter
In future articles, I will add a more logic to our example, and demonstrate the use of commonly used features in the android development (Retrofit, Rx, ButterKnife, Acra, etc.)