Last active
October 12, 2021 14:52
-
-
Save ArunYogeshwaran/1958ad5b88c482d4976b0cdfe77f76b8 to your computer and use it in GitHub Desktop.
Sealed class usage within LiveData in the ViewModel class
This file contains hidden or 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) | |
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