Email format validation

This commit is contained in:
Benoit Marty 2019-11-21 16:12:55 +01:00
parent 1a603742d0
commit 051f77087e
3 changed files with 39 additions and 21 deletions

View file

@ -17,6 +17,7 @@
package im.vector.riotx.core.extensions package im.vector.riotx.core.extensions
import android.os.Bundle import android.os.Bundle
import android.util.Patterns
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
fun Boolean.toOnOff() = if (this) "ON" else "OFF" fun Boolean.toOnOff() = if (this) "ON" else "OFF"
@ -25,3 +26,8 @@ fun Boolean.toOnOff() = if (this) "ON" else "OFF"
* Apply argument to a Fragment * Apply argument to a Fragment
*/ */
fun <T : Fragment> T.withArgs(block: Bundle.() -> Unit) = apply { arguments = Bundle().apply(block) } fun <T : Fragment> T.withArgs(block: Bundle.() -> Unit) = apply { arguments = Bundle().apply(block) }
/**
* Check if a CharSequence is an email
*/
fun CharSequence.isEmail() = Patterns.EMAIL_ADDRESS.matcher(this).matches()

View file

@ -33,6 +33,7 @@ import im.vector.matrix.android.api.failure.Failure
import im.vector.matrix.android.api.failure.MatrixError import im.vector.matrix.android.api.failure.MatrixError
import im.vector.riotx.R import im.vector.riotx.R
import im.vector.riotx.core.error.ErrorFormatter import im.vector.riotx.core.error.ErrorFormatter
import im.vector.riotx.core.extensions.isEmail
import im.vector.riotx.core.extensions.setTextOrHide import im.vector.riotx.core.extensions.setTextOrHide
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
import kotlinx.android.synthetic.main.fragment_login_generic_text_input_form.* import kotlinx.android.synthetic.main.fragment_login_generic_text_input_form.*
@ -74,7 +75,6 @@ class LoginGenericTextInputFormFragment @Inject constructor(private val errorFor
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
when (params.mode) { when (params.mode) {
TextInputFormFragmentMode.SetEmail -> loginGenericTextInputFormTextInput.setAutofillHints(HintConstants.AUTOFILL_HINT_EMAIL_ADDRESS) TextInputFormFragmentMode.SetEmail -> loginGenericTextInputFormTextInput.setAutofillHints(HintConstants.AUTOFILL_HINT_EMAIL_ADDRESS)
// TODO Phone number without country code?
TextInputFormFragmentMode.SetMsisdn -> loginGenericTextInputFormTextInput.setAutofillHints(HintConstants.AUTOFILL_HINT_PHONE_NUMBER) TextInputFormFragmentMode.SetMsisdn -> loginGenericTextInputFormTextInput.setAutofillHints(HintConstants.AUTOFILL_HINT_PHONE_NUMBER)
TextInputFormFragmentMode.ConfirmMsisdn -> loginGenericTextInputFormTextInput.setAutofillHints(HintConstants.AUTOFILL_HINT_SMS_OTP) TextInputFormFragmentMode.ConfirmMsisdn -> loginGenericTextInputFormTextInput.setAutofillHints(HintConstants.AUTOFILL_HINT_SMS_OTP)
} }
@ -169,16 +169,29 @@ class LoginGenericTextInputFormFragment @Inject constructor(private val errorFor
} }
private fun setupSubmitButton() { private fun setupSubmitButton() {
if (params.mandatory) { loginGenericTextInputFormSubmit.isEnabled = false
loginGenericTextInputFormSubmit.isEnabled = false loginGenericTextInputFormTextInput.textChanges()
loginGenericTextInputFormTextInput.textChanges() .subscribe {
.subscribe { loginGenericTextInputFormSubmit.isEnabled = isInputValid(it)
// TODO Better check for email format, etc? }
loginGenericTextInputFormSubmit.isEnabled = it.isNotBlank() .disposeOnDestroyView()
} }
.disposeOnDestroyView()
private fun isInputValid(input: CharSequence): Boolean {
return if (input.isEmpty() && !params.mandatory) {
true
} else { } else {
loginGenericTextInputFormSubmit.isEnabled = true when (params.mode) {
TextInputFormFragmentMode.SetEmail -> {
input.isEmail()
}
TextInputFormFragmentMode.SetMsisdn -> {
input.isNotBlank()
}
TextInputFormFragmentMode.ConfirmMsisdn -> {
input.isNotBlank()
}
}
} }
} }

View file

@ -19,6 +19,7 @@ package im.vector.riotx.features.login
import android.os.Bundle import android.os.Bundle
import android.view.View import android.view.View
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import butterknife.OnClick
import com.airbnb.mvrx.Fail import com.airbnb.mvrx.Fail
import com.airbnb.mvrx.Loading import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.Success import com.airbnb.mvrx.Success
@ -26,6 +27,7 @@ import com.airbnb.mvrx.withState
import com.jakewharton.rxbinding3.widget.textChanges import com.jakewharton.rxbinding3.widget.textChanges
import im.vector.riotx.R import im.vector.riotx.R
import im.vector.riotx.core.error.ErrorFormatter import im.vector.riotx.core.error.ErrorFormatter
import im.vector.riotx.core.extensions.isEmail
import im.vector.riotx.core.extensions.showPassword import im.vector.riotx.core.extensions.showPassword
import io.reactivex.Observable import io.reactivex.Observable
import io.reactivex.functions.BiFunction import io.reactivex.functions.BiFunction
@ -62,10 +64,10 @@ class LoginResetPasswordFragment @Inject constructor(
private fun setupSubmitButton() { private fun setupSubmitButton() {
Observable Observable
.combineLatest( .combineLatest(
resetPasswordEmail.textChanges().map { it.trim().isNotEmpty() }, resetPasswordEmail.textChanges().map { it.isEmail() },
passwordField.textChanges().map { it.trim().isNotEmpty() }, passwordField.textChanges().map { it.isNotEmpty() },
BiFunction<Boolean, Boolean, Boolean> { isEmailNotEmpty, isPasswordNotEmpty -> BiFunction<Boolean, Boolean, Boolean> { isEmail, isPasswordNotEmpty ->
isEmailNotEmpty && isPasswordNotEmpty isEmail && isPasswordNotEmpty
} }
) )
.subscribeBy { .subscribeBy {
@ -74,15 +76,12 @@ class LoginResetPasswordFragment @Inject constructor(
resetPasswordSubmit.isEnabled = it resetPasswordSubmit.isEnabled = it
} }
.disposeOnDestroyView() .disposeOnDestroyView()
resetPasswordSubmit.setOnClickListener { submit() }
} }
private fun submit() { @OnClick(R.id.resetPasswordSubmit)
val email = resetPasswordEmail.text?.trim().toString() fun submit() {
val password = passwordField.text?.trim().toString() val email = resetPasswordEmail.text.toString()
val password = passwordField.text.toString()
// TODO Add static check?
loginViewModel.handle(LoginAction.ResetPassword(email, password)) loginViewModel.handle(LoginAction.ResetPassword(email, password))
} }