Last active
June 13, 2021 05:03
-
-
Save hongbeomi/737ed714df6fb4eb5603fbfd10ccdbb5 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
@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