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
import android.os.Bundle
import android.util.Patterns
import androidx.fragment.app.Fragment
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
*/
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.riotx.R
import im.vector.riotx.core.error.ErrorFormatter
import im.vector.riotx.core.extensions.isEmail
import im.vector.riotx.core.extensions.setTextOrHide
import kotlinx.android.parcel.Parcelize
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) {
when (params.mode) {
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.ConfirmMsisdn -> loginGenericTextInputFormTextInput.setAutofillHints(HintConstants.AUTOFILL_HINT_SMS_OTP)
}
@ -169,16 +169,29 @@ class LoginGenericTextInputFormFragment @Inject constructor(private val errorFor
}
private fun setupSubmitButton() {
if (params.mandatory) {
loginGenericTextInputFormSubmit.isEnabled = false
loginGenericTextInputFormTextInput.textChanges()
.subscribe {
// TODO Better check for email format, etc?
loginGenericTextInputFormSubmit.isEnabled = it.isNotBlank()
}
.disposeOnDestroyView()
loginGenericTextInputFormSubmit.isEnabled = false
loginGenericTextInputFormTextInput.textChanges()
.subscribe {
loginGenericTextInputFormSubmit.isEnabled = isInputValid(it)
}
.disposeOnDestroyView()
}
private fun isInputValid(input: CharSequence): Boolean {
return if (input.isEmpty() && !params.mandatory) {
true
} 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.view.View
import androidx.appcompat.app.AlertDialog
import butterknife.OnClick
import com.airbnb.mvrx.Fail
import com.airbnb.mvrx.Loading
import com.airbnb.mvrx.Success
@ -26,6 +27,7 @@ import com.airbnb.mvrx.withState
import com.jakewharton.rxbinding3.widget.textChanges
import im.vector.riotx.R
import im.vector.riotx.core.error.ErrorFormatter
import im.vector.riotx.core.extensions.isEmail
import im.vector.riotx.core.extensions.showPassword
import io.reactivex.Observable
import io.reactivex.functions.BiFunction
@ -62,10 +64,10 @@ class LoginResetPasswordFragment @Inject constructor(
private fun setupSubmitButton() {
Observable
.combineLatest(
resetPasswordEmail.textChanges().map { it.trim().isNotEmpty() },
passwordField.textChanges().map { it.trim().isNotEmpty() },
BiFunction<Boolean, Boolean, Boolean> { isEmailNotEmpty, isPasswordNotEmpty ->
isEmailNotEmpty && isPasswordNotEmpty
resetPasswordEmail.textChanges().map { it.isEmail() },
passwordField.textChanges().map { it.isNotEmpty() },
BiFunction<Boolean, Boolean, Boolean> { isEmail, isPasswordNotEmpty ->
isEmail && isPasswordNotEmpty
}
)
.subscribeBy {
@ -74,15 +76,12 @@ class LoginResetPasswordFragment @Inject constructor(
resetPasswordSubmit.isEnabled = it
}
.disposeOnDestroyView()
resetPasswordSubmit.setOnClickListener { submit() }
}
private fun submit() {
val email = resetPasswordEmail.text?.trim().toString()
val password = passwordField.text?.trim().toString()
// TODO Add static check?
@OnClick(R.id.resetPasswordSubmit)
fun submit() {
val email = resetPasswordEmail.text.toString()
val password = passwordField.text.toString()
loginViewModel.handle(LoginAction.ResetPassword(email, password))
}