Skip to content

Instantly share code, notes, and snippets.

@novumlogic-devs
Created July 3, 2024 07:12
Show Gist options
  • Save novumlogic-devs/592058d03d3b6b7c24feea5264ec9803 to your computer and use it in GitHub Desktop.
Save novumlogic-devs/592058d03d3b6b7c24feea5264ec9803 to your computer and use it in GitHub Desktop.
@HiltViewModel
internal class UserVerificationViewModel @Inject constructor(
private val useCaseVerificationCodeRequest: VerificationCodeRequest,
) : ViewModel() {
val navigator = Navigator()
val viewState = UserVerificationViewState()
fun requestForVerificationCode(){
viewModelScope.launch {
val draft = VerificationCodeRequestDraft(viewState.phoneNumber)
val result = useCaseVerificationCodeRequest.invoke(draft).data
result?.let {
checkApiResult(result)
}
}
}
fun checkApiResult(result: Result<VerificationCodeResponse>?) {
viewState.loaderVisibility = Visibility.GONE
when (result) {
is Result.NetworkError -> showSnackbarNetworkError()
is Result.ApiError -> showSnackbarForApiError(result.message)
is Result.Success -> navigateToNextScreen(result.data)
}
}
fun navigateToNextScreen(result: VerificationCodeResponse) {
navigator.navigate(actionNavigateToNextScreen(result))
}
private fun showSnackbarForApiError(message: String) {
viewState.snackbar = SnackbarEvent("", ExceptionWrapper(ErrorMessage(message), Throwable()))
}
private fun showSnackbarNetworkError() {
viewState.snackbar = SnackbarEvent("Network error occurred. Please ensure network connectiviy")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment