Skip to content

Instantly share code, notes, and snippets.

View ragdroid's full-sized avatar
🤪

Garima Jain ragdroid

🤪
View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / LocalResponseDispatcher.java
Created March 5, 2017 22:14
MockWebServer Dispatcher to Map the server requests to their json files
public class LocalResponseDispatcher extends QueueDispatcher {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
MockResponse mockResponse = new MockResponse();
String scenario = getScenario(request);
if (scenario != null) {
try {
mockResponse.setBody(readFile(scenario));
@ragdroid
ragdroid / TestGetPokemon.java
Created March 5, 2017 22:26
Use MockWebserver to test getPokemon
@Test
public void testOnSceneAdded() {
reset(mainSceneMock);
MainPresenterImpl presenter = new MainPresenterImpl(schedulersProvider, pokemonService);
presenter.onSceneAdded(mainSceneMock, null);
testScheduler.triggerActions();
@ragdroid
ragdroid / TestHttpUnavailable.java
Created March 5, 2017 22:32
Use MockWebserver to test error cases
@Test
public void testDemoResponseError503() {
reset(mainSceneMock);
MainPresenterImpl presenter = new MainPresenterImpl(schedulersProvider, pokemonService);
MockResponse response = new MockResponse();
response.setResponseCode(HttpURLConnection.HTTP_UNAVAILABLE);
getErrorMockWebServer().enqueue(response);
@ragdroid
ragdroid / SocketIOTest.java
Created March 5, 2017 22:40
Use MockWebServer to test your app under SocketIO fail.
@Test
public void testDemoResponseErrorSocket() {
reset(mainSceneMock);
MainPresenterImpl presenter = new MainPresenterImpl(schedulersProvider, pokemonService);
MockResponse response = new MockResponse();
response.setBody("\"message\":\"Hello\"").throttleBody(1, 2, TimeUnit.SECONDS);