package com.sxyz.rfid.retrofit.config;


import android.app.Dialog;

import io.reactivex.ObservableTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Action;
import io.reactivex.schedulers.Schedulers;

public class Transformer {

    /**
     * @description 无参数
     * @author wuchunyuan
     * @time 2020/8/5 15:35
     */
    public static <T> ObservableTransformer<T, T> switchSchedulers() {
        return switchSchedulers(null);
    }

    /**
     * @description 带参数  显示loading对话框
     * @author wuchunyuan
     * @time 2020/8/5 15:35
     */
    public static <T> ObservableTransformer<T, T> switchSchedulers(final Dialog dialog) {
        return upstream -> upstream.subscribeOn(Schedulers.io()).unsubscribeOn(Schedulers.io()).doOnSubscribe(disposable -> {
            if (dialog != null) {
                dialog.show();
            }
        }).subscribeOn(AndroidSchedulers.mainThread()).observeOn(AndroidSchedulers.mainThread()).doFinally((Action) () -> {
            if (dialog != null) {
                dialog.dismiss();
            }
        });
    }

}
