Skip to content

Instantly share code, notes, and snippets.

@ArunYogeshwaran
Last active October 12, 2021 14:52
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 ArunYogeshwaran/1958ad5b88c482d4976b0cdfe77f76b8 to your computer and use it in GitHub Desktop.
Save ArunYogeshwaran/1958ad5b88c482d4976b0cdfe77f76b8 to your computer and use it in GitHub Desktop.
Sealed class usage within LiveData in the ViewModel class
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)
if (isSuccess) {
_state.postValue(UIState.Success)
} else {
_state.postValue(UIState.Error(R.string.auth_failed))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment