The first two have retrofit and rxandroid applied to the project, this is intended to direct the introduction of DAGGER2 project, but considering the entire project structure, it is a structural arrangement bar, to see the hot online speculation MVP mode.
When it comes to the MVP, you have to mention MVC, and the ape friends who did the Java EE must know what MVC is. MVC is model, view, Controller, the MVP is model, view, Presenter. Model is used to provide data models, view is used to display data, and of course presenter is used to process business logic and display data to the view, which is the model and view bridge.
To get a clearer understanding of how MPV works, go directly to the code.
I divided the entire project into three module, such as
First, the Data processing module domain (including model), here the domain name is just I like this data processing related things are put here.
ServiceManager used to provide the ingress of data to the outside (other class code mentioned earlier blog post)
Package com.micky.retrofitrxandroiddragger2.domain.service;
Import Retrofit. Gsonconverterfactory;
Import Retrofit. Retrofit;
Import Retrofit. Rxjavacalladapterfactory;
/**
* @Project RetrofitRxAndroidDragger2
* @Packate Com.micky.retrofitrxandroiddragger2.domain.service
* @Description
* @Author Micky Liu
* @Email [Email protected]
* @Date 2015-12-22 14:43
* @Version 1.0
*/
public class ServiceManager {
private static final String ENDPOINT = "http://ip.taobao.com";
private Static Class Servicemanagerholder {
private static final ServiceManager INSTANCE = new ServiceManager ();
}
Private ServiceManager () {}
public static final ServiceManager getinstance () {
return servicemanagerholder.instance;
}
Private Apiservice mapiservice = null;
Public Apiservice Getapiservice () {
if (Mapiservice = = null) {
Retrofit Retrofit = new Retrofit.builder ()
. BASEURL (ENDPOINT)
. Addconverterfactory (Gsonconverterfactory.create ())
. Addcalladapterfactory (Rxjavacalladapterfactory.create ())
. build ();
Mapiservice = Retrofit.create (Apiservice.class);
return mapiservice;
}
return mapiservice;
}
}
Second, view and presenter modules
For ease of management I put the view and presenter related classes in the presentation module
View interface
/**
* @Project RetrofitRxAndroidDragger2
* @Packate com.micky.presentation
* @Description
* @Author Micky Liu
* @Email [Email protected]
* @Date 2015-12-22 13:55
* @Version 1.0
*/
Public interface MainView {
void ShowProgress ();
void Hideprogress ();
void Setiptext (String text);
}
Presenter Interface
Package com.micky.retrofitrxandroiddragger2.presenter;
/**
* @Project RetrofitRxAndroidDragger2
* @Packate com.micky.presentation
* @Description
* @Author Micky Liu
* @Email [Email protected]
* @Date 2015-12-22 13:46
* @Version 1.0
*/
Public interface Mainpresenter extends Basepresenter {
void Getipinfo (String IP);
}
Presenter Implementation Class
Package Com.micky.retrofitrxandroiddragger2.presenter.impl;
Import Android.text.TextUtils;
Import Android.util.Log;
Import Android.widget.Toast;
Import com.micky.retrofitrxandroiddragger2.BaseApplication;
Import COM.MICKY.RETROFITRXANDROIDDRAGGER2.R;
Import Com.micky.retrofitrxandroiddragger2.domain.service.ServiceManager;
Import Com.micky.retrofitrxandroiddragger2.domain.service.response.GetIpInfoResponse;
Import Com.micky.retrofitrxandroiddragger2.presenter.MainPresenter;
Import Com.micky.retrofitrxandroiddragger2.presenter.impl.BasePresenterImpl;
Import Com.micky.retrofitrxandroiddragger2.ui.view.MainView;
Import Rx. subscriber;
Import Rx.android.schedulers.AndroidSchedulers;
Import Rx.schedulers.Schedulers;
/**
* @Project RetrofitRxAndroidDragger2
* @Packate Com.micky.retrofitrxandroiddragger2.presenter
* @Description
* @Author Micky Liu
* @Email [Email protected]
* @Date 2015-12-22 14:33
* @Version 1.0
*/
public class Mainpresenterimpl extends Basepresenterimpl implements Mainpresenter {
Private static final String tag = "tag";
Private MainView Mmainview;
Public Mainpresenterimpl (MainView MainView) {
Mmainview = MainView;
}
@Override
public void Getipinfo (String IP) {
if (Textutils.isempty (IP)) {
Toast.maketext (Baseapplication.getcontext (), R.string.input_tip_ip, Toast.length_short). Show ();
Return
}
Mmainview.setiptext ("");
Mmainview.showprogress ();
Servicemanager.getinstance (). Getapiservice (). Getipinfo (IP)
. Subscribeon (Schedulers.io ())
. Observeon (Androidschedulers.mainthread ())
. Subscribe (New subscriber<getipinforesponse> () {
@Override
public void oncompleted () {
Mmainview.hideprogress ();
}
@Override
public void OnError (Throwable e) {
LOG.E (TAG, E.getmessage (), E);
Mmainview.hideprogress ();
Mmainview.setiptext (Baseapplication.getcontext (). getString (R.string.network_error));
}
@Override
public void OnNext (Getipinforesponse getipinforesponse) {
Mmainview.setiptext (GetIpInfoResponse.data.country + "" + GetIpInfoResponse.data.area);
}
});
}
}
Mainactivity
Package com.micky.retrofitrxandroiddragger2.ui.activity;
Import Android.os.Bundle;
Import Android.support.design.widget.FloatingActionButton;
Import android.support.v7.app.AppCompatActivity;
Import Android.support.v7.widget.Toolbar;
Import Android.view.View;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.EditText;
Import Android.widget.ProgressBar;
Import Android.widget.TextView;
Import COM.MICKY.RETROFITRXANDROIDDRAGGER2.R;
Import Com.micky.retrofitrxandroiddragger2.presenter.MainPresenter;
Import Com.micky.retrofitrxandroiddragger2.presenter.impl.MainPresenterImpl;
Import Com.micky.retrofitrxandroiddragger2.ui.view.MainView;
/**
* @Project RetrofitRxAndroidDragger2
* @Packate com.micky.presentation
* @Description
* @Author Micky Liu
* @Email [Email protected]
* @Date 2015-12-22 12:22
* @Version 1.0
*/
public class Mainactivity extends Appcompatactivity implements MainView {
Private EditText Metip;
Private TextView mtvcontent;
Private ProgressBar Mprogressbar;
Private Mainpresenter Mmainpresenter;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar);
Setsupportactionbar (toolbar);
Metip = (EditText) Findviewbyid (R.ID.ET_IP);
Mtvcontent = (TextView) Findviewbyid (r.id.tv_content);
Mprogressbar = (ProgressBar) Findviewbyid (R.id.progress_bar);
Mmainpresenter = new Mainpresenterimpl (this);
Floatingactionbutton fab = (Floatingactionbutton) Findviewbyid (R.id.fab);
Fab.setonclicklistener (New View.onclicklistener () {
@Override
public void OnClick (view view) {
Mmainpresenter.getipinfo (Metip.gettext (). toString ());
}
});
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.menu_main, menu);
return true;
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
int id = item.getitemid ();
if (id = = r.id.action_settings) {
return true;
}
return super.onoptionsitemselected (item);
}
@Override
public void ShowProgress () {
Mprogressbar.setvisibility (view.visible);
}
@Override
public void hideprogress () {
Mprogressbar.setvisibility (View.gone);
}
@Override
public void Setiptext (String text) {
Mtvcontent.settext (text);
}
}
OK, the code basically finished, read the above code everyone may say before in a class inside the function, now how many out of so many interfaces, implementation class AH. Don't worry, I just saw this MVP when I think about it, a few times written to write it all out of the brain, this kind of is too much to see.
Careful study is not difficult to find that the M, V, p three relationship is quite clear.
To get a clearer picture, see the sequence diagram below (Mainview is only designed for better presentation of calls and data flow)
See here, is not feel so simple! it.
Detailed code in the old place: https://github.com/mickyliu945/CommonProj
This article was reprinted from: http://blog.csdn.net/liuhongwei123888/article/details/50380368.
Android App architecture for Android MVP use