Skip to content

Commit

Permalink
Feat #896: Improve UX in registration fragment by adding new features
Browse files Browse the repository at this point in the history
Add error display for every input field. All of them can appear at once now.
Add feature which find the first invalid input and force request on it with automatically appearing keyboard to help user correct it instantly.
Fix validation logic.
Remove redundant checks.
Grouped up similiar functionalities.
  • Loading branch information
letelete committed Oct 27, 2018
1 parent aa20e9f commit a75fd66
Showing 1 changed file with 161 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package org.mifos.mobilebanking.ui.fragments;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputLayout;
import android.text.TextUtils;
import android.util.Patterns;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
Expand Down Expand Up @@ -62,6 +66,8 @@ public class RegistrationFragment extends BaseFragment implements RegistrationVi
RegistrationPresenter presenter;

private View rootView;
private boolean areAllInputsCorrect;
private EditText firstInvalidInput;

public static RegistrationFragment newInstance() {
RegistrationFragment fragment = new RegistrationFragment();
Expand Down Expand Up @@ -96,12 +102,6 @@ public void registerClicked() {
payload.setFirstName(etFirstName.getText().toString());
payload.setLastName(etLastName.getText().toString());
payload.setMobileNumber(etPhoneNumber.getText().toString());
if (!etPassword.getText().toString().equals(etConfirmPassword.getText().toString())) {
Toaster.show(rootView, getString(R.string.error_password_not_match));
return;
} else {
payload.setPassword(etPassword.getText().toString());
}
payload.setPassword(etPassword.getText().toString());
payload.setUsername(etUsername.getText().toString().replace(" ", ""));

Expand All @@ -115,61 +115,165 @@ public void registerClicked() {
}

private boolean areFieldsValidated() {
areAllInputsCorrect = true;
firstInvalidInput = null;

if (TextUtils.isEmpty(getTrimmedInputText(etAccountNumber))) {
showInputError(etAccountNumber,
getString(R.string.error_validation_blank, getString(R.string.
account_number)));
} else {
hideInputError(etAccountNumber);
}

if (etAccountNumber.getText().toString().trim().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
account_number)));
return false;
} else if (etUsername.getText().toString().trim().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
username)));
return false;
} else if (etUsername.getText().toString().trim().length() < 5) {
Toaster.show(rootView, getString(R.string.error_validation_minimum_chars,
getString(R.string.username),
getResources().getInteger(R.integer.username_minimum_length)));
return false;
} else if (etUsername.getText().toString().trim().contains(" ")) {
Toaster.show(rootView, getString(R.string.error_validation_cannot_contain_spaces,
getString(R.string.username), getString(R.string.not_contain_username)));
return false;
} else if (etFirstName.getText().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
first_name)));
return false;
} else if (etLastName.getText().toString().trim().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
last_name)));
return false;
} else if (etEmail.getText().toString().trim().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
email)));
return false;
} else if (etPassword.getText().toString().trim().length() == 0) {
Toaster.show(rootView, getString(R.string.error_validation_blank, getString(R.string.
password)));
return false;
} else if (etPassword.getText().toString().trim().length()
< etPassword.getText().toString().length()) {
Toaster.show(rootView,
getString(R.string.error_validation_cannot_contain_leading_or_trailing_spaces,
getString(R.string.password)));
return false;
} else if (etUsername.getText().toString().trim().length() < 6) {
Toaster.show(rootView, getString(R.string.error_username_greater_than_six));
return false;
} else if (!Patterns.EMAIL_ADDRESS.matcher( etEmail.getText().toString().trim())
if (TextUtils.isEmpty(getTrimmedInputText(etUsername))) {
showInputError(etUsername,
getString(R.string.error_validation_blank, getString(R.string.
username)));
} else if (getTrimmedInputText(etUsername).length() < 6) {
showInputError(etUsername,
getString(R.string.error_username_greater_than_six));
} else if (getTrimmedInputText(etUsername).contains(" ")) {
showInputError(etUsername,
getString(R.string.error_validation_cannot_contain_spaces,
getString(R.string.username),
getString(R.string.not_contain_username)));
} else {
hideInputError(etUsername);
}

if (TextUtils.isEmpty(getTrimmedInputText(etFirstName))) {
showInputError(etFirstName,
getString(R.string.error_validation_blank, getString(R.string.
first_name)));
} else {
hideInputError(etFirstName);
}

if (TextUtils.isEmpty(getTrimmedInputText(etLastName))) {
showInputError(etLastName,
getString(R.string.error_validation_blank, getString(R.string.
last_name)));
} else {
hideInputError(etLastName);
}

if (TextUtils.isEmpty(getTrimmedInputText(etPhoneNumber))) {
showInputError(etPhoneNumber,
getString(R.string.error_validation_blank, getString(R.string.
phone_number)));
} else {
hideInputError(etPhoneNumber);
}

if (TextUtils.isEmpty(getTrimmedInputText(etEmail))) {
showInputError(etEmail,
getString(R.string.error_validation_blank, getString(R.string.
email)));
} else if (!Patterns.EMAIL_ADDRESS.matcher(getTrimmedInputText(etEmail))
.matches()) {
Toaster.show(rootView, getString(R.string.error_invalid_email));
return false;
} else if (etPassword.getText().toString().trim().length() < 6) {
Toaster.show(rootView, getString(R.string.error_validation_minimum_chars,
getString(R.string.password), getResources().
getInteger(R.integer.password_minimum_length)));
return false;
showInputError(etEmail,
getString(R.string.error_invalid_email));
} else {
hideInputError(etEmail);
}

if (getTrimmedInputText(etPassword).length() < etPassword.getText().toString().length()) {
showInputError(etPassword,
getString(R.string.error_validation_cannot_contain_leading_or_trailing_spaces,
getString(R.string.password)));
} else if (TextUtils.isEmpty(getTrimmedInputText(etPassword))) {
showInputError(etPassword,
getString(R.string.error_validation_blank, getString(R.string.
password)));
} else if (getTrimmedInputText(etPassword).length() < 6) {
showInputError(etPassword,
getString(R.string.error_validation_minimum_chars,
getString(R.string.password), getResources().
getInteger(R.integer.password_minimum_length)));
} else {
hideInputError(etPassword);
}

if (!etPassword.getText().toString()
.equals(etConfirmPassword.getText().toString())) {
showInputError(etConfirmPassword,
getString(R.string.error_validation_cannot_contain_leading_or_trailing_spaces,
getString(R.string.password)));
showInputError(etConfirmPassword,
getString(R.string.error_password_not_match));
} else if (getTrimmedInputText(etConfirmPassword).length()
< etConfirmPassword.getText().toString().length()) {
showInputError(etConfirmPassword,
getString(R.string.error_validation_cannot_contain_leading_or_trailing_spaces,
getString(R.string.password)));
} else if (TextUtils.isEmpty(getTrimmedInputText(etConfirmPassword))) {
showInputError(etConfirmPassword,
getString(R.string.error_validation_blank, getString(R.string.
password)));
} else if (getTrimmedInputText(etConfirmPassword).length() < 6) {
showInputError(etConfirmPassword,
getString(R.string.error_validation_minimum_chars,
getString(R.string.password), getResources().
getInteger(R.integer.password_minimum_length)));
} else {
hideInputError(etConfirmPassword);
}

return true;
setFocusOnFirstInvalidInputIfExists();
return areAllInputsCorrect;
}

private String getTrimmedInputText(EditText input) {
return input.getText().toString().trim();
}

private void showInputError(EditText input, String errorMsg) {
setErrorDependencies(input);
TextInputLayout textInputLayout = getInputLayoutByGivenEditText(input);
textInputLayout.setError(errorMsg);
}

private void setErrorDependencies(EditText input) {
areAllInputsCorrect = false;
setFirstInvalidInputIfNotExists(input);
}

private void setFirstInvalidInputIfNotExists(EditText input) {
if (firstInvalidInput == null) {
firstInvalidInput = input;
}
}

private void hideInputError(EditText input) {
TextInputLayout textInputLayout = getInputLayoutByGivenEditText(input);
textInputLayout.setError(null);
}

private TextInputLayout getInputLayoutByGivenEditText(EditText input) {
return (TextInputLayout) input.getParent().getParent();
}

private void setFocusOnFirstInvalidInputIfExists() {
if (firstInvalidInput != null) {
clearFocusIfFirstInvalidInputHasItToAvoidBug();
firstInvalidInput.requestFocus();
forceKeyboardToAppearOnFirstInvalidInput();
}
}

private void clearFocusIfFirstInvalidInputHasItToAvoidBug() {
if (firstInvalidInput.hasFocus()) {
firstInvalidInput.clearFocus();
}
}

private void forceKeyboardToAppearOnFirstInvalidInput() {
InputMethodManager inputMethodManager =
(InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
if (inputMethodManager != null) {
inputMethodManager.showSoftInput(firstInvalidInput, InputMethodManager.SHOW_IMPLICIT);
}
}

@Override
Expand Down

0 comments on commit a75fd66

Please sign in to comment.