diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/enums/CreateAccountResult.java b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/CreateAccountResult.java new file mode 100644 index 0000000..f44a159 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/enums/CreateAccountResult.java @@ -0,0 +1,26 @@ +package org.communiquons.android.comunic.client.data.enums; + +/** + * Create account results + * + * @author Pierre HUBERT + */ +public enum CreateAccountResult { + + /** + * The account was successfully created + */ + SUCCESS, + + /** + * Trying to login with an existing email address + */ + ERROR_EXISTING_EMAIL, + + /** + * Unspecified error occurred + */ + ERROR + + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountHelper.java index f9352e4..9accce1 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/AccountHelper.java @@ -3,6 +3,7 @@ package org.communiquons.android.comunic.client.data.helpers; import android.content.Context; import android.util.Log; +import org.communiquons.android.comunic.client.data.enums.CreateAccountResult; import org.communiquons.android.comunic.client.data.models.APIRequest; import org.communiquons.android.comunic.client.data.models.APIResponse; import org.communiquons.android.comunic.client.data.models.NewAccount; @@ -188,9 +189,10 @@ public class AccountHelper { * @param newAccount Information about the new account to create * @return TRUE for a success / FALSE else */ - public boolean createAccount(NewAccount newAccount) { + public CreateAccountResult createAccount(NewAccount newAccount) { APIRequest request = new APIRequest(mContext, "account/create"); + request.setTryContinueOnError(true); request.addString("firstName", newAccount.getFirstName()); request.addString("lastName", newAccount.getLastName()); request.addString("emailAddress", newAccount.getEmail()); @@ -199,11 +201,22 @@ public class AccountHelper { //Perform the request try { APIResponse response = new APIRequestHelper().exec(request); - return response.getResponse_code() == 200; + + switch (response.getResponse_code()) { + case 200: + return CreateAccountResult.SUCCESS; + + case 409: + return CreateAccountResult.ERROR_EXISTING_EMAIL; + + default: + return CreateAccountResult.ERROR; + + } } catch (Exception e) { e.printStackTrace(); - return false; + return CreateAccountResult.ERROR; } } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CreateAccountActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CreateAccountActivity.java index 697de2f..7f51cec 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CreateAccountActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/CreateAccountActivity.java @@ -12,11 +12,13 @@ import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ProgressBar; +import android.widget.ScrollView; import android.widget.TextView; import org.communiquons.android.comunic.client.BuildConfig; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.asynctasks.SafeAsyncTask; +import org.communiquons.android.comunic.client.data.enums.CreateAccountResult; import org.communiquons.android.comunic.client.data.helpers.AccountHelper; import org.communiquons.android.comunic.client.data.models.NewAccount; import org.communiquons.android.comunic.client.data.utils.Utilities; @@ -29,13 +31,13 @@ import static android.os.AsyncTask.Status.FINISHED; * @author Pierre HUBERT */ public class CreateAccountActivity extends AppCompatActivity - implements SafeAsyncTask.OnPostExecuteListener { + implements SafeAsyncTask.OnPostExecuteListener { /** * Input fields */ private ProgressBar mProgress; - private View mForm; + private ScrollView mForm; private TextView mError; private EditText mFirstName; private EditText mLastName; @@ -238,12 +240,24 @@ public class CreateAccountActivity extends AppCompatActivity } @Override - public void OnPostExecute(Boolean result) { + public void OnPostExecute(CreateAccountResult result) { setLoading(false); //Check for errors - if(!result){ - setError(getString(R.string.err_while_creating_account)); + if(result != CreateAccountResult.SUCCESS){ + + //Find the right error to display + int message = R.string.err_while_creating_account; + switch (result){ + + //Existing email address + case ERROR_EXISTING_EMAIL: + message = R.string.err_create_account_existing_email; + break; + } + + mForm.scrollTo(1, 1); + setError(getString(message)); return; } @@ -261,14 +275,14 @@ public class CreateAccountActivity extends AppCompatActivity /** * Class used to create an account across activities */ - private static class CreateAccountTask extends SafeAsyncTask { + private static class CreateAccountTask extends SafeAsyncTask { CreateAccountTask(Context context) { super(context); } @Override - protected Boolean doInBackground(NewAccount... newAccounts) { + protected CreateAccountResult doInBackground(NewAccount... newAccounts) { return new AccountHelper(getContext()).createAccount(newAccounts[0]); } diff --git a/app/src/main/res/layout/activity_create_account.xml b/app/src/main/res/layout/activity_create_account.xml index a3ec391..f4b31f8 100644 --- a/app/src/main/res/layout/activity_create_account.xml +++ b/app/src/main/res/layout/activity_create_account.xml @@ -2,7 +2,8 @@ + android:layout_height="match_parent" + android:background="@drawable/activity_login_bg"> + android:layout_height="match_parent" > Please specify a valid password! The password and its confirmation are not the same! Please accept the terms of use of Comunic ! - An error occurred while trying to create your account! Maybe an account with the same email address already exists! + An error occurred while trying to create your account! Congratulations Your account has been created. You can now sign into your new account to use all the features of Comunic. Sign in Too many failed login requests. Please try again later… + This email address is already associated with an account!