Created
March 7, 2022 21:01
-
-
Save MessiasLima/edd08036bd5d0be8347be722a8823190 to your computer and use it in GitHub Desktop.
StoreViewModelAfter.kt
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
import androidx.lifecycle.ViewModel | |
import com.messiaslima.promogamer.domain.Store | |
import kotlinx.coroutines.FlowPreview | |
import kotlinx.coroutines.flow.catch | |
import kotlinx.coroutines.flow.map | |
import kotlinx.coroutines.flow.onStart | |
@FlowPreview | |
class StoreViewModel(private val storeOrchestrator: StoreOrchestrator) : ViewModel() { | |
private val retryTrigger = RetryTrigger() | |
val uiState = retryableFlow(retryTrigger) { | |
storeOrchestrator.getStores() | |
.map<List<Store>, UiState> { UiState.Success(it) } | |
.catch { emit(UiState.Error(it))} | |
.onStart { emit(UiState.Loading) } | |
} | |
fun tryAgain() { | |
retryTrigger.retry() | |
} | |
sealed class UiState { | |
object Loading : UiState() | |
class Success(stores: List<Store>) : UiState() | |
class Error(throwable: Throwable) : UiState() | |
object Idle : UiState() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment