This commit is contained in:
Aran Zaiger 2016-07-03 12:52:10 +03:00
commit 8c711b6cf5
3 changed files with 43 additions and 38 deletions

View file

@ -5,7 +5,7 @@
<GradleProjectSettings> <GradleProjectSettings>
<option name="distributionType" value="LOCAL" /> <option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="C:\Program Files\Android\Android Studio\gradle\gradle-2.10" /> <option name="gradleHome" value="$APPLICATION_HOME_DIR$/gradle/gradle-2.10" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />

View file

@ -177,7 +177,7 @@ public class LoginActivity extends AppCompatActivity{ // implements LoaderCallba
Log.d(TAG, "user: " + username); Log.d(TAG, "user: " + username);
Log.d(TAG, "pwd: " + password); Log.d(TAG, "pwd: " + password);
if(TextUtils.isEmpty(username) || !isPasswordValid(username) || TextUtils.isEmpty(password) || !isPasswordValid(password)) { if(TextUtils.isEmpty(username) || !isUsernameValid(username) || TextUtils.isEmpty(password) || !isPasswordValid(password)) {
return false; return false;
} else{ } else{
return true; return true;
@ -187,6 +187,7 @@ public class LoginActivity extends AppCompatActivity{ // implements LoaderCallba
if (mAuthTask != null) { if (mAuthTask != null) {
return; return;
} }
showProgress(true);
// Reset errors. // Reset errors.
// mEmailView.setError(null); // mEmailView.setError(null);
@ -198,26 +199,12 @@ public class LoginActivity extends AppCompatActivity{ // implements LoaderCallba
// String email = mEmailView.getText().toString(); // String email = mEmailView.getText().toString();
if (isParamsValid(mUsernameView.getText().toString(), mPasswordView.getText().toString())) { if (isParamsValid(mUsernameView.getText().toString(), mPasswordView.getText().toString())) {
String username = mUsernameView.getText().toString(); String username = mUsernameView.getText().toString();
String password = mPasswordView.getText().toString(); String password = mPasswordView.getText().toString();
boolean cancel = false; boolean cancel = false;
View focusView = null; View focusView = null;
// Check for a valid password, if the user entered one.
if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
mPasswordView.setError(getString(R.string.error_invalid_password));
focusView = mPasswordView;
cancel = true;
}
if (cancel) {
// There was an error; don't attempt login and focus the first
// form field with an error.
focusView.requestFocus();
} else {
// Show a progress spinner, and kick off a background task to
// perform the user login attempt.
showProgress(true);
// mAuthTask = new UserLoginTask(username, password); // TODO: RETRIEVE ? // mAuthTask = new UserLoginTask(username, password); // TODO: RETRIEVE ?
// mAuthTask.execute((Void) null); // TODO: RETRIEVE ? // mAuthTask.execute((Void) null); // TODO: RETRIEVE ?
@ -231,10 +218,25 @@ public class LoginActivity extends AppCompatActivity{ // implements LoaderCallba
} }
VolleyUtilSingleton.getInstance(LoginActivity.this).post(BASE_URL + LOGIN_PATH, tempJson, onLoginSuccess, onLoginError); VolleyUtilSingleton.getInstance(LoginActivity.this).post(BASE_URL + LOGIN_PATH, tempJson, onLoginSuccess, onLoginError);
}
// Check for a valid password, if the user entered one.
// if (!TextUtils.isEmpty(password) && !isPasswordValid(password)) {
// mPasswordView.setError(getString(R.string.error_invalid_password));
// focusView = mPasswordView;
// cancel = true;
// }
// if (cancel) {
// // There was an error; don't attempt login and focus the first
// // form field with an error.
// focusView.requestFocus();
// } else {
// // Show a progress spinner, and kick off a background task to
// // perform the user login attempt.
//
// }
} else { } else {
showProgress(false); showProgress(false);
Log.d(TAG, "Invalid params"); Log.d(TAG, "Invalid params - make sure username exist & password is 4 characters or more");
} }
// Check for a valid username // Check for a valid username
@ -253,13 +255,17 @@ public class LoginActivity extends AppCompatActivity{ // implements LoaderCallba
@Override @Override
public void onResponse(JSONObject response) { public void onResponse(JSONObject response) {
try { try {
if(response.get("user") != null) { // if(response.get("user") != null) {
if(!response.isNull("user")) {
Log.e(TAG, "onLoginSuccess => user exist"); // TODO: REMOVE console Log.e(TAG, "onLoginSuccess => user exist"); // TODO: REMOVE console
Intent personalSpaceActivity = new Intent(LoginActivity.this, PersonalSpaceActivity.class); Intent personalSpaceActivity = new Intent(LoginActivity.this, PersonalSpaceActivity.class);
Bundle b = new Bundle(); Bundle loginUserBundle = new Bundle();
b.putString("user_id", response.getJSONObject("user").getString("id")); loginUserBundle.putString("user_id", response.getJSONObject("user").getString("id"));
personalSpaceActivity.putExtras(b); personalSpaceActivity.putExtras(loginUserBundle);
startActivity(personalSpaceActivity); startActivity(personalSpaceActivity);
} else {
showProgress(false);
Log.d(TAG, "No such user, " + response.get("user"));
} }
}catch (Exception e) { }catch (Exception e) {
Log.e(TAG, "onLoginSuccess:" + e.getMessage()); Log.e(TAG, "onLoginSuccess:" + e.getMessage());
@ -270,19 +276,18 @@ public class LoginActivity extends AppCompatActivity{ // implements LoaderCallba
Response.ErrorListener onLoginError = new Response.ErrorListener() { Response.ErrorListener onLoginError = new Response.ErrorListener() {
@Override @Override
public void onErrorResponse(VolleyError error) { public void onErrorResponse(VolleyError error) {
showProgress(false);
Log.d(TAG, "onLoginError: msg: " + error.getMessage()); Log.d(TAG, "onLoginError: msg: " + error.getMessage());
} }
}; };
private boolean isEmailValid(String email) { private boolean isUsernameValid(String username) {
//TODO: Replace this with your own logic return username.length() > 0;
return email.contains("@") && email.contains(".");
} }
private boolean isPasswordValid(String password) { private boolean isPasswordValid(String password) {
//TODO: Replace this with your own logic return password.length() > 3;
return password.length() > 4;
} }
/** /**

View file

@ -4,7 +4,7 @@
<!-- Strings related to login --> <!-- Strings related to login -->
<string name="prompt_email">Email</string> <string name="prompt_email">Email</string>
<string name="prompt_username">Username</string> <string name="prompt_username">Username</string>
<string name="prompt_password">Password (optional)</string> <string name="prompt_password">Password</string>
<string name="action_login">Login</string> <string name="action_login">Login</string>
<string name="action_register">Register</string> <string name="action_register">Register</string>
<string name="action_sign_in_short">Sign in</string> <string name="action_sign_in_short">Sign in</string>