diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index f8aece82..a41b86e0 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -21,7 +21,6 @@ - - - diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java index b3dd9c78..73760677 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/LoginActivity.java @@ -1,8 +1,10 @@ package gr.thmmy.mthmmy.activities; +import android.app.ProgressDialog; import android.content.Intent; import android.os.AsyncTask; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; @@ -21,9 +23,11 @@ import static gr.thmmy.mthmmy.utils.Thmmy.WRONG_USER; import static gr.thmmy.mthmmy.utils.Thmmy.authenticate; public class LoginActivity extends BaseActivity { - + private static final String TAG = "LoginActivity"; private EditText inputUsername; private EditText inputPassword; + Button btnLogin; + Button btnGuest; @Override public void onCreate(Bundle savedInstanceState) { @@ -32,66 +36,76 @@ public class LoginActivity extends BaseActivity { inputUsername = (EditText) findViewById(R.id.username); inputPassword = (EditText) findViewById(R.id.password); - Button btnLogin = (Button) findViewById(R.id.btnLogin); - Button btnGuest = (Button) findViewById(R.id.btnContinueAsGuest); + btnLogin = (Button) findViewById(R.id.btnLogin); + btnGuest = (Button) findViewById(R.id.btnContinueAsGuest); // Login button Click Event btnLogin.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { - String username = inputUsername.getText().toString().trim(); - String password = inputPassword.getText().toString().trim(); + Log.d(TAG, "Login"); // Check for empty data in the form - if (!username.isEmpty() && !password.isEmpty()) { - // login user - try { - switch(new loginAsync().execute(username,password).get()){ - case WRONG_USER: - Toast.makeText(getApplicationContext(), - "Wrong username!", Toast.LENGTH_LONG) - .show(); - break; - case WRONG_PASSWORD: - Toast.makeText(getApplicationContext(), - "Wrong password!", Toast.LENGTH_LONG) - .show(); - break; - case FAILED: - Toast.makeText(getApplicationContext(), - "Check your connection!", Toast.LENGTH_LONG) - .show(); - break; - case CERTIFICATE_ERROR: - Toast.makeText(getApplicationContext(), - "Certificate error!", Toast.LENGTH_LONG) - .show(); - break; - case OTHER_ERROR: - Toast.makeText(getApplicationContext(), - "Check your connection!", Toast.LENGTH_LONG) - .show(); - break; - case OK: - Intent intent = new Intent(LoginActivity.this, MainActivity.class); - startActivity(intent); - break; - } - if(new loginAsync().execute(username,password).get() == OK){ + if (!validate()) { + onLoginFailed(); + return; + } + + btnLogin.setEnabled(false); + + final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this, + R.style.AppTheme_Dark_Dialog); + progressDialog.setIndeterminate(true); + progressDialog.setMessage("Authenticating..."); + progressDialog.show(); + + String username = inputUsername.getText().toString().trim(); + String password = inputPassword.getText().toString().trim(); + // login user + try { + switch (new loginAsync().execute(username, password).get()) { + case WRONG_USER: + Toast.makeText(getApplicationContext(), + "Wrong username!", Toast.LENGTH_LONG) + .show(); + break; + case WRONG_PASSWORD: + Toast.makeText(getApplicationContext(), + "Wrong password!", Toast.LENGTH_LONG) + .show(); + break; + case FAILED: + Toast.makeText(getApplicationContext(), + "Check your connection!", Toast.LENGTH_LONG) + .show(); + break; + case CERTIFICATE_ERROR: + Toast.makeText(getApplicationContext(), + "Certificate error!", Toast.LENGTH_LONG) + .show(); + break; + case OTHER_ERROR: + Toast.makeText(getApplicationContext(), + "Check your connection!", Toast.LENGTH_LONG) + .show(); + break; + case OK: + progressDialog.dismiss(); + Toast.makeText(getApplicationContext(), + "Login successful!", Toast.LENGTH_LONG) + .show(); Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); - } - } catch (InterruptedException | ExecutionException e) { - e.printStackTrace(); + finish(); + overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out); + break; } - } else { - // Prompt user to enter credentials - Toast.makeText(getApplicationContext(), - "Please enter the credentials!", Toast.LENGTH_LONG) - .show(); + } catch (InterruptedException | ExecutionException e) { + e.printStackTrace(); } + progressDialog.dismiss(); + btnLogin.setEnabled(true); } - }); // Link to Register Screen @@ -101,7 +115,18 @@ public class LoginActivity extends BaseActivity { //TO-DO } }); + } + @Override + public void onBackPressed() { + // Disable going back to the MainActivity + moveTaskToBack(true); + } + + public void onLoginFailed() { + Toast.makeText(getBaseContext(), "Login failed", Toast.LENGTH_LONG).show(); + + btnLogin.setEnabled(true); } private class loginAsync extends AsyncTask { @@ -123,5 +148,28 @@ public class LoginActivity extends BaseActivity { protected void onProgressUpdate(Void... values) { } } + + public boolean validate() { + boolean valid = true; + + String email = inputUsername.getText().toString(); + String password = inputPassword.getText().toString(); + + if (email.isEmpty()) { + inputUsername.setError("Enter a valid username"); + valid = false; + } else { + inputUsername.setError(null); + } + + if (password.isEmpty()) { + inputPassword.setError("Enter a valid password"); + valid = false; + } else { + inputPassword.setError(null); + } + + return valid; + } } diff --git a/app/src/main/res/anim/push_left_in.xml b/app/src/main/res/anim/push_left_in.xml new file mode 100644 index 00000000..6ac5724d --- /dev/null +++ b/app/src/main/res/anim/push_left_in.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/res/anim/push_left_out.xml b/app/src/main/res/anim/push_left_out.xml new file mode 100644 index 00000000..923cad88 --- /dev/null +++ b/app/src/main/res/anim/push_left_out.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/drawable/thmmy_3d_wshadow.png b/app/src/main/res/drawable/thmmy_3d_wshadow.png new file mode 100644 index 00000000..2697af29 Binary files /dev/null and b/app/src/main/res/drawable/thmmy_3d_wshadow.png differ diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index c38631c2..bdbe54a0 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -1,69 +1,76 @@ - + + android:paddingLeft="24dp" + android:paddingRight="24dp"> - + + + + android:layout_marginBottom="8dp" + android:layout_marginTop="8dp"> - + + + + + android:layout_marginBottom="8dp" + android:layout_marginTop="8dp"> - + + -