Skip to content

Instantly share code, notes, and snippets.

View ragdroid's full-sized avatar
🤪

Garima Jain ragdroid

🤪
View GitHub Profile
@ragdroid
ragdroid / MockitoError.java
Last active March 10, 2017 18:21
How to be a MockStar - Issue Errors
if (throwable instanceof HttpException) {
if (((HttpException) throwable).code() ==
HttpURLConnection.HTTP_NOT_FOUND) {
if (getView() != null) {
getView().showErrorDialog("Lost!");
}
} else if (((HttpException) throwable).code() ==
HttpURLConnection.HTTP_UNAVAILABLE) {
if (getView() != null) {
@ragdroid
ragdroid / Pokemon.java
Last active March 10, 2017 18:18
How to be Mockstar - Problem
pokeSource.getPokemonAbilityStringObservable("12")
.subscribeOn(provider.io())
.observeOn(provider.ui())
.subscribe(new Consumer<Pokemon>() {
@Override
public void accept(@NonNull String pokemonAbility) throws Exception {
if (getView() != null) {
getView().setApiText(pokemonAbility);
}
@ragdroid
ragdroid / AutoCompleteFlatMap.java
Created December 5, 2016 22:02
Order is not maintain if we use flatMap() while performing "Auto-Complete Search" .
//Order is not maintain if we use flatMap() while performing "Auto-Complete Search"
publishSubject
.debounce(300, TimeUnit.MILLISECONDS)
.flatMap(new Function<String, ObservableSource<List<Book>>>() {
@Override
public ObservableSource<List<Book>> apply(String s) throws Exception {
Log.d(TAG, "getting books for " + s);
return dataSource.getBook(s);
}
@ragdroid
ragdroid / ConcatumMapum.java
Created December 5, 2016 22:01
Use concatMap() to maintain order while performing "Auto-Complete Search"
//Use concat-Map() to maintain order while performing "Auto-Complete Search"
publishSubject
.debounce(300, TimeUnit.MILLISECONDS)
.concatMap(new Function<String, ObservableSource<List<Book>>>() {
@Override
public ObservableSource<List<Book>> apply(String s) throws Exception {
Log.d(TAG, "getting books for " + s);
return dataSource.getBook(s);
}
@ragdroid
ragdroid / NetworkThenCache.java
Created November 15, 2016 20:55
Hit Network first and the cache
service.startupDetails(request)
.doOnNext(new Action1<StartupDetails>() {
@Override
public void call(StartupDetails startupDetails) {
startupStore.putStartupDetails(startupDetails);
}
})
.onErrorReturn(new Function<Throwable, StartupDetails>() {
@Override
@ragdroid
ragdroid / Repeatium.java
Last active September 11, 2016 16:23
Session Renew Problem - DroidconIN 2016
// Use concat and repeat to renew the session repeatedly
Observable<Session> getRenewedSession() {
Observable<Session> sessionObservable =
SessionManager.getCurrentSessionObservable()
.filter(new Func1<Session, Boolean>() {
@Override
public Boolean call(Session session) {
return session.isValid();
}
@ragdroid
ragdroid / Retry.java
Created September 7, 2016 21:37
Retry when IOException example DroidconIN 2016
//Retry when IOException
Observable<UserResponse> getUserFromServer() {
return userService.getUserFromServer()
.retryWhen(new Func1<Observable<? extends Throwable>, Observable<?>>() {
@Override
public Observable<?> call(Observable<? extends Throwable> observable) {
return observable.flatMap(new Func1<Throwable, Observable<?>>() {
@Override
public Observable<?> call(Throwable throwable) {
@ragdroid
ragdroid / Riddikulus.java
Last active September 7, 2016 21:04
Example of RxJava repeat and retry operators - DroidconIN 2016
// Example of repeat and retry
private Observable<Boggart> riddikulus() {
return getBoggartObservable()
.flatMap(new Func1<Boggart, Observable<Boggart>>() {
@Override
public Observable<Boggart> call(Boggart boggart) {
boggart.init();
boggart.riddikulus();
if (boggart.isFunny()) {
@ragdroid
ragdroid / Boggart.java
Last active September 7, 2016 20:59
Example of Rxjava retry and repeat operator - DroidconIN 2016
private class Boggart {
private static final int MAX_LAUGHTER_LEVEL = 10;
private static final int MIN_LAUNGHTER_LEVEL = 0;
private static final int LAUGHTER_FUNNY_THRESHOLD = 5;
private static final int LAUGHTER_HILARIOUS_THRESH = 7;
private int laughterLevel;
public Boggart() {
init();
@ragdroid
ragdroid / AutoSearch.java
Created August 21, 2016 18:01
AutoSearch - debounce the search terms Droidcon 2016
//AutoSearch using debounce spell
public Observable<SearchRequest> autoSearch() {
return getSearchtermObservable()
.debounce(500, TimeUnit.MILLISECONDS)
.flatMap(new Func1<String, Observable<SearchRequest>>() {
@Override
public Observable<SearchRequest> call(String term) {
return Observable.just(new SearchRequest(term));
}
});