简体   繁体   中英

Hook behaviour in RxJava Android and Retrofit

I am trying to learn RxJava with Retrofit. So this may be a simple question.

I want to write a wrapper method on Retrofit api calls. This method will show and hide progress view and do some other things before starting the call and post completion of call.

This is my service method

   @GET("/books")
    Observable<List<Book>> getBooks();

Now, before actually making the call, I want to show the progress view.

public <T> void execute(Observable<T> observable, final RequestHandler<T> callback) {
    observable = observable
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread());

    progressDialog = new TransparentProgressDialog(context, progressViewColor);
    progressDialog.setCancelable(false);
    dismissProgressDialog();
    progressDialog.show();

    observable.subscribe(new Observer<T>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable t) {

            ///Handle error
        }

        @Override
        public void onNext(T t) {
            /// Call the callback
        }
    });
}

As can be seen in the code I am passing an interface ( RequestHandler ) which I call on error and onNext . I am able to get this working. But this is not different than having a normal callback implementation. As per my understanding observable should be chainable. But I am not sure how to get that implemented so that observable can be chained.

Can anybody help me in this?

Thanks

Well read something about MVP pattern. Create some presenter which will perform request and handle response. Inside this presenter create BehaviourSubject and depending on your need emit onNext value to it with the wanted Boolean value.

import retrofit.Callback;
import retrofit.RetrofitError;
import rx.Observable;
import rx.subjects.BehaviorSubject;
public class TestTest {

    public final BehaviorSubject<Boolean> progressSubject = BehaviorSubject.create(true);
    public final Observable<SomeResponse> responseObservable;

    public TestTest(ApiService apiService) {

        responseObservable = apiService.performRequest(new Callback<SomeResponse>() {
            @Override
            public void success(SomeResponse someResponse, retrofit.client.Response response) {
                progressSubject.onNext(false);
            }

            @Override
            public void failure(RetrofitError error) {
                progressSubject.onNext(false);
            }
        });
    }

    public Observable<Boolean> progressObservable() {
        return progressSubject;
    }
}

Then in fragment/activity subscribe to progressObservable like

    presenter.progressObservable()
            .compose(this.<Boolean>bindToLifecycle())
            .subscribe(RxView.visibility(progressBar));

    presenter.someResponseObservable()...

Notice that in the example above, progress will be visible from the beginning because "true" is initial value of the BehaviourSubject. Keep in mind that you have to subscribe to reponseObservable. Without it code above would lead to endless progress.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM