Skip to content

Instantly share code, notes, and snippets.

@Diolor
Last active January 13, 2021 09:26
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save Diolor/a17ddb78d3d45b452f02 to your computer and use it in GitHub Desktop.
Save Diolor/a17ddb78d3d45b452f02 to your computer and use it in GitHub Desktop.
Retry with Connection
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Looper;
import rx.Observable;
import rx.Scheduler;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action0;
import rx.subscriptions.Subscriptions;
public class BroadcastObservable implements Observable.OnSubscribe<Boolean> {
private final Context context;
public static Observable<Boolean> fromConnectivityManager(Context context) {
return Observable.create(new BroadcastObservable(context))
.share();
}
public BroadcastObservable(Context context) {
this.context = context;
}
@Override
public void call(Subscriber<? super Boolean> subscriber) {
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
subscriber.onNext(isConnectedToInternet());
}
};
context.registerReceiver(receiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
subscriber.add(unsubscribeInUiThread(() -> context.unregisterReceiver(receiver)));
}
private boolean isConnectedToInternet() {
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
return networkInfo != null && networkInfo.isConnected();
}
private static Subscription unsubscribeInUiThread(final Action0 unsubscribe) {
return Subscriptions.create(() -> {
if (Looper.getMainLooper() == Looper.myLooper()) {
unsubscribe.call();
} else {
final Scheduler.Worker inner = AndroidSchedulers.mainThread().createWorker();
inner.schedule(() -> {
unsubscribe.call();
inner.unsubscribe();
});
}
});
}
}
import android.content.Context;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import rx.Observable;
import rx.functions.Func1;
public class RetryWithConnectivityIncremental implements Func1<Observable<? extends Throwable>, Observable<?>> {
private final int maxTimeout;
private final TimeUnit timeUnit;
private final Observable<Boolean> isConnected;
private final int startTimeOut;
private int timeout;
public RetryWithConnectivityIncremental(Context context, int startTimeOut, int maxTimeout, TimeUnit timeUnit) {
this.startTimeOut = startTimeOut;
this.maxTimeout = maxTimeout;
this.timeUnit = timeUnit;
this.timeout = startTimeOut;
isConnected = getConnectedObservable(context);
}
@Override
public Observable<?> call(Observable<? extends Throwable> observable) {
return observable.flatMap((Throwable throwable) -> {
if (throwable instanceof RetrofitError && ((RetrofitError) throwable).getKind() == RetrofitError.Kind.NETWORK) {
return isConnected;
} else {
return Observable.error(throwable);
}
}).compose(attachIncementalTimeout());
}
private Observable.Transformer<Boolean, Boolean> attachIncementalTimeout() {
return observable -> observable.timeout(timeout, timeUnit)
.doOnError(throwable -> {
if (throwable instanceof TimeoutException) {
timeout = timeout > maxTimeout ? maxTimeout : timeout + startTimeOut;
}
});
}
private Observable<Boolean> getConnectedObservable(Context context) {
return BroadcastObservable.fromConnectivityManager(context)
.distinctUntilChanged()
.filter(isConnected -> isConnected);
}
}
@abhimuktheeswarar
Copy link

I read your article https://lorentzos.com/improving-ux-with-rxjava-4440a13b157f.
Thanks for writing
I tried using the above code, but getting errors since I'm using Rxjava2 in my project.
Can you pls provide BroadcastObservable.java & RetryWithConnectivityIncremental.java based on RxJava2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment