RetrofitManager+RXjava(MVP+抽基类)

//IView

public interface IView {
    Context cotext();

}

//BasePresenter

public abstract class BasePresenter {
    protected CompositeDisposable compositeDisposable = new CompositeDisposable();
    protected V view;

    public BasePresenter(V view) {
        this.view = view;
        initModel();
    }

    //初始化model
    protected abstract void initModel();

    //内存泄漏的解决
    void onDestroy() {
        view = null;
        compositeDisposable.clear();
    }

    //如果view.cotext()环境变量为null的话就提供全局环境变量
    protected Context context() {
        if (view != null && view.cotext() !=null) {
         return view.cotext();
        }
        return MyApp.getAppContext();
    }

}

//public abstract class BaseActivity

extends AppCompatActivity {


    protected  P  presenter;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(provideLayoutId());
        initView();
        initListener();
        //提供presenter
        presenter =  providePresenter();
        initData();
    }

    protected abstract P providePresenter();

    protected  void initData(){}

    protected  void initListener(){}

    protected  void initView(){}

    protected abstract int provideLayoutId();

    //解决内存泄漏

    @Override
    protected void onDestroy() {
        presenter.onDestroy();
        super.onDestroy();
    }
}


//MVP

//LoginModel

public class LoginModel {
    public Observable login(final String account, String password) {
        return RetrofitManager.getDefault().create(IHomeApi.class).login(account, password);
    }

}

//LoginPresenter

public class LoginPresenter extends BasePresenter {
    private LoginModel mLoginModel;

    public LoginPresenter(ILoginView loginView) {
        super(loginView);
    }

    @Override
    protected void initModel() {
        mLoginModel = new LoginModel();
    }

    public void login(String account, String password) {
        if (account == null) {
            if (view != null) {
                view.onLoginFaild("手机号不能为空");
            }
            return;
        }
        mLoginModel.login(account, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        compositeDisposable.add(d);
                    }

                    @Override
                    public void onNext(LoginBean loginBean) {
                        if (view != null) {
                            view.onLoginSuccess(loginBean);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (view != null) {
                            view.onLoginFaild(e.toString());
                        }
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}

//V层不变



你可能感兴趣的:(RetrofitManager+RXjava(MVP+抽基类))