Last active
February 1, 2016 14:39
-
-
Save Piasy/fa4fe934e7feaa3fbedb to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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; | |
} | |
}); | |
} |
This file contains hidden or 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
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