Reputation: 4009
I have UserConfig
that I would like to download during splash screen.
class UserManager {
Single<UserConfig> loadConfig()
}
After downloading of the UserConfig
, user is redirected to the next screen. I do something like these:
@Override
public void onResume(boolean isNewView) {
subscriptions.add(
userManager.loadConfig().subscribe(config -> {
applyConfig(config);
launchActivity(HomeActivity.class);
}, error -> {
//some error handling
})
);
}
However, I would like to show the splash screen for at least 1 second. (If loading took less than 1s add extra delay)
I think .delay()
, .delaySubscription()
will not work for my case, since they will delay every request (no matter was it shorter that 1s or not).
Upvotes: 3
Views: 1066
Reputation: 81
My solution with kotlin extention function for Single type. This delay work similarly with errors
/**
* sets the minimum delay on the success or error
*/
fun <T> Single<T>.minDelay(time: Long, unit: TimeUnit, scheduler: Scheduler = Schedulers.computation()): Single<T> {
val timeStart = scheduler.now(TimeUnit.MILLISECONDS)
val delayInMillis = TimeUnit.MILLISECONDS.convert(time, unit)
return Single.zip(
Single.timer(time, unit, scheduler),
this.onErrorResumeNext { error: Throwable ->
val afterError = scheduler.now(TimeUnit.MILLISECONDS)
val millisPassed = afterError - timeStart
val needWaitDelay = delayInMillis - millisPassed
if (needWaitDelay > 0)
Single.error<T>(error)
.delay(needWaitDelay, TimeUnit.MILLISECONDS, scheduler, true)
else
Single.error<T>(error)
},
BiFunction { _, t2 -> t2 }
)
}
Upvotes: 1
Reputation: 136
Try Zip operator
Returns a Single that emits the results of a specified combiner function > applied to two items emitted by two other Singles.
You can do something like
Single
.zip(
Single.timer(1, TimeUnit.SECONDS),
userManager.loadConfig(),
(time, config) -> config
)
.subscribe(
config -> {
applyConfig(config);
launchActivity(HomeActivity.class);
}, error -> {
//some error handling
}
);
Upvotes: 3