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
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) { |
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
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); | |
} |
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
//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); | |
} |
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
//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); | |
} |
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
service.startupDetails(request) | |
.doOnNext(new Action1<StartupDetails>() { | |
@Override | |
public void call(StartupDetails startupDetails) { | |
startupStore.putStartupDetails(startupDetails); | |
} | |
}) | |
.onErrorReturn(new Function<Throwable, StartupDetails>() { | |
@Override |
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
// 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(); | |
} |
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
//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) { |
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
// 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()) { |
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 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(); |
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
//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)); | |
} | |
}); |