RxLifeCycle使用工具类

public class RxLifeCycleUtils {

    /**
     * RxLifeCycle绑定RxJava的生命周期
     *
     * @param baseView activity 或fragment继承的接口view
     * @param <L>      RxJava数据泛型
     * @return
     */
    public static <L> LifecycleTransformer<L> 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 <L>      RxJava数据泛型
     * @return
     */
    public static <L> LifecycleTransformer<L> 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 <L>      RxJava数据泛型
     * @return
     */
    public static <L> LifecycleTransformer<L> 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.<Integer>bindToLifecycle(view))
                .subscribe(new Consumer<Integer>() {
                    @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.<Integer>bindUntilEvent(view, event))
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer aLong) throws Exception {
                        LogUtils.error(TAG, "rxJavaConcatExample--Consumer--:" + Thread.currentThread().getName() + "--:" + aLong);
                    }
                });
    }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容