This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |