Last active
October 12, 2021 14:59
-
-
Save ArunYogeshwaran/0d1cfac5d5ea8cb037f1c81cb01c20bc to your computer and use it in GitHub Desktop.
Example of a View Class observing Sealed Class LiveData from ViewModel
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) | |
} | |
) | |
} | |
private fun handleUiState(uiState: UIState<Int>?) { | |
when (uiState) { | |
is UIState.Loading -> { | |
showProgress(uiState.loadingMessageId) | |
} | |
is UIState.Success -> { | |
dismissProgress() | |
finish() | |
} | |
is UIState.Error -> { | |
dismissProgress() | |
showSnackBar(uiState.errorMessageId) | |
} | |
// the `else` clause is not required because all the cases are covered | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment