四:案例

2020-08-06  本文已影响0人  我家猫猫不睡觉
public class TestDemoContract {

    interface TestDemoView extends IView {

        void addSuccess();
    }


    interface TestDemoPresenter extends IPresenter<TestDemoView> {

        void getInfo(String aaa, String bbb, String ccc);

    }
}

*present类

public class TestDemoPresenter extends AbstractPresenter<TestDemoContract.TestDemoView> implements TestDemoContract.TestDemoPresenter {

 //private TestDemoContract.TestDemoView mView;

    public TestDemoPresenter(Context context) {
       super(context);
    }

     @Override
    public void getInfo(String aaa, String bbb, String ccc) {
       
           if (TextUtils.isEmpty(aaa)) {
                getView().showError("aaa不能为空"));
                return;
            mView.showLoading();
              getCompositeDisposable().add(NetworkManager.getInstance()
                .create(MoneyApi.class)
                .getInfo(aaa, bbb, ccc)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Response>() {
                    @Override
                    public void accept(Response response) throws Exception {
                         getView().hideLoading();
                        if (response.isSuccess()) {
                             getView().addSuccess();
                        } else {
                            ToastUtil.showToast(response.getMsg());
                        }
                    }
                }, new ResponseException() {
                    @Override
                    public void onError(ApiException e) {
                         getView().hideLoading();
                        ToastUtil.showToast(e.getShowMessage());
                    }
                }));
        }
    }
     @Override
    public void attach(TestDemoContract.TestDemoView view) {
        //this.mView = view;
        bindView(view);
    }

    @Override
    public void dettach() {
        //this.mView = null;
        destory();
    }
}

*Activity

public class TestDemoActivity extends BaseMvpActivity<TestDemoContract.TestDemoPresenter,
        TestDemoContract.TestDemoView>
        implements View.OnClickListener, TestDemoContract.TestDemoView {

 @Override
    protected int getLayout() {
        return R.layout.activity_test_demo;
    }

     @Override
    protected TestDemoContract.TestDemoPresenter initPreseter() {
        if (mPresenter == null) {
            mPresenter = new TestDemoPresenter(this);
        }
        return mPresenter;
    }

     @Override
    protected void initView() {
   
    }

      @Override
    protected void initListener() {
       
    }

     @Override
    protected void initData() {
       
    }

      @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.backImg:
                  finish();
                break;
            case R.id.btn_next_step:
                mPresenter.getInfo(aaa,bbb,ccc);
                break;
        }
    }

     @Override
    public void addSuccess() {
        //接口回调  做需要做的事情,需要参数的话 就在里面传递
    }

     @Override
    public void showLoading() {
        mLoading.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideLoading() {
        mLoading.setVisibility(View.GONE);
    }

    @Override
    public void showError(String message) {
        ToastUtil.showToast(message);
    }
}

*一:基类
https://www.jianshu.com/p/afa7294d5a56

*二:IView + IPresenter + 响应体泛型 + presenter 弱引用持有
https://www.jianshu.com/p/22e38f6aac40

*三:接口文档
https://www.jianshu.com/p/6401f0c4d5c0

*五:网络管理类
https://www.jianshu.com/p/f3a60dd408c5

*推荐

Android Retrofit2.0使用详解
https://blog.csdn.net/qq_35229022/article/details/93849660

RxJava入门
https://www.jianshu.com/p/a406b94f3188

上一篇下一篇

猜你喜欢

热点阅读