From de551ae7167b9195f214780d28c0e9a7d1a46d4e Mon Sep 17 00:00:00 2001 From: Luciano Balmaceda Date: Wed, 18 Oct 2017 11:30:49 -0300 Subject: [PATCH] fix input field validation when focus changes --- .../lock/views/ValidatedInputView.java | 30 +++++++++---------- .../views/ValidatedPasswordInputView.java | 17 +++++------ 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/lib/src/main/java/com/auth0/android/lock/views/ValidatedInputView.java b/lib/src/main/java/com/auth0/android/lock/views/ValidatedInputView.java index c3d1adb4a..b671046ef 100644 --- a/lib/src/main/java/com/auth0/android/lock/views/ValidatedInputView.java +++ b/lib/src/main/java/com/auth0/android/lock/views/ValidatedInputView.java @@ -84,7 +84,7 @@ public class ValidatedInputView extends LinearLayout { private ImageCheckbox showPasswordToggle; private IdentityListener identityListener; private int inputIcon; - private boolean hasValidInput; + private boolean hasValidInput = true; private boolean allowShowPassword = true; private View outline; private ShapeDrawable focusedOutlineBackground; @@ -148,7 +148,7 @@ private void init(AttributeSet attrs) { focusedOutlineBackground = ViewUtils.getRoundedOutlineBackground(getResources(), ContextCompat.getColor(getContext(), R.color.com_auth0_lock_input_field_border_focused)); normalOutlineBackground = ViewUtils.getRoundedOutlineBackground(getResources(), ContextCompat.getColor(getContext(), R.color.com_auth0_lock_input_field_border_normal)); errorOutlineBackground = ViewUtils.getRoundedOutlineBackground(getResources(), ContextCompat.getColor(getContext(), R.color.com_auth0_lock_input_field_border_error)); - updateBorder(true); + updateBorder(); setNextFocusRightId(R.id.com_auth0_lock_show_password_toggle); showPasswordToggle.setOnCheckedChangeListener(new ImageCheckbox.OnCheckedChangeListener() { @@ -173,7 +173,7 @@ public void onCheckedChanged(ImageButton view, boolean isChecked) { input.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { - updateBorder(true); + updateBorder(); } }); } @@ -225,7 +225,7 @@ private void notifyEmailChanged(String emailInput) { private Runnable uiUpdater = new Runnable() { @Override public void run() { - updateBorder(hasValidInput); + updateBorder(); } }; @@ -307,14 +307,12 @@ private void setupInputValidation() { /** * Updates the view knowing if the input is valid or not. - * - * @param isValid if the input is valid or not for this kind of DataType. */ @CallSuper - protected void updateBorder(boolean isValid) { + protected void updateBorder() { boolean isFocused = input.hasFocus() && !input.isInTouchMode(); - ViewUtils.setBackground(outline, isValid ? (isFocused ? focusedOutlineBackground : normalOutlineBackground) : errorOutlineBackground); - errorDescription.setVisibility(isValid ? GONE : VISIBLE); + ViewUtils.setBackground(outline, hasValidInput ? (isFocused ? focusedOutlineBackground : normalOutlineBackground) : errorOutlineBackground); + errorDescription.setVisibility(hasValidInput ? GONE : VISIBLE); requestLayout(); } @@ -333,7 +331,8 @@ private void createBackground() { */ public void setDataType(@DataType int type) { dataType = type; - updateBorder(true); + hasValidInput = true; + updateBorder(); setupInputValidation(); } @@ -348,15 +347,15 @@ protected int getDataType() { } /** - * Validates the input data and updates the icon. DataType must be set. + * Validates the input data and updates the border. DataType must be set. * Empty fields are considered valid. * * @return whether the data is valid or not. */ public boolean validate() { - boolean isValid = validate(true); - updateBorder(isValid); - return isValid; + hasValidInput = validate(true); + updateBorder(); + return hasValidInput; } /** @@ -481,7 +480,8 @@ public void setIcon(@DrawableRes int icon) { public void clearInput() { Log.v(TAG, "Input cleared and validation errors removed"); input.setText(""); - updateBorder(true); + hasValidInput = true; + updateBorder(); showPasswordToggle.setChecked(false); } diff --git a/lib/src/main/java/com/auth0/android/lock/views/ValidatedPasswordInputView.java b/lib/src/main/java/com/auth0/android/lock/views/ValidatedPasswordInputView.java index 7dc8d184d..dc1f4f457 100644 --- a/lib/src/main/java/com/auth0/android/lock/views/ValidatedPasswordInputView.java +++ b/lib/src/main/java/com/auth0/android/lock/views/ValidatedPasswordInputView.java @@ -10,6 +10,7 @@ public class ValidatedPasswordInputView extends ValidatedInputView { private static final String TAG = ValidatedPasswordInputView.class.getSimpleName(); private PasswordStrengthView strengthView; + private boolean hasValidInput = true; public ValidatedPasswordInputView(Context context) { super(context); @@ -35,20 +36,16 @@ public void init() { protected boolean validate(boolean validateEmptyFields) { String value = getText(); //Run strength validation to update ui - final boolean valid = strengthView.isValid(value); - if (!validateEmptyFields && value.isEmpty()) { - return true; - } - - Log.v(TAG, "Field validation results: Is valid? " + valid); - return valid; + hasValidInput = strengthView.isValid(value) || !validateEmptyFields && value.isEmpty(); + Log.v(TAG, "Field validation results: Is valid? " + hasValidInput); + return hasValidInput; } @Override - protected void updateBorder(boolean isValid) { - super.updateBorder(isValid); + protected void updateBorder() { + super.updateBorder(); if (strengthView != null && strengthView.isEnabled()) { - strengthView.setVisibility(!isValid || !getText().isEmpty() ? VISIBLE : GONE); + strengthView.setVisibility(!hasValidInput || !getText().isEmpty() ? VISIBLE : GONE); } }