From f389b0e1f46e3dc731a01a987ac0ad268f96c610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mieszko=20Wrzeszczy=C5=84ski?= Date: Thu, 17 Jan 2019 22:28:16 +0100 Subject: [PATCH 1/2] initial commit --- .../findmytutor/activity/LoginActivity.java | 55 +++++++++++++------ app/src/main/res/layout/activity_login.xml | 4 +- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java index d49dc60..5b3887a 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java @@ -19,6 +19,7 @@ import android.widget.EditText; 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.uam.wmi.findmytutor.R; import com.uam.wmi.findmytutor.model.JwtToken; import com.uam.wmi.findmytutor.model.LdapUser; @@ -35,8 +36,12 @@ import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; +import butterknife.BindView; +import io.reactivex.Observable; 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; @@ -49,6 +54,13 @@ public class LoginActivity extends AppCompatActivity { private LdapService ldapService; private UserService userService; private CompositeDisposable disposable = new CompositeDisposable(); + private Observable observable; + + @BindView(R.id.password) + EditText password; + + @BindView(R.id.email) + EditText email; @Override protected void onCreate(Bundle savedInstanceState) { @@ -75,46 +87,57 @@ public class LoginActivity extends AppCompatActivity { mLoginFormView = findViewById(R.id.login_form); mProgressView = findViewById(R.id.login_progress); + + rx.Observable nameObservable = RxTextView.textChanges(password).map(CharSequence::toString); + rx.Observable passwordObservable = RxTextView.textChanges(password).map(CharSequence::toString); + + observable = Observable.combineLatest(nameObservable, passwordObservable, this::isValidForm); + } + 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)); - focusView = mLoginNameView; - cancel = true; + return false; } else if (!isEmailValid(loginName)) { mLoginNameView.setError(getString(R.string.error_invalid_login_name)); - focusView = mLoginNameView; - cancel = true; + return false; } // Check for a valid password address. if (TextUtils.isEmpty(password)) { mPasswordView.setError(getString(R.string.error_field_required)); - focusView = mPasswordView; - cancel = true; + return false; } - if (cancel) { - focusView.requestFocus(); - } else { - showProgress(true); - loginProcess(loginName, password); + return true; - } } private boolean isEmailValid(String loginName) { diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index b192e97..c7e60fb 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -45,7 +45,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content"> - - Date: Thu, 17 Jan 2019 23:56:22 +0100 Subject: [PATCH 2/2] Disable login button when form in invalid --- app/build.gradle | 6 +- .../findmytutor/activity/LoginActivity.java | 101 +++++++++++------- 2 files changed, 64 insertions(+), 43 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 53b2d9f..93c3447 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' } diff --git a/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java b/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java index 5b3887a..1823b50 100644 --- a/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java +++ b/app/src/main/java/com/uam/wmi/findmytutor/activity/LoginActivity.java @@ -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 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 nameObservable = RxTextView.textChanges(password).map(CharSequence::toString); - rx.Observable 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 emailChangeObservable = RxTextView.textChangeEvents(email); + rx.Observable 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); + } + }