Skip to content

Instantly share code, notes, and snippets.

@hongbeomi
Last active June 13, 2021 05:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hongbeomi/737ed714df6fb4eb5603fbfd10ccdbb5 to your computer and use it in GitHub Desktop.
Save hongbeomi/737ed714df6fb4eb5603fbfd10ccdbb5 to your computer and use it in GitHub Desktop.
@HiltViewModel
class DetailViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val repository: Repository
) : ViewModel() {
private val houseName = savedStateHandle.get<HouseType>(KEY_HOUSE)?.name
private val _isLoadingFlow = MutableStateFlow(false)
val isLoadingFlow: StateFlow<Boolean> = _isLoadingFlow.asStateFlow()
private val _selectedCharacter = MutableSharedFlow<Character?>()
val selectedCharacter: SharedFlow<Character?> = _selectedCharacter.asSharedFlow()
val characterListFlow: StateFlow<List<Character>> = flow {
houseName?.let {
_isLoadingFlow.emit(true)
emit(repository.getCharacters(it))
}
_isLoadingFlow.emit(false)
}.stateIn(
scope = viewModelScope,
started = WhileSubscribed(5000),
initialValue = emptyList()
)
fun showCharacterDialogEvent(character: Character) = viewModelScope.launch {
_selectedCharacter.emit(character)
}
fun hideCharacterDialogEvent() = viewModelScope.launch {
_selectedCharacter.emit(null)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment