美文网首页
MVPArms使用

MVPArms使用

作者: 晓晓桑 | 来源:发表于2019-06-27 15:29 被阅读0次
     * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
     * <a href="https://github.com/JessYanCoding">Follow me</a>
     * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
     * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
     * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
    

    Api的baseurl

     String APP_DOMAIN = "http://api.channel.petope.com";
    

    CommonService设置service

    public interface CommonService {
        //登录
        String HEADER_API_VERSION = "ua: 1.1;2;API1;Android";
        @Headers({HEADER_API_VERSION})
        @FormUrlEncoded
        @POST("code/index")
        Observable<LoginBean> login(@Field("type") String type, @Field("phone") String phone);
    }
    

    LoginContracts设置接口

    public interface LoginContract {
        //对于经常使用的关于UI的方法可以定义到IView中,如显示隐藏进度条,和显示文字消息
        interface View extends IView {
            void LoginSuccess();
            void LoginFail();
        }
    
        //Model层定义接口,外部只需关心Model返回的数据,无需关心内部细节,即是否使用缓存
        interface Model extends IModel {
    //            Observable<BaseResponseBean> getLogin();
            io.reactivex.Observable<LoginBean> getLogin(String type, String phone);
    
        }
    }
    

    LoginModel

    @ActivityScope
    public class LoginModel extends BaseModel implements  LoginContract.Model {
        @Inject
        Gson mGson;
        @Inject
        Application mApplication;
    
        @Inject
        public LoginModel(IRepositoryManager repositoryManager) {
            super(repositoryManager);
    
    
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            this.mGson = null;
            this.mApplication = null;
        }
    
        @Override
        public Observable<LoginBean> getLogin(String type, String phone) {
            return mRepositoryManager.obtainRetrofitService(CommonService.class).login(type,phone);
    
        }
    
    }
    
    @ActivityScope
    public class LoginPresenter extends BasePresenter<LoginContract.Model, LoginContract.View> {
        @Inject
        RxErrorHandler mErrorHandler;
        @Inject
        Application mApplication;
        @Inject
        ImageLoader mImageLoader;
        @Inject
        AppManager mAppManager;
    
        @Inject
        public LoginPresenter(LoginContract.Model model, LoginContract.View rootView) {
            super(model, rootView);
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            this.mErrorHandler = null;
            this.mAppManager = null;
            this.mImageLoader = null;
            this.mApplication = null;
        }
    
    
        public void getLogin(String type,String phone) {
    
            mModel.getLogin(type,phone)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeOn(AndroidSchedulers.mainThread())
    //                .retryWhen(new RetryWithDelay(3, 2))//遇到错误时重试,第一个参数为重试几次,第二个参数为重试的间隔
                    .compose(RxLifecycleUtils.bindToLifecycle(mRootView))//使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                    .subscribe(new ErrorHandleSubscriber<LoginBean>(mErrorHandler) {
                        @Override
                        public void onNext(LoginBean loginBean) {
                            Log.i("aseResponseBean",loginBean.getMsg());
                            if (loginBean.isSuccess()){
                                mRootView.LoginSuccess();
                            }else {
                                mRootView.LoginFail();
                            }
    
                        }
                        @Override
                        public void onError(Throwable t) {
                            super.onError(t);
    
                            Log.i("onError", "=====" + t.getMessage());
                        }
    
                    });
        }
    }
    
    

    LoginActivity写视图

    public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {
    
    }
    

    相关文章

      网友评论

          本文标题:MVPArms使用

          本文链接:https://www.haomeiwen.com/subject/uajrcctx.html