The old project with the MVC model, recently completed all the work of the MVP model, although the more troublesome, the advantage is that the code logic more clear, concise, clearer process, for subsequent versions of the iteration maintenance is very convenient.
For some students who want to learn the MVP model, Baidu search out a lot of can not directly into the project directly to use things, so here just take out their projects have been used, you can directly use their projects. Of course, it is impossible to put all the project code here, so take the landing process out, this is more appropriate and more commonly used.
1, first look at the package structure:
Model: Put some bean classes, and network processing class Retrofitmanager,servicehelper (encapsulated network request Class), etc.
View: The logic to be implemented for the UI layer to be put
Presenter: Put some business logic related interfaces and implementation classes
2. Go to the Chase
First, take the login process as an example and simply draw the flowchart:
Then start dividing the logic for the corresponding three layers:
Presenter: As a login page, the business logic involved is: Remember password, log in, save tokens obtained after login
Public Interface iloginpresenter { void Rememberpassword (String account,string pwd); void Login (String phonenum,string pwd); void Savetoken (String token);}
Loginpresenterimpl: responsible for the specific login logic and view layer business calls, hold the View Layer object reference:iloginview
Public classLoginpresenterimplImplementsIloginpresenter {Private Static FinalString TAG = "Loginpresenterimpl"; PrivateString mmd5pwd; Iloginview Miloginview; PrivateContext Mcontext; PublicLoginpresenterimpl (Iloginview Iloginview, Context context) { This. Miloginview =Iloginview; This. Mcontext =context; } @Override Public voidRememberpassword (String account, string pwd) {sputils.put (Mcontext,"Remember_password",true); Sputils.put (Mcontext,"Phonenum", account); Sputils.put (Mcontext,"Password", PWD); } @Override Public voidLogin (String phonenum, string pwd) {if(Textutils.isempty (phonenum)) {Miloginview.loginresult (false, constant.phonenum_null);return; } if(!Utils.ismobileno (Phonenum)) {Miloginview.loginresult (false, constant.phonenum_false);return; } if(Textutils.isempty (pwd)) {Miloginview.loginresult (false, constant.pwd_null);return; } mmd5pwd=Utils.encrypt (PWD); LOGUTILS.D (TAG,"PWD:" + pwd + "------------mmd5pwd:" +mmd5pwd); //determine if the network is available if(!Utils.isnetavail ()) {Miloginview.loginresult (false, constant.internet_failed); LOGUTILS.D (TAG,"Network Unavailable"); return; } //initiate a network request to see if the phone number and password are correctservicehelper.callentity (Retrofitmanager.getinstance (). Createreq (Login.class). Getlogindata (Phonenum, mmd5pwd), Loginbean.class,NewOnresponselisner<loginbean>() {@Override Public voidonsuccess (Loginbean info) {intMuid =info.getdata (). Getuid (); String Token=info.getdata (). GetToken (); Savetoken (token); Miloginview.loginresult (true, String.valueof (Muid)); } @Override Public voidonError (String errormsg) {Miloginview.loginresult (false, errormsg); } }); }@Override Public voidSavetoken (String token) {if(!Textutils.isempty (token)) { //Storing string valuesSputils.put (Mcontext, "Token", token); } }
View: processing display of landing results (implementation of the corresponding method by implementing class Mainactivity)
Public Interface Iloginview { void loginresult (Boolean result, String msg);
Model: Data Bean class returned by the server
Public classLoginbean { Public BooleanSuccess; Public intCode; PublicString Errormsg_zh; PublicString errormsg_en; PublicDatabean Data; Public intServertime; PublicString Logid; Public Static classDatabean { Public intUID; PublicString Name; PublicString Phone; PublicString Email; PublicString Facepic; PublicString Token;}
Finally, take a look at the full landing page mainactivity (Iloginview Implementation class ) code:
Public classLoginactivityextendsBaseactivityImplementsIloginview {@BindView (r.id.et_phonenum) EditText metphonenum; @BindView (r.id.et_pwd) EditText metpwd; @BindView (r.id.iv_phonenumclear) ImageView mphonenumclear; @BindView (r.id.iv_pwdclear) ImageView mpwdclear; @BindView (r.id.cb_checkbox) checkbox Mcheckbox; @BindView (R.id.btn_login) Button mbtnlogin; @BindView (r.id.avi_loading) Avloadingindicatorview maviloading; PrivateString TAG = "Loginactivity"; PrivateString Mphonenum; PrivateString mpwd; PrivateIloginpresenter Miloginpresenter; @Override Public intGetlayoutresid () {returnR.layout.activity_login; } @Overrideprotected voidinit () {Super. Init (); Miloginpresenter=NewLoginpresenterimpl ( This, Loginactivity. This); BooleanIsremenber = (Boolean) Sputils.get ( This, "Remember_password",false); LOGUTILS.D (TAG,"Isremenber:" +isremenber); if(isremenber) {//set the account and password in the textString account = (string) sputils.get ( This, "Phonenum", "" "); String Password= (String) sputils.get ( This, "Password", "" "); Metphonenum.settext (account); Metpwd.settext (password); Mcheckbox.setchecked (true); } mbtnlogin.setonclicklistener (NewView.onclicklistener () {@Override Public voidOnClick (view view) {maviloading.setvisibility (view.visible); Mphonenum=Metphonenum.gettext (). toString (). Trim (); Mpwd=Metpwd.gettext (). toString (). Trim (); Miloginpresenter.login (Mphonenum, mpwd); if(mcheckbox.ischecked ()) {Miloginpresenter.rememberpassword (mphonenum, mpwd); } Else{sputils.remove (loginactivity). This, "Remember_password"); Sputils.remove (loginactivity. This, "Phonenum"); Sputils.remove (loginactivity. This, "Password"); } } }); } @Override Public voidLoginresult (Boolean result, String msg) {if(Result) {LOGUTILS.D (TAG,"UID:" +msg); Intent Intent=NewIntent ( This, Mainactivity.class); Intent.putextra ("UID", MSG); StartActivity (Intent); } Else{customtoast.show ( This, MSG + "Please try again later!" "); } maviloading.setvisibility (view.invisible); }
3, summary, MVP structure chart:
The view layer and the presenter layer hold each other's references, and the model is only used by the presenter layer.
PS: think to see or not too understand or seem to understand the classmate can himself write a write, should be finished can fully understand.
Android MVP Mode Brief introduction: Take a login process as an example