-
-
Save novumlogic-devs/592058d03d3b6b7c24feea5264ec9803 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 | |
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