Skip to content

Commit

Permalink
Feat: Add error message for all the EditText in LoginActivity
Browse files Browse the repository at this point in the history
  • Loading branch information
letelete committed Oct 25, 2018
1 parent e6ef485 commit eef18fd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public class LoginPresenter extends BasePresenter<LoginView> {
private final DataManager dataManager;
private PreferencesHelper preferencesHelper;
private CompositeDisposable compositeDisposable;
private Resources resources;
private boolean fieldNotValidated;

/**
* Initialises the LoginPresenter by automatically injecting an instance of
Expand All @@ -50,6 +52,7 @@ public LoginPresenter(DataManager dataManager, @ApplicationContext Context conte
this.dataManager = dataManager;
preferencesHelper = this.dataManager.getPreferencesHelper();
compositeDisposable = new CompositeDisposable();
resources = context.getResources();
}

@Override
Expand Down Expand Up @@ -175,36 +178,45 @@ public void onNext(Page<Client> clientPage) {


private boolean isCredentialsValid(final String username, final String password) {
fieldNotValidated = false;
validateUsername(username);
validatePassword(password);

final Resources resources = context.getResources();
return !fieldNotValidated;
}

private void validateUsername(final String username) {
final String correctUsername = username.replaceFirst("\\s++$", "").trim();

if (username == null || username.matches("\\s*") || username.isEmpty()) {
getMvpView().showUsernameError(context.getString(R.string.error_validation_blank,
context.getString(R.string.username)));
return false;
fieldNotValidated = true;
} else if (username.length() < 5) {
getMvpView().showUsernameError(context.getString(R.string.error_validation_minimum_chars
, resources.getString(R.string.username), resources.getInteger(R.integer.
username_minimum_length)));
return false;
fieldNotValidated = true;
} else if (correctUsername.contains(" ")) {
getMvpView().showUsernameError(context.getString(
R.string.error_validation_cannot_contain_spaces,
resources.getString(R.string.username),
context.getString(R.string.not_contain_username)));
return false;
} else if (password == null || password.isEmpty()) {
fieldNotValidated = true;
}
}

private void validatePassword(final String password) {
if (password == null || password.isEmpty()) {
getMvpView().showPasswordError(context.getString(R.string.error_validation_blank,
context.getString(R.string.password)));
return false;
fieldNotValidated = true;
} else if (password.length() < 6) {
getMvpView().showPasswordError(context.getString(R.string.error_validation_minimum_chars
, resources.getString(R.string.password), resources.getInteger(R.integer.
password_minimum_length)));
return false;
fieldNotValidated = true;
}

return true;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ buildscript {

allprojects {
repositories {
jcenter()
google()
jcenter()
maven { url "https://www.jitpack.io" }
maven { url 'https://maven.fabric.io/public' }
}
Expand Down

0 comments on commit eef18fd

Please sign in to comment.