Skip to content

Instantly share code, notes, and snippets.

@ArunYogeshwaran
Last active October 12, 2021 14:59
Show Gist options
  • Save ArunYogeshwaran/0d1cfac5d5ea8cb037f1c81cb01c20bc to your computer and use it in GitHub Desktop.
Save ArunYogeshwaran/0d1cfac5d5ea8cb037f1c81cb01c20bc to your computer and use it in GitHub Desktop.
Example of a View Class observing Sealed Class LiveData from ViewModel
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