Skip to content

Instantly share code, notes, and snippets.

@tdcolvin
Last active June 18, 2023 08:02
Show Gist options
  • Save tdcolvin/1063f221921fded5c633a79b8cab5255 to your computer and use it in GitHub Desktop.
Save tdcolvin/1063f221921fded5c633a79b8cab5255 to your computer and use it in GitHub Desktop.
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