Skip to content

Instantly share code, notes, and snippets.

@makovkastar
Last active January 9, 2018 13:48
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 makovkastar/f85ecc3f7480e608543a40cb8f7bce95 to your computer and use it in GitHub Desktop.
Save makovkastar/f85ecc3f7480e608543a40cb8f7bce95 to your computer and use it in GitHub Desktop.
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