Last active
January 9, 2018 13:48
-
-
Save makovkastar/f85ecc3f7480e608543a40cb8f7bce95 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
class LoginViewModel @Inject constructor(private val loginGateway: LoginGateway) : ViewModel() { | |
val emailField = ObservableField<String>() | |
val passwordField = ObservableField<String>() | |
val isSignInButtonEnabled = ObservableBoolean() | |
val isProgressBarVisible = ObservableBoolean() | |
val navigateToForgotPasswordPage = SingleLiveEvent<Unit>() | |
val navigateToNextScreen = SingleLiveEvent<Unit>() | |
val showErrorToast = SingleLiveEvent<String>() | |
init { | |
isSignInButtonEnabled.set(false) | |
isProgressBarVisible.set(false) | |
} | |
fun onLoginButtonClicked() { | |
isProgressBarVisible.set(true) | |
loginGateway.login(emailField.get(), passwordField.get(), LoginCallbacks()) | |
} | |
fun onForgotPasswordLabelClicked() { | |
navigateToForgotPasswordPage.call() | |
} | |
fun emailTextChangedListener(): TextWatcher { | |
return object : TextWatcherAdapter() { | |
override fun afterTextChanged(s: Editable?) { | |
validateInputFields() | |
} | |
} | |
} | |
fun passwordTextChangedListener(): TextWatcher { | |
return object : TextWatcherAdapter() { | |
override fun afterTextChanged(s: Editable?) { | |
validateInputFields() | |
} | |
} | |
} | |
private fun validateInputFields() { | |
isSignInButtonEnabled.set(!emailField.get().isNullOrBlank() | |
&& !passwordField.get().isNullOrBlank()) | |
} | |
inner class LoginCallbacks : LoginGateway.LoginCallbacks { | |
override fun onLoginSuccess() { | |
navigateToNextScreen.call() | |
} | |
override fun onLoginError() { | |
isProgressBarVisible.set(false) | |
showErrorToast.call() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment