diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/SurveyHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/SurveyHelper.java index 21a24ce..fd22faf 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/SurveyHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/SurveyHelper.java @@ -40,6 +40,26 @@ public class SurveyHelper extends BaseHelper { } } + /** + * Respond to a survey + * + * @param postID The ID of the target post + * @param choiceID The ID of the target choice + * @return TRUE in case of success / FALSE else + */ + public boolean sendResponse(int postID, int choiceID){ + APIRequest request = new APIRequest(getContext(), "surveys/send_response"); + request.addInt("postID", postID); + request.addInt("choiceID", choiceID); + + try { + return new APIRequestHelper().exec(request).getResponse_code() == 200; + } catch (Exception e) { + e.printStackTrace(); + return false; + } + } + /** * Turn an API JSON object into a Survey * diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/RespondSurveyTask.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/RespondSurveyTask.java new file mode 100644 index 0000000..2c353b4 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/asynctasks/RespondSurveyTask.java @@ -0,0 +1,21 @@ +package org.communiquons.android.comunic.client.ui.asynctasks; + +import android.content.Context; + +import org.communiquons.android.comunic.client.data.helpers.SurveyHelper; + +/** + * Respond to survey task + * + * @author Pierre HUBERT + */ +public class RespondSurveyTask extends SafeAsyncTask { + public RespondSurveyTask(Context context) { + super(context); + } + + @Override + protected Boolean doInBackground(Integer... integers) { + return new SurveyHelper(getContext()).sendResponse(integers[0], integers[1]); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AbstractPostsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AbstractPostsListFragment.java index 2dfad5c..95a8e62 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AbstractPostsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/AbstractPostsListFragment.java @@ -35,12 +35,12 @@ import org.communiquons.android.comunic.client.data.helpers.PostsHelper; import org.communiquons.android.comunic.client.data.models.Comment; import org.communiquons.android.comunic.client.data.models.Post; import org.communiquons.android.comunic.client.data.models.Survey; -import org.communiquons.android.comunic.client.data.models.SurveyChoice; import org.communiquons.android.comunic.client.data.models.UserInfo; import org.communiquons.android.comunic.client.data.utils.AccountUtils; import org.communiquons.android.comunic.client.data.utils.StringsUtils; import org.communiquons.android.comunic.client.ui.adapters.PostsAdapter; import org.communiquons.android.comunic.client.ui.asynctasks.CancelSurveyResponseTask; +import org.communiquons.android.comunic.client.ui.asynctasks.RespondSurveyTask; import org.communiquons.android.comunic.client.ui.asynctasks.SafeAsyncTask; import org.communiquons.android.comunic.client.ui.listeners.OnScrollChangeDetectListener; import org.communiquons.android.comunic.client.ui.listeners.onPostUpdateListener; @@ -929,7 +929,7 @@ public abstract class AbstractPostsListFragment extends AbstractFragment cancelSurveyResponseTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { @Override public void OnPostExecute(Boolean success) { - delete_callback(postID, success); + delete_survey_response_callback(postID, success); } }); cancelSurveyResponseTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, survey.getPostID()); @@ -942,7 +942,7 @@ public abstract class AbstractPostsListFragment extends AbstractFragment * @param postID The ID of the related post * @param success TRUE in case of success / FALSE else */ - private void delete_callback(int postID, boolean success){ + private void delete_survey_response_callback(int postID, boolean success){ //Check for failure if(!success){ @@ -959,7 +959,39 @@ public abstract class AbstractPostsListFragment extends AbstractFragment } @Override - public void onRespondToSurvey(Survey survey, int choiceID) { + public void onRespondToSurvey(Survey survey, final int choiceID) { + final int postID = survey.getPostID(); + + RespondSurveyTask respondSurveyTask = new RespondSurveyTask(getActivity()); + respondSurveyTask.setOnPostExecuteListener(new SafeAsyncTask.OnPostExecuteListener() { + @Override + public void OnPostExecute(Boolean result) { + respond_survey_callback(result, postID, choiceID); + } + }); + respondSurveyTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, survey.getPostID(), choiceID); + getTasksManager().addTask(respondSurveyTask); + } + + /** + * Respond to a survey callback + * + * @param result The result of the operation + * @param postID The ID of the related choice + * @param choiceID The ID of choice + */ + private void respond_survey_callback(boolean result, int postID, int choiceID){ + + if(!result){ + Toast.makeText(getActivity(), R.string.err_respond_survey, Toast.LENGTH_SHORT).show(); + mPostsAdapter.notifyDataSetChanged(); + return; + } + + Survey survey = getPostsList().find(postID).getSurvey(); + survey.setUser_choice(choiceID); + survey.getChoices().find(choiceID).addOneResponse(); + mPostsAdapter.notifyDataSetChanged(); } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/SurveyView.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/SurveyView.java index 5d274d1..45890b1 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/SurveyView.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/SurveyView.java @@ -7,13 +7,14 @@ import android.support.constraint.ConstraintLayout; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.Spinner; import android.widget.TextView; -import android.widget.Toast; import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.data.arrays.SurveyChoicesList; import org.communiquons.android.comunic.client.data.models.Survey; import org.communiquons.android.comunic.client.data.models.SurveyChoice; import org.communiquons.android.comunic.client.ui.listeners.OnSurveyUpdateListener; @@ -26,6 +27,11 @@ public class SurveyView extends BaseFrameLayoutView implements View.OnClickListe */ private Survey mSurvey; + /** + * Adapter for select dropdown + */ + private SurveyChoicesAdapter mSpinnerAdapter; + /** * Update listener */ @@ -69,6 +75,9 @@ public class SurveyView extends BaseFrameLayoutView implements View.OnClickListe mCancelButton.setOnClickListener(this); mResponseButton.setOnClickListener(this); + + mSpinnerAdapter = new SurveyChoicesAdapter(getContext()); + mResponsesSpinner.setAdapter(mSpinnerAdapter); } /** @@ -136,7 +145,7 @@ public class SurveyView extends BaseFrameLayoutView implements View.OnClickListe * Render respond form */ private void renderRespondForm(){ - + mSpinnerAdapter.setList(mSurvey.getChoices()); } @Override @@ -152,12 +161,65 @@ public class SurveyView extends BaseFrameLayoutView implements View.OnClickListe //Respond to a survey if(v.equals(mResponseButton)){ + + int choiceID = mSpinnerAdapter.getChoice(mResponsesSpinner.getSelectedItemPosition()) + .getChoiceID(); + if(mOnSurveyUpdateListener != null) - mOnSurveyUpdateListener.onRespondToSurvey(mSurvey, -1); - //TODO : implement + mOnSurveyUpdateListener.onRespondToSurvey(mSurvey, choiceID); + } } + /** + * Adapter for the spinner + * + * @author Pierre HUBERT + */ + private static class SurveyChoicesAdapter extends ArrayAdapter { + + private SurveyChoicesList mList; + + SurveyChoicesAdapter(@NonNull Context context) { + super(context, android.R.layout.simple_spinner_item); + setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); + } + + /** + * Set a new list of choices + * + * @param list The list of choices + */ + void setList(@NonNull SurveyChoicesList list){ + mList = list; + notifyDataSetChanged(); + } + + @Override + public int getCount() { + + if(mList == null) + return 0; + + return mList.size(); + } + + @Nullable + @Override + public CharSequence getItem(int position) { + return mList.get(position).getName(); + } + + /** + * Get a choice from the list at a specified position + * + * @param pos The position of the item to select + * @return The SurveyChoice + */ + SurveyChoice getChoice(int pos) { + return mList.get(pos); + } + } } diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index 99c010f..c4dfdba 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -293,4 +293,8 @@ Les groupes ne sont pas encore supportés sur l\'application Android… (%1$d) %2$s Votre choix : %1$s + Annuler + Répondre + Une erreur a survenue lors de l\'annulation de votre réponse au sondage ! + Une erreur a survenue lors de l\'envoi de votre réponse au sondage ! \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 34d0682..677a1d5 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -295,4 +295,5 @@ Cancel Respond Could not cancel your response to the survey! + Could not send response to the server!