Created
October 16, 2020 15:01
-
-
Save BacLuc/1c02214957e727218bb9676710125c73 to your computer and use it in GitHub Desktop.
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.vanniktech.rxriddles.operators.withscheduler; | |
import java.time.Duration; | |
import java.util.List; | |
import java.util.concurrent.Callable; | |
import java.util.concurrent.TimeUnit; | |
import io.reactivex.rxjava3.core.Completable; | |
import io.reactivex.rxjava3.core.Flowable; | |
import io.reactivex.rxjava3.core.Observable; | |
import io.reactivex.rxjava3.core.Scheduler; | |
import io.reactivex.rxjava3.core.Single; | |
public class RiddleCarousel { | |
/** | |
* After each fetchInterval, fetch the current acquirer List. | |
* From the fetched acquirer List, emit the next item after carouselInterval. | |
* Repeat the same acquirer List, until the next time the acquirerList is fetched. | |
* Then use the new acquirer List. | |
* <p> | |
* Use Case: You want to show the available acquirers from an external resource and show each of them for carouselInterval in the UI. | |
*/ | |
public static Observable<String> solve(Callable<List<String>> acquirerCallable, Duration fetchInterval, Duration carouselInterval, Scheduler scheduler) { | |
Flowable<Long> carouselIntervalFlowable = Flowable.interval(carouselInterval.toMillis(), TimeUnit.MILLISECONDS, scheduler) | |
.startWithItem(1L) | |
.publish() | |
.autoConnect(); | |
return Single.fromCallable(acquirerCallable) | |
.subscribeOn(scheduler) | |
.repeatWhen(objectFlowable -> objectFlowable.delay(fetchInterval.toMillis(), TimeUnit.MILLISECONDS, scheduler)) | |
.flatMap(strings -> Flowable.zip(Flowable.fromIterable(strings).take(strings.size()), carouselIntervalFlowable, (s, aLong) -> s) | |
.repeat() | |
.takeUntil(Completable.complete() | |
.delay(fetchInterval.toMillis(), TimeUnit.MILLISECONDS, scheduler) | |
.toFlowable())) | |
.toObservable(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment