简体   繁体   English

RxJava Android中的挂钩行为和改造

[英]Hook behaviour in RxJava Android and Retrofit

I am trying to learn RxJava with Retrofit. 我正在尝试通过Retrofit学习RxJava。 So this may be a simple question. 因此,这可能是一个简单的问题。

I want to write a wrapper method on Retrofit api calls. 我想在Retrofit api调用上编写包装方法。 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 . 从代码中可以看出,我正在传递一个接口( RequestHandler ),该接口在error和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. 好吧,阅读有关MVP模式的知识。 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. 在此演示者内部,创建BehaviourSubject并根据您的需要向其发出带有所需布尔值的onNext值。

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 然后在片段/活动中订阅progressObservable如

    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. 请注意,在上面的示例中,从一开始就可以看到进度,因为“ true”是BehaviourSubject的初始值。 Keep in mind that you have to subscribe to reponseObservable. 请记住,您必须订阅reponseObservable。 Without it code above would lead to endless progress. 没有它,上面的代码将导致无休止的进步。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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