Android MVP从入门到放弃(二)

上章写了一个简单的MVP,了解了MVP的 基本方式。但是存在很多问题,比如如果正在网络请求时、页面被关闭,p层还为释放,怎么办,时间长久会泄露崩溃。等问题。

下面扩展下,让MVP更友好的使用。MVP2.0版,将MVP 分离出来,写成一个通用的module。 这样后面 项目可以直接引用module 就能使用。

目录结构


image.png
  • basemodel
public interface IBaseModel {

}
  • baseview
public interface IBaseView {

    /**
     * 绑定 p和 v 之间的关系。
     *
     * @return
     */
    @NonNull
    IBasePresenter initPresenter();


    /**
     * 显示弹窗
     * @param msg
     */
    void showDialog(String msg);

    /**
     * 取消弹窗
     */
    void hideDialog();

    /**
     * 显示吐司
     * @param msg
     */
    void showToast(String msg);


}
  • basepresenter
public abstract class IBasePresenter {

    protected M model;
    protected V view;

    /**
     * 获取model
     * @return
     */
    public abstract M getModel();

    /**
     * 绑定v 、m
     * @param v
     */
    public void attchMView(@NonNull V v) {
        this.view = v;
        this.model = getModel();
    }

    /**
     * 解绑
     */
    public void detachMView() {
        model = null;
        view = null;
    }

}
  • IBaseMvpActivity
public abstract class IBaseMvpActivity

extends AppCompatActivity implements IBaseView { protected P mpresenter; protected ProgressDialog progressDialog; public static final String TAG="duanc"; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mpresenter = (P) initPresenter(); if (mpresenter != null) { mpresenter.attchMView(this); } } @Override protected void onDestroy() { super.onDestroy(); mpresenter.detachMView(); } @Override public void showDialog(String msg) { startLoading(msg); Log.i(TAG, "showDialog: "+msg); } public void startLoading(String msg){ progressDialog = ProgressDialog.show(this, null, msg); progressDialog.setCancelable(true); progressDialog.setCanceledOnTouchOutside(true); } @Override public void hideDialog() { if (progressDialog != null) { progressDialog.dismiss(); } } @Override public void showToast(String msg) { Toast.makeText(this, "2.0--->"+msg, Toast.LENGTH_SHORT).show(); } }

上面是 所有基类。我们只要继承。

  • 这里我们加入一个契约类。PicContract、 用来统一管理接口逻辑。
public interface PicContract {

    interface PicView extends IBaseView {

        void onSuccess(String msg);

        void onError(String msg);
    }

    interface PicModel extends IBaseModel {
       boolean getLogin(String name,String pwd);
    }

    abstract class PicPresenter extends IBasePresenter {
        abstract void Onclicklogin(String name, String pwd);

    }
}
  • PicModel
public class PicModel implements PicContract.PicModel {

    public static PicModel newInstance() {
        return PicHodler.sInstance;
    }

    public static class PicHodler{
        private static final PicModel sInstance = new PicModel();
    }


    @Override
    public boolean getLogin(String name, String pwd) {


        if (name.equals("123456")) {
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return true;
        } else {
            return false;
        }
    }
}
  • picpresenter
public class PicPresenter extends PicContract.PicPresenter {

    public static PicPresenter newInstance() {

        return PicPresenHolder.sInstance;
    }

    public static class PicPresenHolder{
        public static final PicPresenter sInstance = new PicPresenter();
    }

    @Override
    public void Onclicklogin(String name, String pwd) {

        if (model != null | view != null) {

            assert model != null;
            if (model.getLogin(name, pwd)) {
                view.onSuccess(name);
            }else {
                view.onError(pwd);
            }
        }
    }

    @Override
    public PicContract.PicModel getModel() {
        return PicModel.newInstance();
    }
}
  • Main2Activity
/**
 * 2.0版本
 */
public class Main2Activity extends IBaseMvpActivity implements PicContract.PicView{

    @BindView(R.id.et_name)
    EditText etName;
    @BindView(R.id.et_pwd)
    EditText etPwd;
    @BindView(R.id.btn)
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ButterKnife.bind(this);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDialog("login...");
                mpresenter.Onclicklogin(etName.getText().toString(), etPwd.getText().toString());
            }
        });
    }

    @NonNull
    @Override
    public IBasePresenter initPresenter() {
        return PicPresenter.newInstance();
    }


    @Override
    public void onSuccess(String msg) {
        hideDialog();
        showToast("success");
    }

    @Override
    public void onError(String msg) {
        hideDialog();
        showToast("error");
    }

    @Override
    public void showDialog(String msg) {
        super.showDialog(msg);

    }
}

这样2.0版本完成, 下面我们会继续完善这个MVP基类。 加入retrofit 和 rxjava。

你可能感兴趣的:(Android MVP从入门到放弃(二))