經過委派模式包裝一個RxJavaCallAdapterFactory

經過委派模式包裝一個RxJavaCallAdapterFactory

標籤(空格分隔): RxJava Retrofit CallAdapter網絡


實現是厭倦了寫不少重複的代碼了。ide

最近項目使用RxJava(RxAndroid)和Retrofit搭配作網絡請求。有一段代碼常常重複,this

service.XXX()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(
        httpResp -> {
            ... 這裏是一些處理結果前的常規操做
            
            ... 這裏處理結果
            
            ... 這裏是一些處理結果後的常規操做
        },
        throwable -> {
            ... 這裏是一些處理錯誤前的常規操做
            
            ... 這裏處理錯誤
            
            ... 這裏是一些處理錯誤後的常規操做
        }
    );

其實上面的代碼中,重要的就只有server.XXX()和「處理結果」、「處理錯誤」兩個三行。其餘的都是一些常規性的代碼,在其餘地方也重複的寫着。由於以前沒有想到很好的組織方式,就一直放着沒有整理。可是,寫得多的時候就煩了,因而就寫了下面一個類。code

public class MyRxJavaCallAdapterFactory implements CallAdapter.Factory {
  private RxJavaCallAdapterFactory mFactory;
  private MyRxJavaCallAdapterFactory() {
    mFactory = RxJavaCallAdapterFactory.create();
  }

  public static MyRxJavaCallAdapterFactory create() {
    return new MyRxJavaCallAdapterFactory();
  }


  @Override
  public CallAdapter<?> get(Type returnType, Annotation[] annotations, Retrofit retrofit) {
    CallAdapter<?> a = mFactory.get(returnType, annotations, retrofit);
    if (a == null) {
      return null;
    } else {
      CallAdapter<Observable<?>> adapter = (CallAdapter<Observable<?>>) a;
      return new MyCallAdapter(adapter);
    }
  }

    private class MyCallAdapter implements CallAdapter<Observable<?>> {

        private CallAdapter<Observable<?>> mAdapter;

        MyCallAdapter(CallAdapter<Observable<?>> adapter) {
            this.mAdapter = adapter;
        }

        @Override
        public Type responseType() {
            return mAdapter.responseType();
        }

        @Override
        public <R> Observable<?> adapt(Call<R> call) {
            Observable<?> observable = mAdapter.adapt(call);

            return observable
                    .compose(o -> {
                        // 有點尷尬,不少狀況下,網絡請求是在onCreate的時候就建立了。
                        // 而load事件的監聽倒是在onResume。
                        EventBus.getInstance().send(new LoadingStartEvent());
                        return o;
                    })
                    .lift(subscriber -> {
                        Subscriber temp = new Subscriber() {
                            @Override
                            public void onCompleted() {
                                subscriber.onCompleted();
                            }

                            @Override
                            public void onError(Throwable e) {
                                subscriber.onError(e);
                                EventBus.getInstance().send(new LoadingStopEvent());
                            }

                            @Override
                            public void onNext(Object o) {
                                subscriber.onNext(o);
                                EventBus.getInstance().send(new LoadingStopEvent());
                            }
                        };
                        return temp;
                    })
                    // 下面兩句先不要開放,由於有些地方須要同步請求。
//                    .subscribeOn(Schedulers.io())
//                    .observeOn(AndroidSchedulers.mainThread())
                    ;
        }
    }


}

好睏,不想排版了。睡覺去。server

相關文章
相關標籤/搜索