Skip to content

Instantly share code, notes, and snippets.

@MessiasLima
Last active March 7, 2022 20:30
Show Gist options
  • Save MessiasLima/8956d82aef89651aa682be73a695c384 to your computer and use it in GitHub Desktop.
Save MessiasLima/8956d82aef89651aa682be73a695c384 to your computer and use it in GitHub Desktop.
SplashViewModelBefore
import androidx.lifecycle.ViewModel
import com.messiaslima.promogamer.domain.Store
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.onStart
class StoreViewModel(private val storeOrchestrator: StoreOrchestrator) : ViewModel() {
val uiState = storeOrchestrator.getStores()
.map<List<Store>, UiState> { UiState.Success(it) }
.catch { throwable -> emit(UiState.Error(throwable)) }
.onStart { emit(UiState.Loading) }
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