今天又有时间学学东西,写写博客了,虽然已经快2点了,还是没有一丝丝困倦之意,之前自己对mvp有了大概1% 的不全面的认识,今天大概有了5%的认识了,算是小小的进步,对于第一次接触mvp的同学请移步第一步
...
这篇介绍与上一次的区别有如下几点,增加了Contract(契约接口),实现登录的逻辑,增加使用ButterKnife和 LeakCanary
...
契约接口
<pre>
public interface LoginContract {
interface View extends BaseHttpView {
/**
* 登录时,本地验证手机号码不对
*/
void onLocalPhoneError();
/\*\*
\* 登录时,本地验证密码不对
\*/
void onLocalPwdError();
}
interface Presenter extends BaseHttpPresenter{
/\*\*本地验证登录信息\*/
void localValidateLoginInfo(@NonNull String phone,@NonNull String pwd);
/\*\*请求登录接口\*/
void login(@NonNull String phone, @NonNull String pwd);
}
}
</pre>
<pre>
public class LoginPresenter implements LoginContract.Presenter {
private LoginContract.View view;
public LoginPresenter(@NonNull LoginContract.View view) {
this.view = view;
}
@Override
public void localValidateLoginInfo(String phone, String pwd) {
if(!StringUtil.isPhoneNum(phone)){
view.onLocalPhoneError();
return;
}
if(!StringUtil.isLengthOK(pwd, AppConst.loginPwdMinLength, AppConst.loginPwdMaxLength)){
view.onLocalPwdError();
return;
}
login(phone, pwd);
}
@Override
public void login(@NonNull String phone, @NonNull String pwd) {
OkHttpClient okHttpClient = OkHttpUtil.getInstance().getOkHttpClient(new LogInterceptor(), new HeadParams().addHeader("phoneNum",phone).addHeader("uuid", DeviceUtil.getSafeDeviceSoleId(App.getApp())));
Retrofit retrofit = new Retrofit.Builder().baseUrl(Url.doMainApi).client(okHttpClient).addConverterFactory(GsonConverterFactory.create())//添加 json 转换器
.addCallAdapterFactory(RxJavaCallAdapterFactory.create())//添加 RxJava 适配器
.build();
HttpMan httpMan = retrofit.create(HttpMan.class);
httpMan.login(phone,pwd).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new MyHttpSubscriber());
}
private final class MyHttpSubscriber extends HttpSubscriber<LoginBean> {
@Override
public void onStart() {
view.showLoadingDialog();
}
@Override
public void onFailure(int code, String message) {
Logger.e(code+" "+message);
view.dismissLoadingDialog();
}
@Override
public void onSuccess(LoginBean result) {
view.dismissLoadingDialog();
Logger.e("登录成功");
}
}
private final class LogInterceptor implements HttpLoggingInterceptor.Logger{
@Override
public void log(String message) {
Logger.e(message);
}
}
}
</pre>
<pre>
public class LoginActivity extends BaseActivity implements LoginContract.View {
private LoginPresenter loginPresenter;
@Override
public int getLayoutResID() {
return R.layout.activity_login;
}
@Override
public int getLeftTitleViewId() {
return R.id.iv_back;
}
@Override
public void onCreateData() {
super.onCreateData();
ButterKnife.bind(this);
loginPresenter = new LoginPresenter(this);
setText(R.id.tv_title, "登录");
findView(R.id.tv_login).setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (R.id.tv_login == v.getId()) {
loginPresenter.localValidateLoginInfo(etPhone.getText().toString(), etPwd.getText().toString());
}
}
@Override
public void onLocalPhoneError() {
showToast(AppConst.userManPhone);
}
@Override
public void onLocalPwdError() {
showToast(AppConst.userLoginPwd);
}
}
</pre>