Created
March 13, 2015 09:20
-
-
Save tehmou/143f2e056e3768d4549b to your computer and use it in GitHub Desktop.
android-meetup-rxjava-challenge solution
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
package com.futurice.project; | |
import java.util.concurrent.TimeUnit; | |
import rx.Observable; | |
import rx.Scheduler; | |
import rx.functions.Func1; | |
public class Solution { | |
public static final String SECRET_SEQUENCE = "ABBABA"; // this is the password the user must input | |
public static final long SEQUENCE_TIMEOUT = 4000; // this is the timeframe in milliseconds | |
/** | |
* Given two buttons (A & B) we want to print a "CORRECT!" message if a certain "secret sequence" | |
* is clicked within a given timeframe. The "secret sequence" is the combination "ABBABA" that | |
* has to be inputted within 4 seconds. In other words, the user must input the password, but | |
* must do it quickly. | |
* | |
* @param inputStream an observable event stream of String, either "A" String or "B" String. | |
* @param scheduler if you need a scheduler, use this one provided. | |
* @return an observable event stream of String, that returns the secret sequence AT THE RIGHT | |
* time according to the problem specification. | |
*/ | |
public static Observable<String> defineSuccessStream(final Observable<String> inputStream, final Scheduler scheduler) { | |
return inputStream | |
.flatMap(new Func1<String, Observable<String>>() { | |
@Override | |
public Observable<String> call(final String s) { | |
return inputStream | |
.startWith(s) | |
.map(new Func1<String, String>() { | |
String input = ""; | |
@Override | |
public String call(String s2) { | |
input += s2; | |
return input; | |
} | |
}) | |
.filter(new Func1<String, Boolean>() { | |
@Override | |
public Boolean call(String s) { | |
return SECRET_SEQUENCE.equals(s); | |
} | |
}) | |
.timeout(SEQUENCE_TIMEOUT, TimeUnit.MILLISECONDS, scheduler) | |
.onErrorResumeNext(new Func1<Throwable, Observable<? extends String>>() { | |
@Override | |
public Observable<? extends String> call(Throwable throwable) { | |
return Observable.empty(); | |
} | |
}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment