Rx-Mvp

项目地址:RuffianZhong/Rx-Mvp
简介:RxJava2+Retrofit2+RxLifecycle2 使用 MVP 模式构建项目
更多:作者   提 Bug   
标签:
retrofit2-rxjava2-rxlifecycle-mvp-

RxJava2+Retrofit2+RxLifecycle2 使用 MVP 模式构建项目

Api 接口

public interface UserApi {

@GET("user/login")
Observable<HttpResponse> login(@QueryMap Map<String, Object> request);

}

适用 Retrofit 网络请求 Observable(被订阅者)

public class HttpRxObservable {

        /**
         * 获取被订阅者
         * 备注:网络请求 Observable 构建
         * <h1>补充说明</h1>
         * 传入 LifecycleProvider 自动管理生命周期,避免内存泄漏
         * 备注:需要继承 RxActivity.../RxFragment...
         */
        public static Observable getObservable(Observable<HttpResponse> apiObservable, LifecycleProvider lifecycle) {
            Observable observable;
            observable = apiObservable
                    .map(new ServerResultFunction())
                    .compose(lifecycle.bindToLifecycle())//随生命周期自动管理.eg:onCreate(start)->onStop(end)
                    .onErrorResumeNext(new HttpResultFunction<>())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
            return observable;
        }

        /**
         * 获取被订阅者
         * 备注:网络请求 Observable 构建
         * <h1>补充说明</h1>
         * 传入 LifecycleProvider<ActivityEvent>手动管理生命周期,避免内存泄漏
         * 备注:需要继承 RxActivity,RxAppCompatActivity,RxFragmentActivity
         */
        public static Observable getObservable(Observable<HttpResponse> apiObservable, LifecycleProvider<ActivityEvent> lifecycle, ActivityEvent event) {
            Observable observable;
            observable = apiObservable
                    .map(new ServerResultFunction())
                    .compose(lifecycle.bindUntilEvent(event))//手动管理移除监听生命周期.eg:ActivityEvent.STOP
                    .onErrorResumeNext(new HttpResultFunction<>())
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread());
            return observable;
        }

}

适用 Retrofit 网络请求 Observer(订阅者)

public abstract class HttpRxObserver<T> implements Observer<T>, HttpRequestListener {

        private String mTag;//请求标识

        public HttpRxObserver() {
        }

        public HttpRxObserver(String tag) {
            this.mTag = tag;
        }

        @Override
        public void onError(Throwable e) {
            RxActionManagerImpl.getInstance().remove(mTag);
            if (e instanceof ApiException) {
                onError((ApiException) e);
            } else {
                onError(new ApiException(e, ExceptionEngine.UN_KNOWN_ERROR));
            }
        }

        @Override
        public void onComplete() {
        }

        @Override
        public void onNext(@NonNull T t) {
            if (!TextUtils.isEmpty(mTag)) {
                RxActionManagerImpl.getInstance().remove(mTag);
            }
            onSuccess(t);
        }

        @Override
        public void onSubscribe(@NonNull Disposable d) {
            if (!TextUtils.isEmpty(mTag)) {
                RxActionManagerImpl.getInstance().add(mTag, d);
            }
            onStart(d);
        }

        @Override
        public void cancel() {
            if (!TextUtils.isEmpty(mTag)) {
                RxActionManagerImpl.getInstance().cancel(mTag);
            }
        }


        protected abstract void onStart(Disposable d);

        protected abstract void onError(ApiException e);

        protected abstract void onSuccess(T response);

}

使用

   public void login(RxActivity activity, String phone, String psw) {

            //构建请求数据
            Map<String, Object> request = HttpRequest.getRequest();
            request.put("phone", phone);
            request.put("psw", psw);

            //设置唯一 TAG
            HttpRxObserver httpRxObserver = new HttpRxObserver("xxx_login") {
                @Override
                protected void onStart(Disposable d) {
                }

                @Override
                protected void onError(ApiException e) {
                    LogUtils.w("onError code:" + e.getCode() + " msg:" + e.getMsg());
                }

                @Override
                protected void onSuccess(Object response) {
                    LogUtils.w("onSuccess response:" + response.toString());
                }
            };

            /**
             * 获取请求 Observable
             * 1.RxActivity,RxFragment...所在页面继承 RxLifecycle 支持的组件
             * 2.ActivityEvent 指定监听函数解绑的生命周期(手动管理,未设置则自动管理)
             * 以上两点作用防止 RxJava 监听没解除导致内存泄漏,ActivityEvent 若未指定则按照 activity/fragment 的生命周期
             */

            HttpRxObservable.getObservable(ApiUtils.getUserApi().login(request), activity).subscribe(httpRxObserver);
            //HttpRxObservable.getObservable(ApiUtils.getUserApi().login(request), activity, ActivityEvent.PAUSE).subscribe(httpRxObserver);

            //取消请求
            /*if(!httpRxObserver.isDisposed()){
                httpRxObserver.cancel();
            }*/
}

CSDN 博客地址,文章说明:http://blog.csdn.net/u014702653/article/details/75268919

Android 开发经验分享
C 轮融资近 30 亿元理财推荐
Android 开发经验分享
C 轮融资近 30 亿元理财推荐