-
近期刚好在做新项目,简单介绍下自己的MVP
1.先介绍下View的结构
1.通常会有一个BaseActivity,代码如下:
/**
* @Description:
* @Author: kele
* @email: 984127585@qq.com
* @Date: 2020/6/19 0019
*/
public abstract class BaseActivity<T extends IBaseP> extends AppCompatActivity {
protected T mPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
mPresenter = createPresenter();
init();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (null != mPresenter) {
mPresenter.onVDestroy();
}
}
/**
* 初始化Presenter
*
* @return
*/
protected abstract T createPresenter();
/**
* 初始化
*/
protected abstract void init();
}
2.以登录界面为例:
/**
* Des: 登录界面
* Created by kele on 2020/7/17.
* E-mail:984127585@qq.com
*/
public class LoginActivity extends BaseActivity<LoginP> implements LoginControl.V {
@Override
protected LoginP createPresenter() {
return new LoginP(this);
}
@Override
protected void init() {
if (null != mPresenter) {
mPresenter.login("账号", "密码");
}
}
@Override
public void loginEd(boolean isSuc, UserInfoBean bean, String errMsg) {
//结果处理
}
}
-
上面介绍的基本的View的实现,下面开始介绍MVP的具体实现
2.MVP具体实现
1.抽出统一操作方法到最底层
IBaseM,代码如下:
/**
* @Description:
* @Author: kele
* @email: 984127585@qq.com
* @Date: 2020/6/19 0019
*/
public interface IBaseM {
/**
* 销毁M相关的内容
*/
void onVDestroy();
}
IBaseV,代码如下:
/**
* @Description:
* @Author: kele
* @email: 984127585@qq.com
* @Date: 2020/6/19 0019
*/
public interface IBaseV {
}
IBaseP,代码如下:
/**
* @Description:
* @Author: kele
* @email: 984127585@qq.com
* @Date: 2020/6/19 0019
*/
public interface IBaseP {
/**
* 销毁P相关的内容
*/
void onVDestroy();
}
2.M、V、P对应的Base
BaseM,代码如下:
/**
* @Description:
* @Author: kele
* @email: 984127585@qq.com
* @Date: 2020/6/19 0019
*/
public class BaseM<T extends IBaseP> implements IBaseM {
/**
* 请求器列表-用于取消请求
*/
protected List<Disposable> disposableList = new ArrayList<>();
protected T iBaseP;
@Override
public void onVDestroy() {
if (null != iBaseP) {
iBaseP = null;
}
//取消所有请求
if (disposableList.size() > 0) {
for (Disposable d : disposableList) {
if (!d.isDisposed()) {
d.dispose();
}
}
}
}
}
BaseV 即上面的BaseActivity或者BaseFragment等
BaseP,代码如下:
/**
* @Description:
* @Author: kele
* @email: 984127585@qq.com
* @Date: 2020/6/19 0019
*/
public class BaseP<T extends IBaseV,S extends IBaseM> implements IBaseP{
protected T iBaseV;
protected S iBaseM;
@Override
public void onVDestroy() {
if (null != iBaseV) {
iBaseV = null;
}
if (null != iBaseM) {
iBaseM.onVDestroy();
iBaseM = null;
}
}
}
3.具体到对应的页面或者View的实现,同样以登录为例
- LoginControl 登录控制类,代码如下:
/**
* Des:
* Created by kele on 2020/7/17.
* E-mail:984127585@qq.com
*/
public interface LoginControl {
interface M extends IBaseM {
void login(String userName, String pwd);
}
interface V extends IBaseV {
void loginEd(boolean isSuc, UserInfoBean bean, String errMsg);
}
interface P extends IBaseP {
void login(String un, String pwd);
void loginEd(boolean isSuc, UserInfoBean bean, String errMsg);
}
}
- LoginM,代码如下:
/**
* Des:
* Created by kele on 2020/7/17.
* E-mail:984127585@qq.com
*/
public class LoginM extends BaseM<LoginP> implements LoginControl.M {
private LoginHttpService loginS;
public LoginM(LoginP p) {
if (null == p) {
return;
}
iBaseP = p;
if (null == loginS) {
loginS = RetrofitManager.getInstance().createService(LoginHttpService.class);
}
}
@Override
public void login(String userName, String pwd) {
if (null == iBaseP) {
return;
}
if (null == loginS) {
return;
}
GetUserInfoRequestBean userInfoBean = RequestBeanFactory.getInstance().getUserInfoBean(userName, pwd);
Observable<BaseResponse<UserInfoBean>> userLogin = loginS.userLogin(userInfoBean);
Disposable userLoginDis = userLogin
.compose(ResponseTransformer.<UserInfoBean>handleResult())
.compose(SchedulerProvider.getInstance().<UserInfoBean>applySchedulers())
.subscribe(new Consumer<UserInfoBean>() {
@Override
public void accept(UserInfoBean bean) throws Exception {
if (null == iBaseP) {
return;
}
iBaseP.loginEd(true, bean, "");
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
if (null == iBaseP) {
return;
}
String errMsg = throwable.getMessage();
if (throwable instanceof ApiException) {
errMsg = ((ApiException) throwable).getDisplayMessage();
}
iBaseP.loginEd(false, null, errMsg);
}
});
disposableList.add(userLoginDis);
}
}
-
LoginV 即上面的LoginActivity
-
LoginP,代码如下:
/**
* Des:
* Created by kele on 2020/7/17.
* E-mail:984127585@qq.com
*/
public class LoginP extends BaseP<LoginControl.V, LoginM> implements LoginControl.P {
public LoginP(LoginControl.V v) {
if (null == v) {
return;
}
this.iBaseV = v;
this.iBaseM = new LoginM(this);
}
@Override
public void login(String un, String pwd) {
if (null == iBaseM) {
return;
}
iBaseM.login(un, pwd);
}
@Override
public void loginEd(boolean isSuc, UserInfoBean bean, String errMsg) {
if (null == iBaseV) {
return;
}
iBaseV.loginEd(isSuc, bean, errMsg);
}
@Override
public void onVDestroy() {
super.onVDestroy();
}
}
欢迎交流
网友评论