Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
package com.lukaslechner.coroutineusecasesonandroid.usecases.coroutines.usecase7
import androidx.lifecycle.viewModelScope
import com.lukaslechner.coroutineusecasesonandroid.base.BaseViewModel
import com.lukaslechner.coroutineusecasesonandroid.mock.MockApi
import kotlinx.coroutines.*
import timber.log.Timber
class TimeoutAndRetryViewModel(
private val api: MockApi = mockApi()
) : BaseViewModel<UiState>() {
fun performNetworkRequest() {
uiState.value = UiState.Loading
val numberOfRetries = 2
val timeout = 1000L
val oreoVersionsDeferred = viewModelScope.async {
retryWithTimeout(numberOfRetries, timeout) {
api.getAndroidVersionFeatures(27)
}
}
val pieVersionsDeferred = viewModelScope.async {
retryWithTimeout(numberOfRetries, timeout) {
api.getAndroidVersionFeatures(28)
}
}
viewModelScope.launch {
try {
val versionFeatures = listOf(
oreoVersionsDeferred,
pieVersionsDeferred
).awaitAll()
uiState.value = UiState.Success(versionFeatures)
} catch (e: Exception) {
Timber.e(e)
uiState.value = UiState.Error("Network Request failed")
}
}
}
private suspend fun <T> retryWithTimeout(
numberOfRetries: Int,
timeout: Long,
block: suspend () -> T
) = retry(numberOfRetries) {
withTimeout(timeout) {
block()
}
}
private suspend fun <T> retry(
numberOfRetries: Int,
delayBetweenRetries: Long = 100,
block: suspend () -> T
): T {
repeat(numberOfRetries) {
try {
return block()
} catch (exception: Exception) {
Timber.e(exception)
}
delay(delayBetweenRetries)
}
return block() // last attempt
}
}