package com.tjzhxx.union.public_store.rx;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Dong on 2017/1/17.
 */

public class SchedulersCompat {

    public static final int NETWORK_REQUEST_DELAY = 16;

    private static ObservableTransformer ioToUiTransformer =
            new ObservableTransformer() {
                @Override
                public ObservableSource apply(Observable upstream) {
                    return upstream.delay(NETWORK_REQUEST_DELAY, TimeUnit.MILLISECONDS)
                            .subscribeOn(Schedulers.io())
                            .unsubscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread());
                }
            };
    private static ObservableTransformer ioTransformer =
            new ObservableTransformer() {
                @Override
                public ObservableSource apply(Observable upstream) {
                    return upstream.delay(NETWORK_REQUEST_DELAY, TimeUnit.MILLISECONDS)
                            .subscribeOn(Schedulers.io())
                            .unsubscribeOn(Schedulers.io());
                }
            };

    /**
     * Don't break the chain; use RxJava's compose() operator
     */
    public static <T> ObservableTransformer<T, T> applyIoToUiSchedulers() {
        return (ObservableTransformer<T, T>) ioToUiTransformer;
    }

    public static <T> ObservableTransformer<T, T> applyIoSchedulers() {
        return (ObservableTransformer<T, T>) ioTransformer;
    }
}
