Skip to content

Instantly share code, notes, and snippets.

@MessiasLima
Created March 7, 2022 21:01
Show Gist options
  • Save MessiasLima/edd08036bd5d0be8347be722a8823190 to your computer and use it in GitHub Desktop.
Save MessiasLima/edd08036bd5d0be8347be722a8823190 to your computer and use it in GitHub Desktop.
StoreViewModelAfter.kt
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