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);
}
});
}
河马过河微信公众号.jpg
网友评论