Skip to content

Instantly share code, notes, and snippets.

@cp-radhika-s
Last active December 27, 2021 08:37
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 cp-radhika-s/9b2b8c299051edd4f7d47caafc6439dd to your computer and use it in GitHub Desktop.
Save cp-radhika-s/9b2b8c299051edd4f7d47caafc6439dd to your computer and use it in GitHub Desktop.
@HiltViewModel
class MainViewModel
@Inject constructor(
private val userServices: UserServices,
private val appDispatchers: AppDispatchers,
) : ViewModel() {
val state = MutableStateFlow<State>(State.START)
init {
loadUser()
}
private fun loadUser() = viewModelScope.launch {
state.value = State.LOADING
try {
val users = withContext(appDispatchers.IO) {
userServices.getUsers()
}
state.value = State.SUCCESS(users)
} catch (e: Exception) {
state.value = State.FAILURE(e.localizedMessage)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment