|
| 1 | +package com.puddlealley.splash.ui.login |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import androidx.lifecycle.Observer |
| 5 | +import androidx.lifecycle.ViewModelProviders |
| 6 | +import android.os.Bundle |
| 7 | +import androidx.annotation.StringRes |
| 8 | +import androidx.appcompat.app.AppCompatActivity |
| 9 | +import android.text.Editable |
| 10 | +import android.text.TextWatcher |
| 11 | +import android.view.View |
| 12 | +import android.view.inputmethod.EditorInfo |
| 13 | +import android.widget.Button |
| 14 | +import android.widget.EditText |
| 15 | +import android.widget.ProgressBar |
| 16 | +import android.widget.Toast |
| 17 | + |
| 18 | +import com.puddlealley.splash.R |
| 19 | + |
| 20 | +class LoginActivity : AppCompatActivity() { |
| 21 | + |
| 22 | + private lateinit var loginViewModel: LoginViewModel |
| 23 | + |
| 24 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 25 | + super.onCreate(savedInstanceState) |
| 26 | + |
| 27 | + setContentView(R.layout.activity_login) |
| 28 | + |
| 29 | + val username = findViewById<EditText>(R.id.username) |
| 30 | + val password = findViewById<EditText>(R.id.password) |
| 31 | + val login = findViewById<Button>(R.id.login) |
| 32 | + val loading = findViewById<ProgressBar>(R.id.loading) |
| 33 | + |
| 34 | + loginViewModel = ViewModelProviders.of(this, LoginViewModelFactory()) |
| 35 | + .get(LoginViewModel::class.java) |
| 36 | + |
| 37 | + loginViewModel.loginFormState.observe(this@LoginActivity, Observer { |
| 38 | + val loginState = it ?: return@Observer |
| 39 | + |
| 40 | + // disable login button unless both username / password is valid |
| 41 | + login.isEnabled = loginState.isDataValid |
| 42 | + |
| 43 | + if (loginState.usernameError != null) { |
| 44 | + username.error = getString(loginState.usernameError) |
| 45 | + } |
| 46 | + if (loginState.passwordError != null) { |
| 47 | + password.error = getString(loginState.passwordError) |
| 48 | + } |
| 49 | + }) |
| 50 | + |
| 51 | + loginViewModel.loginResult.observe(this@LoginActivity, Observer { |
| 52 | + val loginResult = it ?: return@Observer |
| 53 | + |
| 54 | + loading.visibility = View.GONE |
| 55 | + if (loginResult.error != null) { |
| 56 | + showLoginFailed(loginResult.error) |
| 57 | + } |
| 58 | + if (loginResult.success != null) { |
| 59 | + updateUiWithUser(loginResult.success) |
| 60 | + } |
| 61 | + setResult(Activity.RESULT_OK) |
| 62 | + |
| 63 | + //Complete and destroy login activity once successful |
| 64 | + finish() |
| 65 | + }) |
| 66 | + |
| 67 | + username.afterTextChanged { |
| 68 | + loginViewModel.loginDataChanged( |
| 69 | + username.text.toString(), |
| 70 | + password.text.toString() |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + password.apply { |
| 75 | + afterTextChanged { |
| 76 | + loginViewModel.loginDataChanged( |
| 77 | + username.text.toString(), |
| 78 | + password.text.toString() |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + setOnEditorActionListener { _, actionId, _ -> |
| 83 | + when (actionId) { |
| 84 | + EditorInfo.IME_ACTION_DONE -> |
| 85 | + loginViewModel.login( |
| 86 | + username.text.toString(), |
| 87 | + password.text.toString() |
| 88 | + ) |
| 89 | + } |
| 90 | + false |
| 91 | + } |
| 92 | + |
| 93 | + login.setOnClickListener { |
| 94 | + loading.visibility = View.VISIBLE |
| 95 | + loginViewModel.login(username.text.toString(), password.text.toString()) |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private fun updateUiWithUser(model: LoggedInUserView) { |
| 101 | + val welcome = getString(R.string.welcome) |
| 102 | + val displayName = model.displayName |
| 103 | + // TODO : initiate successful logged in experience |
| 104 | + Toast.makeText( |
| 105 | + applicationContext, |
| 106 | + "$welcome $displayName", |
| 107 | + Toast.LENGTH_LONG |
| 108 | + ).show() |
| 109 | + } |
| 110 | + |
| 111 | + private fun showLoginFailed(@StringRes errorString: Int) { |
| 112 | + Toast.makeText(applicationContext, errorString, Toast.LENGTH_SHORT).show() |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +/** |
| 117 | + * Extension function to simplify setting an afterTextChanged action to EditText components. |
| 118 | + */ |
| 119 | +fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) { |
| 120 | + this.addTextChangedListener(object : TextWatcher { |
| 121 | + override fun afterTextChanged(editable: Editable?) { |
| 122 | + afterTextChanged.invoke(editable.toString()) |
| 123 | + } |
| 124 | + |
| 125 | + override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {} |
| 126 | + |
| 127 | + override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {} |
| 128 | + }) |
| 129 | +} |
0 commit comments