I have a http request observable and I have such l...
# rx
m
I have a http request observable and I have such logic for showing progress: if making a request took <200ms then don’t show progress at all if it took >=200ms then show progress for at least 1s My code looks somewhat like this:
Copy code
val 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.