Can respond to a survey

This commit is contained in:
Pierre HUBERT 2018-09-07 09:41:39 +02:00
parent 1daffd4af5
commit 7fd4602091
6 changed files with 148 additions and 8 deletions

View File

@ -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
*

View File

@ -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<Integer, Void, Boolean> {
public RespondSurveyTask(Context context) {
super(context);
}
@Override
protected Boolean doInBackground(Integer... integers) {
return new SurveyHelper(getContext()).sendResponse(integers[0], integers[1]);
}
}

View File

@ -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<Boolean>() {
@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<Boolean>() {
@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();
}
}

View File

@ -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<CharSequence> {
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);
}
}
}

View File

@ -293,4 +293,8 @@
<string name="err_groups_not_supported">Les groupes ne sont pas encore supportés sur l\'application Android…</string>
<string name="survey_choice">(%1$d) %2$s</string>
<string name="survey_your_choice">Votre choix : %1$s</string>
<string name="action_cancel_response_survey">Annuler</string>
<string name="action_send_survey_response">Répondre</string>
<string name="err_cancel_response">Une erreur a survenue lors de l\'annulation de votre réponse au sondage !</string>
<string name="err_respond_survey">Une erreur a survenue lors de l\'envoi de votre réponse au sondage !</string>
</resources>

View File

@ -295,4 +295,5 @@
<string name="action_cancel_response_survey">Cancel</string>
<string name="action_send_survey_response">Respond</string>
<string name="err_cancel_response">Could not cancel your response to the survey!</string>
<string name="err_respond_survey">Could not send response to the server!</string>
</resources>