mzgreen
02/08/2017, 11:21 AMval DELAY = 200L
val LONG_DELAY = 1200L
val requestObservable = retrofit.foo().startWith(null)
val timerObservable = Observable.just(DELAY, LONG_DELAY).concatMap { it -> Observable.just(it).delay(it, TimeUnit.MILLISECONDS) }.startWith(0L)
Observable.combineLatest(timerObservable, requestObservable)
{ time, response -> Pair(time, response) }
.subscribe {
val time = it.first
val response = it.second
if (time < DELAY && response != null) {
// show result without progress
} else if (time >= LONG_DELAY && response != null) {
// hide already shown progress and show result
} else if (time >= DELAY && time < LONG_DELAY && response == null) {
// show progress
}
}
Any suggestions on how to improve it? I don’t like that I have to wait until timer finishes even if I’ve already handled the result, for example if response will be available after 100ms then I’ll handle it and then I’ll get 2 more notifications because of timerObservable.