RxLifeCycle使用工具类

public class RxLifeCycleUtils {

    /**
     * RxLifeCycle绑定RxJava的生命周期
     *
     * @param baseView activity 或fragment继承的接口view
     * @param       RxJava数据泛型
     * @return
     */
    public static  LifecycleTransformer bindToLifecycle(BaseView baseView) {
        if (baseView != null) {
            if (baseView instanceof RxAppCompatActivity) {
                return ((RxAppCompatActivity) baseView).bindToLifecycle();
            } else if (baseView instanceof RxFragmentActivity) {
                return ((RxFragmentActivity) baseView).bindToLifecycle();
            } else if (baseView instanceof RxFragment) {
                return ((RxFragment) baseView).bindToLifecycle();
            } else if (baseView instanceof RxDialogFragment) {
                return ((RxDialogFragment) baseView).bindToLifecycle();
            } else if (baseView instanceof RxAppCompatDialogFragment) {
                return ((RxAppCompatDialogFragment) baseView).bindToLifecycle();
            } else {
                throw new IllegalArgumentException("view isn't activity or fragment");
            }
        } else {
            throw new IllegalArgumentException("view is null");
        }
    }

    /**
     * RxLifeCycle绑定RxJava的生命周期 ,支持指定某个生命周期
     *
     * @param baseView activity 继承的接口view
     * @param event    activity的生命周期
     * @param       RxJava数据泛型
     * @return
     */
    public static  LifecycleTransformer bindUntilEvent(BaseView baseView, ActivityEvent event) {
        if (baseView != null) {
            if (baseView instanceof RxAppCompatActivity) {
                return ((RxAppCompatActivity) baseView).bindUntilEvent(event);
            } else if (baseView instanceof RxFragmentActivity) {
                return ((RxFragmentActivity) baseView).bindUntilEvent(event);
            } else {
                throw new IllegalArgumentException("view isn't activity");
            }
        } else {
            throw new IllegalArgumentException("view is null");
        }
    }

    /**
     * RxLifeCycle绑定RxJava的生命周期,支持指定某个生命周期
     *
     * @param baseView fragment继承的接口view
     * @param event    fragment的生命周期
     * @param       RxJava数据泛型
     * @return
     */
    public static  LifecycleTransformer bindUntilEvent(BaseView baseView, FragmentEvent event) {
        if (baseView != null) {
            if (baseView instanceof RxFragment) {
                return ((RxFragment) baseView).bindUntilEvent(event);
            } else if (baseView instanceof RxDialogFragment) {
                return ((RxDialogFragment) baseView).bindUntilEvent(event);
            } else if (baseView instanceof RxAppCompatDialogFragment) {
                return ((RxAppCompatDialogFragment) baseView).bindUntilEvent(event);
            } else {
                throw new IllegalArgumentException("view isn't  fragment");
            }
        } else {
            throw new IllegalArgumentException("view is null");
        }
    }
}

实例1

  public void interval() {
        model.getIntervalData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifeCycleUtils.bindToLifecycle(view))
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Integer aLong) throws Exception {
                        LogUtils.error(TAG, "rxJavaConcatExample--Consumer--:" + Thread.currentThread().getName() + "--:" + aLong);
                    }
                });
    }

实例2

  public void intervalRxLifeCycle(ActivityEvent event) {
        model.getIntervalData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxLifeCycleUtils.bindUntilEvent(view, event))
                .subscribe(new Consumer() {
                    @Override
                    public void accept(Integer aLong) throws Exception {
                        LogUtils.error(TAG, "rxJavaConcatExample--Consumer--:" + Thread.currentThread().getName() + "--:" + aLong);
                    }
                });
    }

你可能感兴趣的:(RxLifeCycle使用工具类)