Made login activity more modern

This commit is contained in:
Pierre HUBERT 2018-08-01 10:46:51 +02:00
parent eed86ea221
commit 22c51480a6
6 changed files with 133 additions and 53 deletions

View File

@ -1,8 +1,9 @@
package org.communiquons.android.comunic.client.ui.activities; package org.communiquons.android.comunic.client.ui.activities;
import android.content.Intent; import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ProgressBar; import android.widget.ProgressBar;
@ -11,7 +12,6 @@ import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;
import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.R;
import org.communiquons.android.comunic.client.data.asynctasks.APIRequestTask;
import org.communiquons.android.comunic.client.data.helpers.APIRequestHelper; import org.communiquons.android.comunic.client.data.helpers.APIRequestHelper;
import org.communiquons.android.comunic.client.data.helpers.AccountHelper; import org.communiquons.android.comunic.client.data.helpers.AccountHelper;
import org.communiquons.android.comunic.client.data.models.APIRequest; import org.communiquons.android.comunic.client.data.models.APIRequest;
@ -45,6 +45,8 @@ public class LoginActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login); setContentView(R.layout.activity_login);
assert getSupportActionBar() != null;
getSupportActionBar().hide();
//Create utilities object //Create utilities object
utils = new Utilities(this); utils = new Utilities(this);
@ -84,8 +86,8 @@ public class LoginActivity extends AppCompatActivity {
void submitLogin(){ void submitLogin(){
//Get the fields //Get the fields
final EditText login_mail = (EditText) findViewById(R.id.email_field); EditText login_mail = findViewById(R.id.email_field);
EditText login_password = (EditText) findViewById(R.id.password_field); EditText login_password = findViewById(R.id.password_field);
int stop = 0; int stop = 0;
@ -120,39 +122,78 @@ public class LoginActivity extends AppCompatActivity {
//Perform a request on the API to check user credentials and get login tokens //Perform a request on the API to check user credentials and get login tokens
APIRequest params = new APIRequest(this, "user/connectUSER"); final APIRequest params = new APIRequest(this, "user/connectUSER");
params.addString("userMail", ""+login_mail.getText()); params.addString("userMail", ""+login_mail.getText());
params.addString("userPassword", ""+login_password.getText()); params.addString("userPassword", ""+login_password.getText());
//Create Request //Create Request
new APIRequestTask(){ new Thread(new SubmitLoginRequest(params)).start();
@Override
protected void onPostExecute(APIResponse result) {
//Check for errors
if(result == null) {
//Hide loading wheel
enter_loading_state(false);
//Put the error on the login mail field
show_form_error(getString(R.string.activity_login_err_invalid_credentials));
}
else
//Perform next actions
handle_server_response(result);
}
}.execute(params);
} }
/** /**
* Handle server responses that seems to accept a response * Runnable used to submit login request and retrieve login tokens
*
* @param response The server reponse
*/ */
void handle_server_response(APIResponse response){ private class SubmitLoginRequest implements Runnable {
private APIRequest mRequest;
SubmitLoginRequest(APIRequest request){
mRequest = request;
}
@Override
public void run() {
APIResponse response = null;
try {
response = new APIRequestHelper().exec(mRequest);
} catch (Exception e) {
e.printStackTrace();
}
if(!isFinishing()){
findViewById(R.id.email_field).post(new ServerResponseRunnable(response));
}
}
}
/**
* Runnable used on the UI Thread to pre-process server response
*/
private class ServerResponseRunnable implements Runnable {
private APIResponse mResponse;
ServerResponseRunnable(APIResponse response){
mResponse = response;
}
@Override
public void run() {
handle_server_response(mResponse);
}
}
/**
* Handle server response
*
* @param response The server response
*/
void handle_server_response(@Nullable APIResponse response){
if(response == null){
//Hide loading wheel
enter_loading_state(false);
//Put the error on the login mail field
show_form_error(getString(R.string.activity_login_err_invalid_credentials));
return;
}
JSONObject data = response.getJSONObject(); JSONObject data = response.getJSONObject();
@ -214,12 +255,12 @@ public class LoginActivity extends AppCompatActivity {
/** /**
* Switch between loading state and ready state for the login form * Switch between loading state and ready state for the login form
* *
* @param show_progress Specify wether a progress bar should be shown or not * @param show_progress Specify whether a progress bar should be shown or not
*/ */
void enter_loading_state(boolean show_progress){ void enter_loading_state(boolean show_progress){
//Grab elements //Grab elements
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar); ProgressBar progressBar = findViewById(R.id.progress_bar);
ScrollView login_form = (ScrollView) findViewById(R.id.login_form); ScrollView login_form = findViewById(R.id.login_form);
progressBar.setVisibility(show_progress ? View.VISIBLE : View.GONE); progressBar.setVisibility(show_progress ? View.VISIBLE : View.GONE);
login_form.setVisibility(show_progress ? View.GONE : View.VISIBLE); login_form.setVisibility(show_progress ? View.GONE : View.VISIBLE);
@ -233,7 +274,7 @@ public class LoginActivity extends AppCompatActivity {
void show_form_error(String message){ void show_form_error(String message){
//Retrieve error field //Retrieve error field
TextView v_error = (TextView) findViewById(R.id.login_error_message); TextView v_error = findViewById(R.id.login_error_message);
//Check what to do //Check what to do
boolean display = message.length() > 0; boolean display = message.length() > 0;

View File

@ -0,0 +1,9 @@
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:angle="135"
android:centerColor="#3f51b5"
android:endColor="#283593"
android:startColor="#7986cb"
android:type="linear" />
</shape>

View File

@ -2,8 +2,8 @@
android:shape="rectangle"> android:shape="rectangle">
<gradient <gradient
android:angle="135" android:angle="135"
android:centerColor="#009688" android:centerColor="#3f51b5"
android:endColor="#00695C" android:endColor="#283593"
android:startColor="#4DB6AC" android:startColor="#7986cb"
android:type="linear" /> android:type="linear" />
</shape> </shape>

View File

@ -1,30 +1,48 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp" android:paddingBottom="16dp"
android:layout_marginEnd="16dp" android:paddingEnd="16dp"
android:layout_marginStart="16dp" android:paddingStart="16dp"
android:layout_marginTop="16dp" android:paddingTop="16dp"
android:orientation="vertical" android:orientation="vertical"
android:background="@drawable/activity_login_bg"
tools:context="org.communiquons.android.comunic.client.ui.activities.LoginActivity"> tools:context="org.communiquons.android.comunic.client.ui.activities.LoginActivity">
<!-- Login form -->
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:text="@string/app_name"
android:textAlignment="center"
android:textColor="@android:color/background_light"
android:textSize="24sp" />
<!-- Login loading progress --> <!-- Login loading progress -->
<ProgressBar <ProgressBar
android:id="@+id/progress_bar" android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleLarge" style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal" android:layout_gravity="center_horizontal"
android:visibility="gone"/> android:layout_centerVertical="true"
android:scrollbarAlwaysDrawVerticalTrack="false"
android:visibility="gone" />
<!-- Login form -->
<ScrollView <ScrollView
android:id="@+id/login_form" android:id="@+id/login_form"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent"> android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_centerVertical="true">
<LinearLayout <LinearLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -37,22 +55,24 @@
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:textAlignment="center" android:textAlignment="center"
android:textColor="#FF2222" android:textColor="@color/colorAccent"
tools:text="Error content" android:textStyle="bold"
android:visibility="gone"/> android:visibility="gone"
tools:text="Error content" />
<!-- Email field --> <!-- Email field -->
<android.support.design.widget.TextInputLayout <android.support.design.widget.TextInputLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content"> android:layout_height="wrap_content"
style="@style/LoginInput">
<EditText <EditText
android:id="@+id/email_field" android:id="@+id/email_field"
style="@style/LoginInput"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/activity_login_email_hint" android:hint="@string/activity_login_email_hint"
android:inputType="textEmailAddress" android:inputType="textEmailAddress" />
/>
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
@ -60,7 +80,8 @@
<android.support.design.widget.TextInputLayout <android.support.design.widget.TextInputLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp"> android:layout_marginTop="8dp"
style="@style/LoginInput">
<EditText <EditText
android:id="@+id/password_field" android:id="@+id/password_field"
@ -68,7 +89,7 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:hint="@string/activity_login_password_hint" android:hint="@string/activity_login_password_hint"
android:inputType="textPassword" android:inputType="textPassword"
/> style="@style/LoginInput" />
</android.support.design.widget.TextInputLayout> </android.support.design.widget.TextInputLayout>
@ -85,4 +106,4 @@
</ScrollView> </ScrollView>
</LinearLayout> </RelativeLayout>

View File

@ -2,7 +2,7 @@
<resources> <resources>
<color name="colorPrimary">#3F51B5</color> <color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color> <color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color> <color name="colorAccent">#ff4081</color>
<color name="holo_green_dark">#ff669900</color> <color name="holo_green_dark">#ff669900</color>
<color name="darker_gray">#aaa</color> <color name="darker_gray">#aaa</color>

View File

@ -12,6 +12,15 @@
<item name="colorAccent">@color/colorAccent</item> <item name="colorAccent">@color/colorAccent</item>
</style> </style>
<!-- Login activity -->
<style name="LoginInput">
<item name="android:textColor">@android:color/white</item>
<item name="android:textColorHint">@android:color/white</item>
<item name="android:textColorLink">@android:color/white</item>
<item name="android:textColorHighlight">@android:color/white</item>
</style>
<!-- User page fragment --> <!-- User page fragment -->
<!-- User page header --> <!-- User page header -->
<style name="UserPageHeader"> <style name="UserPageHeader">