Skip to content

Instantly share code, notes, and snippets.

Avatar
🏠
Working from home

Arun ArunYogeshwaran

🏠
Working from home
  • VMware
  • Bangalore
View GitHub Profile
@ArunYogeshwaran
ArunYogeshwaran / SealedClassObserver.kt
Last active Oct 12, 2021
Example of a View Class observing Sealed Class LiveData from ViewModel
View SealedClassObserver.kt
val viewModel: AuthViewMode by viewModels()
private fun observeState() {
viewModel.state.observe(
this,
{ uiState ->
handleUiState(uiState)
}
)
}
@ArunYogeshwaran
ArunYogeshwaran / SealedClassViewModel.kt
Last active Oct 12, 2021
Sealed class usage within LiveData in the ViewModel class
View SealedClassViewModel.kt
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)
@ArunYogeshwaran
ArunYogeshwaran / UIState.kt
Last active Oct 12, 2021
Sealed class representing all the possible UI states
View UIState.kt
/**
* Represents the UI state of a long running operation.
*/
sealed class UIState<out Int> {
/**
* Indicates the operation succeeded.
*/
object Success : UIState<Nothing>()
/**