Last active
December 27, 2021 08:37
-
-
Save cp-radhika-s/9b2b8c299051edd4f7d47caafc6439dd to your computer and use it in GitHub Desktop.
This file contains 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
@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