View SealedClassObserver.kt
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
val viewModel: AuthViewMode by viewModels() | |
private fun observeState() { | |
viewModel.state.observe( | |
this, | |
{ uiState -> | |
handleUiState(uiState) | |
} | |
) | |
} |
View SealedClassViewModel.kt
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
private val repository = UserRepository() | |
private val _state = MutableLiveData<UIState<Int>?>() | |
val state: LiveData<UIState<Int>?> = _state | |
fun evaluatePassword(password: Long) { | |
_state.value = UIState.Loading(R.string.evaluating_creds) | |
viewmodelScope.launch(Dispatchers.IO) { | |
// This is a long-running operation making network call | |
val isSuccess = repository.authenticatUser(password) |
View UIState.kt
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
/** | |
* Represents the UI state of a long running operation. | |
*/ | |
sealed class UIState<out Int> { | |
/** | |
* Indicates the operation succeeded. | |
*/ | |
object Success : UIState<Nothing>() | |
/** |