diff --git a/app/src/main/java/com/xuexiang/rxutil2demo/activity/RxJavaActivity.java b/app/src/main/java/com/xuexiang/rxutil2demo/activity/RxJavaActivity.java index 5f81963..5c2a5bd 100644 --- a/app/src/main/java/com/xuexiang/rxutil2demo/activity/RxJavaActivity.java +++ b/app/src/main/java/com/xuexiang/rxutil2demo/activity/RxJavaActivity.java @@ -43,6 +43,7 @@ import butterknife.OnClick; import io.reactivex.functions.Consumer; import io.reactivex.functions.Function; +import io.reactivex.subscribers.SafeSubscriber; /** * RxJavaUtils演示示例 @@ -103,9 +104,35 @@ public void doInUIThread(String s) { // }, new SimpleSubscriber() { // @Override // public void onSuccess(Integer integer) { -// int a = 100/0; +// Log.e(TAG, "[doInUIThread] " + getLooperStatus() + ", 入参:" + integer); // } // }); +// RxJavaUtils.executeAsyncTask(new Function() { +// @Override +// public Integer apply(Integer integer) throws Exception { +// Log.e(TAG, "[doInIOThread] " + getLooperStatus()); +// return 12345; +// } +// }).subscribe(new Consumer() { +// @Override +// public void accept(Integer integer) throws Exception { +// Log.e(TAG, "[doInUIThread] " + getLooperStatus() + ", 入参:" + integer); +// } +// }); + +// RxJavaUtils.executeAsyncTask2(new Function() { +// @Override +// public Integer apply(Integer integer) throws Exception { +// Log.e(TAG, "[doInIOThread] " + getLooperStatus()); +// return 12345; +// } +// }, new SimpleSubscriber() { +// @Override +// public void onSuccess(Integer integer) { +// Log.e(TAG, "[doInUIThread] " + getLooperStatus() + ", 入参:" + integer); +// } +// }); + RxJavaUtils.executeAsyncTask(new RxAsyncTask("我是入参789") { @Override public Integer doInIOThread(String s) { diff --git a/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/RxJavaUtils.java b/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/RxJavaUtils.java index 516081f..831cfb9 100644 --- a/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/RxJavaUtils.java +++ b/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/RxJavaUtils.java @@ -149,9 +149,9 @@ public static Disposable polling(long initialDelay, long interval, @NonNull Cons /** * 轮询操作 * - * @param initialDelay 初始延迟 - * @param interval 轮询间期 - * @param unit 轮询间期时间单位 + * @param initialDelay 初始延迟 + * @param interval 轮询间期 + * @param unit 轮询间期时间单位 */ public static Flowable polling(long initialDelay, long interval, @NonNull TimeUnit unit) { return Flowable.interval(initialDelay, interval, unit) @@ -174,6 +174,7 @@ public static Disposable polling(long initialDelay, long interval, @NonNull Time } //=================倒计时=================// + /** * 倒计时操作【间隔1秒】 * @@ -245,8 +246,8 @@ public static Disposable delay(@NonNull long delayTime, @NonNull TimeUnit unit, /** * 延迟操作 * - * @param delayTime 延迟时间 - * @param unit 延迟时间单位 + * @param delayTime 延迟时间 + * @param unit 延迟时间单位 */ public static Observable delay(long delayTime, @NonNull TimeUnit unit) { return Observable.timer(delayTime, unit) @@ -269,9 +270,9 @@ public static Disposable delay(long delayTime, @NonNull TimeUnit unit, @NonNull /** * 延迟操作 * - * @param t 发射源 - * @param delayTime 延迟时间 - * @param unit 延迟时间单位 + * @param t 发射源 + * @param delayTime 延迟时间 + * @param unit 延迟时间单位 */ public static Observable delay(@NonNull T t, long delayTime, @NonNull TimeUnit unit) { return Observable.just(t) @@ -359,14 +360,13 @@ public static Flowable executeAsyncTask(@NonNull T t, @NonNull Functio /** * 执行异步任务(IO线程处理,UI线程显示) * - * @param t 处理入参 * @param func1 动作 * @return */ - public static Observable executeAsyncTask2(@NonNull T t, @NonNull Function func1) { - return Observable.just(t) + public static Flowable executeAsyncTask(@NonNull Function func1) { + return Flowable.just(1) .map(func1) - .compose(RxSchedulerUtils._io_main_o()); + .compose(RxSchedulerUtils._io_main_f()); } /** @@ -395,6 +395,31 @@ public static Disposable executeAsyncTask(@NonNull T t, @NonNull Function return executeAsyncTask(t, func1).subscribe(consumer, errorConsumer); } + /** + * 执行异步任务(IO线程处理,UI线程显示) + * + * @param t 处理入参 + * @param func1 动作 + * @return + */ + public static Observable executeAsyncTask2(@NonNull T t, @NonNull Function func1) { + return Observable.just(t) + .map(func1) + .compose(RxSchedulerUtils._io_main_o()); + } + + /** + * 执行异步任务(IO线程处理,UI线程显示) + * + * @param func1 动作 + * @return + */ + public static Observable executeAsyncTask2(@NonNull Function func1) { + return Observable.just(1) + .map(func1) + .compose(RxSchedulerUtils._io_main_o()); + } + /** * 执行异步任务(IO线程处理,UI线程显示) * @@ -407,6 +432,17 @@ public static Disposable executeAsyncTask2(@NonNull T t, @NonNull Functio return executeAsyncTask2(t, func1).subscribeWith(subscriber); } + /** + * 执行异步任务(IO线程处理,UI线程显示) + * + * @param func1 动作 + * @param subscriber 订阅事件 + * @return + */ + public static Disposable executeAsyncTask2(@NonNull Function func1, @NonNull BaseSubscriber subscriber) { + return executeAsyncTask2(func1).subscribeWith(subscriber); + } + //==================Transformer=====================// /** diff --git a/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/RxSchedulerUtils.java b/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/RxSchedulerUtils.java index 6c865fa..67325eb 100644 --- a/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/RxSchedulerUtils.java +++ b/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/RxSchedulerUtils.java @@ -18,12 +18,9 @@ import com.xuexiang.rxutil2.rxjava.scheduler.SchedulerType; -import org.reactivestreams.Publisher; - import io.reactivex.Flowable; import io.reactivex.FlowableTransformer; import io.reactivex.Observable; -import io.reactivex.ObservableSource; import io.reactivex.ObservableTransformer; import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.schedulers.Schedulers; diff --git a/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/SchedulerTransformer.java b/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/SchedulerTransformer.java index 5668c62..bcd037e 100644 --- a/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/SchedulerTransformer.java +++ b/rxutil2/src/main/java/com/xuexiang/rxutil2/rxjava/SchedulerTransformer.java @@ -27,7 +27,7 @@ * @author xuexiang * @since 2018/6/12 下午11:25 */ -public class SchedulerTransformer implements ObservableTransformer, FlowableTransformer, SingleTransformer, MaybeTransformer , CompletableTransformer { +public class SchedulerTransformer implements ObservableTransformer, FlowableTransformer, SingleTransformer, MaybeTransformer, CompletableTransformer { /** * 线程类型 @@ -42,23 +42,19 @@ public SchedulerTransformer(SchedulerType schedulerType) { public ObservableSource apply(Observable upstream) { switch (mSchedulerType) { case _main: - upstream.observeOn(AndroidSchedulers.mainThread()); - break; + return upstream.observeOn(AndroidSchedulers.mainThread()); case _io: - upstream.observeOn(Schedulers.io()); - break; + return upstream.observeOn(Schedulers.io()); case _io_main: - upstream + return upstream .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); - break; case _io_io: - upstream + return upstream .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(Schedulers.io()); - break; default: break; } @@ -69,23 +65,19 @@ public ObservableSource apply(Observable upstream) { public Publisher apply(Flowable upstream) { switch (mSchedulerType) { case _main: - upstream.observeOn(AndroidSchedulers.mainThread()); - break; + return upstream.observeOn(AndroidSchedulers.mainThread()); case _io: - upstream.observeOn(Schedulers.io()); - break; + return upstream.observeOn(Schedulers.io()); case _io_main: - upstream + return upstream .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); - break; case _io_io: - upstream + return upstream .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(Schedulers.io()); - break; default: break; } @@ -96,23 +88,19 @@ public Publisher apply(Flowable upstream) { public MaybeSource apply(Maybe upstream) { switch (mSchedulerType) { case _main: - upstream.observeOn(AndroidSchedulers.mainThread()); - break; + return upstream.observeOn(AndroidSchedulers.mainThread()); case _io: - upstream.observeOn(Schedulers.io()); - break; + return upstream.observeOn(Schedulers.io()); case _io_main: - upstream + return upstream .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); - break; case _io_io: - upstream + return upstream .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(Schedulers.io()); - break; default: break; } @@ -123,23 +111,19 @@ public MaybeSource apply(Maybe upstream) { public SingleSource apply(Single upstream) { switch (mSchedulerType) { case _main: - upstream.observeOn(AndroidSchedulers.mainThread()); - break; + return upstream.observeOn(AndroidSchedulers.mainThread()); case _io: - upstream.observeOn(Schedulers.io()); - break; + return upstream.observeOn(Schedulers.io()); case _io_main: - upstream + return upstream .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); - break; case _io_io: - upstream + return upstream .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(Schedulers.io()); - break; default: break; } @@ -150,23 +134,19 @@ public SingleSource apply(Single upstream) { public CompletableSource apply(Completable upstream) { switch (mSchedulerType) { case _main: - upstream.observeOn(AndroidSchedulers.mainThread()); - break; + return upstream.observeOn(AndroidSchedulers.mainThread()); case _io: - upstream.observeOn(Schedulers.io()); - break; + return upstream.observeOn(Schedulers.io()); case _io_main: - upstream + return upstream .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()); - break; case _io_io: - upstream + return upstream .subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(Schedulers.io()); - break; default: break; } diff --git a/rxutil2/src/main/java/com/xuexiang/rxutil2/subsciber/ProgressDialogLoader.java b/rxutil2/src/main/java/com/xuexiang/rxutil2/subsciber/ProgressDialogLoader.java index 05e5d6e..c08a7c3 100644 --- a/rxutil2/src/main/java/com/xuexiang/rxutil2/subsciber/ProgressDialogLoader.java +++ b/rxutil2/src/main/java/com/xuexiang/rxutil2/subsciber/ProgressDialogLoader.java @@ -50,11 +50,7 @@ public ProgressDialogLoader(Context context, String msg) { @Override public boolean isLoading() { - if (mDialog != null) { - return mDialog.isShowing(); - } else { - return false; - } + return mDialog != null && mDialog.isShowing(); } @Override