package com.pr0gramm.app.ui.base;

import android.support.annotation.NonNull;

import com.pr0gramm.app.util.BackgroundScheduler;
import com.trello.rxlifecycle.LifecycleTransformer;

import rx.Completable;
import rx.Observable;
import rx.Single;
import rx.android.schedulers.AndroidSchedulers;

/**
 */
class AsyncLifecycleTransformer<T> implements LifecycleTransformer<T> {
    private final LifecycleTransformer<T> transformer;

    AsyncLifecycleTransformer(LifecycleTransformer<T> transformer) {
        this.transformer = transformer;
    }

    @NonNull
    @Override
    public <U> Single.Transformer<U, U> forSingle() {
        return single -> single.toObservable().compose((Observable.Transformer) this).toSingle();
    }

    @NonNull
    @Override
    public Completable.Transformer forCompletable() {
        return completable -> completable.<T>toObservable().compose(this).toCompletable();
    }

    @Override
    public Observable<T> call(Observable<T> observable) {
        return observable
                .subscribeOn(BackgroundScheduler.instance())
                .unsubscribeOn(BackgroundScheduler.instance())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(transformer);
    }
}

@@ -655,73 +655,177 @@
 ngle
-.toObservable().compose((Observable.Transformer) this).to
+%0A                .subscribeOn(BackgroundScheduler.instance())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .compose(transformer.for
 Single()
 ;%0A  
@@ -816,24 +816,25 @@
 .forSingle()
+)
 ;%0A    %7D%0A%0A   
@@ -956,43 +956,232 @@
 able
-.%3CT%3EtoObservable().compose(this).to
+%0A                .subscribeOn(BackgroundScheduler.instance())%0A                .unsubscribeOn(BackgroundScheduler.instance())%0A                .observeOn(AndroidSchedulers.mainThread())%0A                .compose(transformer.for
 Comp
@@ -1189,16 +1189,17 @@
 etable()
+)
 ;%0A    %7D%0A

Remove redundant single -> observable -> single transformation