Skip to content

Instantly share code, notes, and snippets.

View danylovolokh's full-sized avatar

Danylo Volokh danylovolokh

View GitHub Profile
@danylovolokh
danylovolokh / retryWhen_repeatWhen.java
Last active February 1, 2016 02:02
Server polling and retry operations when failed. With Retrofit and RxJava.
retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
@Override
public Observable<?> call(Observable<? extends Throwable> observable) {
Log.v(TAG, "retryWhen, call");
return observable.compose(zipWithFlatMap());
}
}).repeatWhen(new Func1<Observable<? extends Void>, Observable<?>>() {
@Override
public Observable<?> call(Observable<? extends Void> observable) {
Log.v(TAG, "repeatWhen, call");
retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
@Override
public Observable<?> call(Observable<? extends Throwable> observable) {
Log.v(TAG, "retryWhen, call");
return observable.zipWith(Observable.range(COUNTER_START, ATTEMPTS), new Func2<Throwable, Integer, Integer>() {
@Override
public Integer call(Throwable throwable, Integer attempt) {
Log.v(TAG, "zipWith, call, attempt " + attempt);
return attempt;
@danylovolokh
danylovolokh / repeatWhen_flatMap.java
Last active February 1, 2016 02:02
Server polling and retry operations when failed. With Retrofit and RxJava.
repeatWhen(new Func1<Observable<? extends Void>, Observable<?>>() {
@Override
public Observable<?> call(Observable<? extends Void> observable) {
Log.v(TAG, "repeatWhen, call");
return observable.flatMap(new Func1<Void, Observable<?>>() {
@Override
public Observable<?> call(Void aVoid) {
if(mCounter > ATTEMPTS){
// terminate by ourselves
@danylovolokh
danylovolokh / repeatWhen_zipWith_flatMap_example.java
Last active February 1, 2016 02:00
Server polling and retry operations when failed. With Retrofit and RxJava.
private static final int COUNTER_START = 1;
private static final int ATTEMPTS = 5;
private static final int ORIGINAL_DELAY_IN_SECONDS = 10;
// this is new methods chain in repeatWhen predicate call function
repeatWhen(new Func1<Observable<? extends Void>, Observable<?>>() {
@Override
public Observable<?> call (Observable < ?extends Void > observable){
Log.v(TAG, "repeatWhen, call");
@danylovolokh
danylovolokh / repeatWhen_takeUntil_filter_example.java
Last active February 21, 2021 23:56
Server polling and retry operations when failed. With Retrofit and RxJava.
/**
* This is a class that should be
* mapped on your json response from the server
*/
class ServerPollingResponse {
boolean isJobDone;
@Override
public String toString() {
return "isJobDone=" + isJobDone;
@RunWith(AndroidJUnit4.class)
public class TestAsyncLogin {
private static final String TAG = TestAsyncLogin.class.getSimpleName();
@Rule
public ActivityTestRule<LoginTestActivity> mActivityRule = new ActivityTestRule<>(LoginTestActivity.class);
/**
* This method uses {@link LoginTestActivity} and does few things:
/**
* This activity was created only in test purposes.
* Is can accept a callback by {@link #setLoginCallback(LoginTestCallback)}
* The {@link LoginTestCallback#onHandleResponseCalled(LoginResponse)} will be called when login response arrives.
*
* Using this approach the calling side can validate that login response received.
*/
public class LoginTestActivity extends LoginActivity {
private LoginTestCallback mCallback;
public class LoginActivity extends AppCompatActivity {
//... some fields here
private BroadcastReceiver mLoginBroadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);