Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Kotlin-Coroutines-and-Flow-UseCases-on-Android/app/src/main/java/com/lukaslechner/coroutineusecasesonandroid/usecases/coroutines/usecase7/rx/TimeoutAndRetryRxViewModel.kt
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
61 lines (53 sloc)
2.07 KB
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.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase7.rx | |
import com.lukaslechner.coroutineusecasesonandroid.base.BaseViewModel | |
import com.lukaslechner.coroutineusecasesonandroid.mock.VersionFeatures | |
import io.reactivex.Single | |
import io.reactivex.android.schedulers.AndroidSchedulers | |
import io.reactivex.disposables.CompositeDisposable | |
import io.reactivex.functions.BiFunction | |
import io.reactivex.rxkotlin.addTo | |
import io.reactivex.rxkotlin.subscribeBy | |
import io.reactivex.schedulers.Schedulers | |
import timber.log.Timber | |
import java.util.concurrent.TimeUnit | |
class TimeoutAndRetryRxViewModel( | |
private val api: RxMockApi = mockApi() | |
) : BaseViewModel<UiState>() { | |
private val disposables = CompositeDisposable() | |
fun performNetworkRequest() { | |
uiState.value = UiState.Loading | |
val timeout = 1000L | |
val numberOfRetries = 2 | |
Single.zip( | |
api.getAndroidVersionFeatures(27) | |
.timeout(timeout, TimeUnit.MILLISECONDS) | |
.retry { x, e -> | |
Timber.e(e) | |
x <= numberOfRetries | |
}, | |
api.getAndroidVersionFeatures(28) | |
.timeout(timeout, TimeUnit.MILLISECONDS) | |
.retry { x, e -> | |
Timber.e(e) | |
x <= numberOfRetries | |
}, | |
BiFunction<VersionFeatures, VersionFeatures, List<VersionFeatures>> { versionFeaturesOreo, versionFeaturesPie -> | |
listOf(versionFeaturesOreo, versionFeaturesPie) | |
}) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribeBy( | |
onSuccess = { versionFeatures -> | |
uiState.value = UiState.Success(versionFeatures) | |
}, | |
onError = { error -> | |
Timber.e(error) | |
uiState.value = UiState.Error("Network Request failed") | |
}) | |
.addTo(disposables) | |
} | |
override fun onCleared() { | |
super.onCleared() | |
disposables.clear() | |
} | |
} |