-
-
Save skydoves/1c82da0a713171fbfe8d935837512e8a to your computer and use it in GitHub Desktop.
DetailsViewModel
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
// https://github.com/skydoves/pokedex-compose | |
@HiltViewModel | |
class DetailsViewModel @Inject constructor( | |
detailsRepository: DetailsRepository, | |
savedStateHandle: SavedStateHandle, | |
) : ViewModel() { | |
val pokemon = savedStateHandle.getStateFlow<Pokemon?>("pokemon", null) | |
val pokemonInfo: StateFlow<PokemonInfo?> = | |
pokemon.filterNotNull().flatMapLatest { pokemon -> | |
detailsRepository.fetchPokemonInfo( | |
name = pokemon.nameField.replaceFirstChar { it.lowercase() }, | |
onComplete = { uiState.tryEmit(key, DetailsUiState.Idle) }, | |
onError = { uiState.tryEmit(key, DetailsUiState.Error(it)) }, | |
) | |
}.stateIn( | |
scope = viewModelScope, | |
started = SharingStarted.WhileSubscribed(5_000), | |
initialValue = null, | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment