Last active
June 18, 2023 08:02
-
-
Save tdcolvin/1063f221921fded5c633a79b8cab5255 to your computer and use it in GitHub Desktop.
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.tdcolvin.planetspotters.ui.planetslist | |
... | |
@HiltViewModel | |
class PlanetsListViewModel @Inject constructor( | |
planetsRepository: PlanetsRepository | |
): ViewModel() { | |
private val planets = planetsRepository.getPlanetsFlow() | |
val uiState = planets.map { planets -> | |
when (planets) { | |
is WorkResult.Error -> PlanetsListUiState(isError = true) | |
is WorkResult.Loading -> PlanetsListUiState(isLoading = true) | |
is WorkResult.Success -> PlanetsListUiState(planets = planets.data) | |
} | |
}.stateIn( | |
scope = viewModelScope, | |
started = SharingStarted.WhileSubscribed(5000), | |
initialValue = PlanetsListUiState(isLoading = true) | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment