Disable login button when form in invalid
This commit is contained in:
parent
f389b0e1f4
commit
942970c740
@ -10,7 +10,7 @@ android {
|
||||
applicationId "com.uam.wmi.findmytutor"
|
||||
minSdkVersion 22
|
||||
targetSdkVersion 27
|
||||
versionCode 72
|
||||
versionCode 73
|
||||
versionName "1.0.0"
|
||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||
vectorDrawables.useSupportLibrary = true
|
||||
@ -76,5 +76,7 @@ dependencies {
|
||||
// spinner loaders library
|
||||
implementation 'com.github.ybq:Android-SpinKit:1.2.0'
|
||||
// rx binding
|
||||
implementation 'com.jakewharton.rxbinding:rxbinding:0.3.0'
|
||||
implementation 'com.jakewharton.rxbinding:rxbinding:0.4.0'
|
||||
implementation 'com.jakewharton.rxbinding:rxbinding-appcompat-v7:0.4.0'
|
||||
implementation 'com.jakewharton.rxbinding:rxbinding-support-v4:0.4.0'
|
||||
}
|
||||
|
@ -10,9 +10,9 @@ import android.os.Bundle;
|
||||
import android.support.design.widget.Snackbar;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.view.inputmethod.EditorInfo;
|
||||
import android.widget.AutoCompleteTextView;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
@ -20,15 +20,14 @@ import com.auth0.android.jwt.Claim;
|
||||
import com.auth0.android.jwt.JWT;
|
||||
import com.jakewharton.retrofit2.adapter.rxjava2.HttpException;
|
||||
import com.jakewharton.rxbinding.widget.RxTextView;
|
||||
import com.jakewharton.rxbinding.widget.TextViewTextChangeEvent;
|
||||
import com.uam.wmi.findmytutor.R;
|
||||
import com.uam.wmi.findmytutor.model.JwtToken;
|
||||
import com.uam.wmi.findmytutor.model.LdapUser;
|
||||
import com.uam.wmi.findmytutor.model.User;
|
||||
import com.uam.wmi.findmytutor.model.ValidateUser;
|
||||
import com.uam.wmi.findmytutor.network.ApiClient;
|
||||
import com.uam.wmi.findmytutor.service.LdapService;
|
||||
import com.uam.wmi.findmytutor.service.UserService;
|
||||
import com.uam.wmi.findmytutor.utils.MapUtils;
|
||||
import com.uam.wmi.findmytutor.utils.PrefUtils;
|
||||
import com.uam.wmi.findmytutor.utils.RestApiHelper;
|
||||
|
||||
@ -37,36 +36,34 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import butterknife.BindView;
|
||||
import io.reactivex.Observable;
|
||||
import butterknife.ButterKnife;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
import io.reactivex.functions.BiFunction;
|
||||
import io.reactivex.functions.Function;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import okhttp3.ResponseBody;
|
||||
|
||||
public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
private AutoCompleteTextView mLoginNameView;
|
||||
@BindView(R.id.password)
|
||||
EditText password;
|
||||
@BindView(R.id.email)
|
||||
EditText email;
|
||||
@BindView(R.id.email_sign_in_button)
|
||||
Button sign_in_button;
|
||||
private EditText mLoginNameView;
|
||||
private EditText mPasswordView;
|
||||
private View mProgressView;
|
||||
private View mLoginFormView;
|
||||
private LdapService ldapService;
|
||||
private UserService userService;
|
||||
private CompositeDisposable disposable = new CompositeDisposable();
|
||||
private Observable<Boolean> observable;
|
||||
|
||||
@BindView(R.id.password)
|
||||
EditText password;
|
||||
|
||||
@BindView(R.id.email)
|
||||
EditText email;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_login);
|
||||
mLoginNameView = findViewById(R.id.email);
|
||||
ButterKnife.bind(this);
|
||||
|
||||
ldapService = ApiClient.getClient(getApplicationContext())
|
||||
.create(LdapService.class);
|
||||
@ -88,59 +85,74 @@ public class LoginActivity extends AppCompatActivity {
|
||||
mLoginFormView = findViewById(R.id.login_form);
|
||||
mProgressView = findViewById(R.id.login_progress);
|
||||
|
||||
rx.Observable<String> nameObservable = RxTextView.textChanges(password).map(CharSequence::toString);
|
||||
rx.Observable<String> passwordObservable = RxTextView.textChanges(password).map(CharSequence::toString);
|
||||
|
||||
observable = Observable.combineLatest(nameObservable, passwordObservable, this::isValidForm);
|
||||
|
||||
validateForm();
|
||||
}
|
||||
|
||||
|
||||
private void validateForm() {
|
||||
sign_in_button.setEnabled(false);
|
||||
mLoginNameView.setError(null);
|
||||
mPasswordView.setError(null);
|
||||
|
||||
rx.Observable<TextViewTextChangeEvent> emailChangeObservable = RxTextView.textChangeEvents(email);
|
||||
rx.Observable<TextViewTextChangeEvent> passwordChangeObservable = RxTextView.textChangeEvents(password);
|
||||
|
||||
rx.Observable.combineLatest(emailChangeObservable, passwordChangeObservable,
|
||||
(emailObservable, passwordObservable) -> {
|
||||
boolean emailCheck = isEmailValid(emailObservable.text());
|
||||
boolean passwordCheck = passwordObservable.text().length() > 0;
|
||||
|
||||
if (!emailCheck) {
|
||||
mLoginNameView.setError(getString(R.string.error_invalid_login_name));
|
||||
}
|
||||
|
||||
return emailCheck && passwordCheck;
|
||||
}).subscribe(aBoolean -> sign_in_button.setEnabled(aBoolean));
|
||||
}
|
||||
|
||||
|
||||
private void attemptLogin() {
|
||||
|
||||
if (cancel) {
|
||||
focusView.requestFocus();
|
||||
} else {
|
||||
showProgress(true);
|
||||
loginProcess(loginName, password);
|
||||
}
|
||||
}
|
||||
|
||||
public Boolean isValidForm(String name, String password) {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private Boolean isFormValid(String loginName, String password){
|
||||
// Reset errors.
|
||||
mLoginNameView.setError(null);
|
||||
mPasswordView.setError(null);
|
||||
|
||||
// Store values at the time of the login attempt.
|
||||
String loginName = mLoginNameView.getText().toString();
|
||||
String password = mPasswordView.getText().toString();
|
||||
|
||||
boolean cancel = false;
|
||||
View focusView = null;
|
||||
|
||||
// Check for a valid email address.
|
||||
if (TextUtils.isEmpty(loginName)) {
|
||||
mLoginNameView.setError(getString(R.string.error_field_required));
|
||||
return false;
|
||||
focusView = mLoginNameView;
|
||||
cancel = true;
|
||||
} else if (!isEmailValid(loginName)) {
|
||||
mLoginNameView.setError(getString(R.string.error_invalid_login_name));
|
||||
return false;
|
||||
focusView = mLoginNameView;
|
||||
cancel = true;
|
||||
}
|
||||
|
||||
// Check for a valid password address.
|
||||
if (TextUtils.isEmpty(password)) {
|
||||
mPasswordView.setError(getString(R.string.error_field_required));
|
||||
return false;
|
||||
focusView = mPasswordView;
|
||||
cancel = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
if (cancel) {
|
||||
focusView.requestFocus();
|
||||
} else {
|
||||
showProgress(true);
|
||||
loginProcess(loginName, password);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isEmailValid(String loginName) {
|
||||
private boolean isEmailValid(CharSequence loginName) {
|
||||
|
||||
Log.e("AAA", String.valueOf(loginName));
|
||||
Pattern pattern = Pattern.compile("\\s");
|
||||
Matcher matcher = pattern.matcher(loginName);
|
||||
return !matcher.find();
|
||||
@ -150,7 +162,6 @@ public class LoginActivity extends AppCompatActivity {
|
||||
private void showProgress(final boolean show) {
|
||||
int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
|
||||
|
||||
|
||||
mLoginFormView.setVisibility(show ? View.GONE : View.VISIBLE);
|
||||
mLoginFormView.animate().setDuration(shortAnimTime).alpha(
|
||||
show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
|
||||
@ -240,5 +251,13 @@ public class LoginActivity extends AppCompatActivity {
|
||||
PrefUtils.storeUserLastName(getApplicationContext(), user.getLastName());
|
||||
PrefUtils.storeUserName(getApplicationContext(), user.getUserName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
mLoginNameView.setError(null);
|
||||
mPasswordView.setError(null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user