Add IME action to the password field

This commit is contained in:
Benoit Marty 2020-04-22 14:46:49 +02:00
parent c854491248
commit a4ba8c152d
3 changed files with 37 additions and 10 deletions

View file

@ -19,6 +19,7 @@ package im.vector.riotx.features.login
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.inputmethod.EditorInfo
import androidx.autofill.HintConstants
import androidx.core.view.isVisible
import butterknife.OnClick
@ -40,7 +41,8 @@ import kotlinx.android.synthetic.main.fragment_login.*
import javax.inject.Inject
/**
* In this screen, in signin mode:
* In this screen:
* In signin mode:
* - the user is asked for login (or email) and password to sign in to a homeserver.
* - He also can reset his password
* In signup mode:
@ -49,6 +51,7 @@ import javax.inject.Inject
class LoginFragment @Inject constructor() : AbstractLoginFragment() {
private var passwordShown = false
private var isSignupMode = false
override fun getLayoutResId() = R.layout.fragment_login
@ -57,6 +60,14 @@ class LoginFragment @Inject constructor() : AbstractLoginFragment() {
setupSubmitButton()
setupPasswordReveal()
passwordField.setOnEditorActionListener { _, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
submit()
return@setOnEditorActionListener true
}
return@setOnEditorActionListener false
}
}
private fun setupAutoFill(state: LoginViewState) {
@ -82,8 +93,21 @@ class LoginFragment @Inject constructor() : AbstractLoginFragment() {
val login = loginField.text.toString()
val password = passwordField.text.toString()
// This can be called by the IME action, so deal with empty cases
var error = 0
if (login.isEmpty()) {
loginFieldTil.error = getString(if (isSignupMode) R.string.error_empty_field_choose_user_name else R.string.error_empty_field_enter_user_name)
error++
}
if (password.isEmpty()) {
passwordFieldTil.error = getString(if (isSignupMode) R.string.error_empty_field_choose_password else R.string.error_empty_field_your_password)
error++
}
if (error == 0) {
loginViewModel.handle(LoginAction.LoginOrRegister(login, password, getString(R.string.login_mobile_device_riotx)))
}
}
private fun cleanupUi() {
loginSubmit.hideKeyboard()
@ -190,6 +214,8 @@ class LoginFragment @Inject constructor() : AbstractLoginFragment() {
}
override fun updateWithState(state: LoginViewState) {
isSignupMode = state.signMode == SignMode.SignUp
setupUi(state)
setupAutoFill(state)
setupButtons(state)

View file

@ -82,6 +82,7 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:imeOptions="actionDone"
android:inputType="textPassword"
android:maxLines="1"
android:paddingEnd="48dp"
@ -104,19 +105,18 @@
</FrameLayout>
<RelativeLayout
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="22dp"
android:orientation="horizontal">
android:layout_marginTop="22dp">
<com.google.android.material.button.MaterialButton
android:id="@+id/forgetPasswordButton"
style="@style/Style.Vector.Login.Button.Text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:text="@string/auth_forgot_password" />
android:text="@string/auth_forgot_password"
app:layout_constraintStart_toStartOf="parent" />
<com.google.android.material.button.MaterialButton
android:id="@+id/loginSubmit"
@ -124,12 +124,12 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_gravity="end"
android:text="@string/auth_login"
app:layout_constraintEnd_toEndOf="parent"
tools:enabled="false"
tools:ignore="RelativeOverlap" />
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>

View file

@ -11,7 +11,8 @@
<!-- BEGIN Strings added by Benoit -->
<string name="error_empty_field_choose_user_name">Please choose a username.</string>
<string name="error_empty_field_choose_password">Please choose a password.</string>
<!-- END Strings added by Benoit -->