lib_bet support Rx Single

androidx
Zhanty 5 years ago
parent 67fccd64ef
commit 287c94a567
  1. 17
      lib_network/src/main/java/com/android/sdk/net/core/HttpResultTransformer.java
  2. 13
      lib_network/src/main/java/com/android/sdk/net/kit/RxResultKit.kt
  3. 3
      lib_network/src/main/java/com/android/sdk/net/provider/PostTransformer.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<Upstream, Downstream, T extends Result<Upstream>> implements ObservableTransformer<T, Downstream>, FlowableTransformer<T, Downstream> {
public class HttpResultTransformer<Upstream, Downstream, T extends Result<Upstream>> implements ObservableTransformer<T, Downstream>, FlowableTransformer<T, Downstream>, SingleTransformer<T, Downstream> {
private final boolean mRequireNonNullData;
private final DataExtractor<Downstream, Upstream> mDataExtractor;
@ -57,6 +60,18 @@ public class HttpResultTransformer<Upstream, Downstream, T extends Result<Upstre
}
}
@Override
public SingleSource<Downstream> apply(Single<T> upstream) {
Single<Downstream> downstreamSingle = upstream.map(this::processData);
@SuppressWarnings("unchecked")
PostTransformer<Downstream> postTransformer = (PostTransformer<Downstream>) NetContext.get().netProvider().postTransformer();
if (postTransformer != null) {
return downstreamSingle.compose(postTransformer);
} else {
return downstreamSingle;
}
}
private Downstream processData(Result<Upstream> rResult) {
if (rResult == null) {

@ -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 <T : Result<E>, E> Observable<T>.optionalExtractor(): Observable<Optional<E>> {
@ -30,6 +31,18 @@ fun <E, T : Result<E>> Flowable<T>.resultChecker(): Flowable<Result<E>> {
return (this.compose(ResultHandlers.resultChecker<E>()))
}
fun <T : Result<E>, E> Single<T>.optionalExtractor(): Single<Optional<E>> {
return this.compose(ResultHandlers.optionalExtractor<E>())
}
fun <T : Result<E>, E> Single<T>.resultExtractor(): Single<E> {
return this.compose(ResultHandlers.resultExtractor<E>())
}
fun <E, T : Result<E>> Single<T>.resultChecker(): Single<Result<E>> {
return (this.compose(ResultHandlers.resultChecker<E>()))
}
/**组合远程数据与本地数据,参考 [RxResultKit.composeMultiSource]*/
fun <T> composeMultiSource(
remote: Flowable<Optional<T>>,

@ -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<Data> extends ObservableTransformer<Data, Data>, FlowableTransformer<Data, Data> {
public interface PostTransformer<Data> extends ObservableTransformer<Data, Data>, FlowableTransformer<Data, Data>, SingleTransformer<Data, Data> {
}
Loading…
Cancel
Save