Skip to content

Instantly share code, notes, and snippets.

@Piasy
Last active February 1, 2016 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Piasy/fa4fe934e7feaa3fbedb to your computer and use it in GitHub Desktop.
Save Piasy/fa4fe934e7feaa3fbedb to your computer and use it in GitHub Desktop.
static Observable<Integer> detect(Observable<Void> clicks, final long maxIntervalMillis,
final int minComboTimesCared) {
return clicks.map(new Func1<Void, Integer>() {
@Override
public Integer call(Void aVoid) {
return 1;
}
}).timestamp()
.scan(new Func2<Timestamped<Integer>, Timestamped<Integer>, Timestamped<Integer>>() {
@Override
public Timestamped<Integer> call(Timestamped<Integer> lastOne,
Timestamped<Integer> thisOne) {
if (thisOne.getTimestampMillis() - lastOne.getTimestampMillis() <=
maxIntervalMillis) {
return new Timestamped<>(thisOne.getTimestampMillis(),
lastOne.getValue() + 1);
} else {
return new Timestamped<>(thisOne.getTimestampMillis(), 1);
}
}
}).map(new Func1<Timestamped<Integer>, Integer>() {
@Override
public Integer call(Timestamped<Integer> timestamped) {
return timestamped.getValue();
}
}).filter(new Func1<Integer, Boolean>() {
@Override
public Boolean call(Integer combo) {
return combo >= minComboTimesCared;
}
});
}
new RxComboDetector.Builder()
.maxIntervalMillis(500)
.minComboTimesCared(3)
.detectOn(mBtnCombo)
.start()
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<Integer>() {
@Override
public void call(Integer combo) {
// do something
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment