From 287c94a567e4f89ab3321e0e323d64aa737018b0 Mon Sep 17 00:00:00 2001 From: Zhanty Date: Fri, 16 Aug 2019 11:50:18 +0800 Subject: [PATCH] lib_bet support Rx Single --- .../sdk/net/core/HttpResultTransformer.java | 17 ++++++++++++++++- .../java/com/android/sdk/net/kit/RxResultKit.kt | 13 +++++++++++++ .../sdk/net/provider/PostTransformer.java | 5 +++-- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/lib_network/src/main/java/com/android/sdk/net/core/HttpResultTransformer.java b/lib_network/src/main/java/com/android/sdk/net/core/HttpResultTransformer.java index 270cf1a..35d792c 100644 --- a/lib_network/src/main/java/com/android/sdk/net/core/HttpResultTransformer.java +++ b/lib_network/src/main/java/com/android/sdk/net/core/HttpResultTransformer.java @@ -17,8 +17,11 @@ import io.reactivex.FlowableTransformer; import io.reactivex.Observable; import io.reactivex.ObservableSource; import io.reactivex.ObservableTransformer; +import io.reactivex.Single; +import io.reactivex.SingleSource; +import io.reactivex.SingleTransformer; -public class HttpResultTransformer> implements ObservableTransformer, FlowableTransformer { +public class HttpResultTransformer> implements ObservableTransformer, FlowableTransformer, SingleTransformer { private final boolean mRequireNonNullData; private final DataExtractor mDataExtractor; @@ -57,6 +60,18 @@ public class HttpResultTransformer apply(Single upstream) { + Single downstreamSingle = upstream.map(this::processData); + @SuppressWarnings("unchecked") + PostTransformer postTransformer = (PostTransformer) NetContext.get().netProvider().postTransformer(); + if (postTransformer != null) { + return downstreamSingle.compose(postTransformer); + } else { + return downstreamSingle; + } + } + private Downstream processData(Result rResult) { if (rResult == null) { diff --git a/lib_network/src/main/java/com/android/sdk/net/kit/RxResultKit.kt b/lib_network/src/main/java/com/android/sdk/net/kit/RxResultKit.kt index 1184e40..4afc103 100644 --- a/lib_network/src/main/java/com/android/sdk/net/kit/RxResultKit.kt +++ b/lib_network/src/main/java/com/android/sdk/net/kit/RxResultKit.kt @@ -4,6 +4,7 @@ import com.android.sdk.net.core.Result import com.github.dmstocking.optional.java.util.Optional import io.reactivex.Flowable import io.reactivex.Observable +import io.reactivex.Single fun , E> Observable.optionalExtractor(): Observable> { @@ -30,6 +31,18 @@ fun > Flowable.resultChecker(): Flowable> { return (this.compose(ResultHandlers.resultChecker())) } +fun , E> Single.optionalExtractor(): Single> { + return this.compose(ResultHandlers.optionalExtractor()) +} + +fun , E> Single.resultExtractor(): Single { + return this.compose(ResultHandlers.resultExtractor()) +} + +fun > Single.resultChecker(): Single> { + return (this.compose(ResultHandlers.resultChecker())) +} + /**组合远程数据与本地数据,参考 [RxResultKit.composeMultiSource]*/ fun composeMultiSource( remote: Flowable>, diff --git a/lib_network/src/main/java/com/android/sdk/net/provider/PostTransformer.java b/lib_network/src/main/java/com/android/sdk/net/provider/PostTransformer.java index 3fdf0da..77d7c63 100644 --- a/lib_network/src/main/java/com/android/sdk/net/provider/PostTransformer.java +++ b/lib_network/src/main/java/com/android/sdk/net/provider/PostTransformer.java @@ -4,6 +4,7 @@ import com.android.sdk.net.kit.ResultHandlers; import io.reactivex.FlowableTransformer; import io.reactivex.ObservableTransformer; +import io.reactivex.SingleTransformer; /** * 经过 {@link ResultHandlers} 处理网络结果后,可以添加此接口来添加统一的再处理逻辑,比如 token 实现后的重试。 @@ -12,6 +13,6 @@ import io.reactivex.ObservableTransformer; * Email: ztiany3@gmail.com * Date : 2018-12-21 14:31 */ -public interface PostTransformer extends ObservableTransformer, FlowableTransformer { +public interface PostTransformer extends ObservableTransformer, FlowableTransformer, SingleTransformer { -} +} \ No newline at end of file