optimize code

androidx
Zhanty 5 years ago
parent 948015997a
commit 8279f2f191
  1. 2
      lib_base/src/main/java/com/android/base/adapter/recycler/RecyclerAdapter.java
  2. 8
      lib_network/src/main/java/com/android/sdk/net/kit/RxResultKit.java

@ -23,7 +23,7 @@ public abstract class RecyclerAdapter<T, VH extends ViewHolder> extends Recycler
private RecyclerDataManagerImpl<T> mDataManager; private RecyclerDataManagerImpl<T> mDataManager;
@NonNull @NonNull
protected Context mContext; protected final Context mContext;
public RecyclerAdapter(@NonNull Context context, List<T> data) { public RecyclerAdapter(@NonNull Context context, List<T> data) {
mDataManager = new RecyclerDataManagerImpl<>(data, this); mDataManager = new RecyclerDataManagerImpl<>(data, this);

@ -11,7 +11,6 @@ import com.github.dmstocking.optional.java.util.Optional;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import java.io.IOException; import java.io.IOException;
import java.net.ConnectException;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import io.reactivex.flowables.ConnectableFlowable; import io.reactivex.flowables.ConnectableFlowable;
@ -71,7 +70,7 @@ public class RxResultKit {
if (!localData.isPresent()) { if (!localData.isPresent()) {
return remote.doOnNext(tOptional -> onNewData.accept(tOptional.orElse(null))); return remote.doOnNext(tOptional -> onNewData.accept(tOptional.orElse(null)));
} }
/*有缓存网络错误,不触发错误,只有在过期时返回新的数据*/ /*有缓存网络错误,不触发错误,只有在过期时返回新的数据*/
return remote return remote
.onErrorResumeNext(onErrorResumeFunction(onNewData)) .onErrorResumeNext(onErrorResumeFunction(onNewData))
.filter(remoteData -> selector.test(localData.get(), remoteData.orElse(null))) .filter(remoteData -> selector.test(localData.get(), remoteData.orElse(null)))
@ -90,7 +89,7 @@ public class RxResultKit {
if (local != null) { if (local != null) {
return Flowable.concat( return Flowable.concat(
Flowable.just(Optional.of(local)), Flowable.just(Optional.of(local)),
/*有缓存网络错误,不触发错误,只有在过期时返回新的数据*/ /*有缓存网络错误,不触发错误,只有在过期时返回新的数据*/
remote.onErrorResumeNext(onErrorResumeFunction(onNewData)) remote.onErrorResumeNext(onErrorResumeFunction(onNewData))
.filter(tOptional -> selector.test(local, tOptional.orElse(null))) .filter(tOptional -> selector.test(local, tOptional.orElse(null)))
.doOnNext(tOptional -> onNewData.accept(tOptional.orElse(null)))); .doOnNext(tOptional -> onNewData.accept(tOptional.orElse(null))));
@ -114,8 +113,7 @@ public class RxResultKit {
} }
private static boolean isNetworkError(Throwable exception) { private static boolean isNetworkError(Throwable exception) {
return exception instanceof ConnectException return exception instanceof IOException
|| exception instanceof IOException
|| exception instanceof HttpException || exception instanceof HttpException
|| exception instanceof NetworkErrorException; || exception instanceof NetworkErrorException;
} }

Loading…
Cancel
Save