Skip to content

Commit

Permalink
Handle submitting answers for external survey
Browse files Browse the repository at this point in the history
Signed-off-by: Amr Hossam <[email protected]>
  • Loading branch information
amrhossamdev committed Aug 18, 2024
1 parent 71a6825 commit 8f1a166
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.onebusaway.android.ui.survey;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
Expand Down Expand Up @@ -56,6 +55,10 @@ public class SurveyManager {
private final Boolean isVisibleOnStops;
// Cur stop if the survey visible on the stops
private ObaStop currentStop;
// Stores external survey URL
private String externalSurveyUrl = null;
// true if we have a question as an external survey
private Integer externalSurveyResult = 0;


// TODO CHANGE STATIC API URL TO SUPPORT DIFFERENT REGIONS
Expand All @@ -77,24 +80,25 @@ public void requestSurveyData() {
private void updateSurveyData() {
List<StudyResponse.Surveys.Questions> questionsList = mStudyResponse.getSurveys().get(curSurveyIndex).getQuestions();

int externalSurveyResult = SurveyUtils.checkExternalSurvey(questionsList);
externalSurveyResult = SurveyUtils.checkExternalSurvey(questionsList);
Log.d("SurveyState", "External survey result: " + externalSurveyResult);

switch (externalSurveyResult) {
case SurveyUtils.EXTERNAL_SURVEY_WITHOUT_HERO_QUESTION:
SurveyViewUtils.showSharedInfoDetailsTextView(context,surveyView,questionsList.get(0).getContent().getEmbedded_data_fields());
SurveyViewUtils.showSharedInfoDetailsTextView(context, surveyView, questionsList.get(0).getContent().getEmbedded_data_fields());
SurveyViewUtils.showExternalSurveyButtons(surveyView);
handleOpenExternalSurvey(surveyView, questionsList.get(0).getContent().getUrl());
break;
case SurveyUtils.EXTERNAL_SURVEY_WITH_HERO_QUESTION:
SurveyViewUtils.showSharedInfoDetailsTextView(context,surveyView,questionsList.get(1).getContent().getEmbedded_data_fields());
externalSurveyUrl = questionsList.get(1).getContent().getUrl();
SurveyViewUtils.showSharedInfoDetailsTextView(context, surveyView, questionsList.get(1).getContent().getEmbedded_data_fields());
SurveyViewUtils.showHeroQuestionButtons(surveyView);
handleNextButton(surveyView, externalSurveyResult, questionsList.size() > 1 ? questionsList.get(1).getContent().getUrl() : "");
handleNextButton(surveyView);
break;

default:
SurveyViewUtils.showHeroQuestionButtons(surveyView);
handleNextButton(surveyView, externalSurveyResult, "");
handleNextButton(surveyView);
break;
}

Expand Down Expand Up @@ -135,14 +139,10 @@ public void submitSurveyAnswers(StudyResponse.Surveys survey, boolean heroQuesti
new Thread(request::call).start();
}

public void handleNextButton(View view, int externalSurveyResult, String externalSurveyUrl) {
public void handleNextButton(View view) {
Button nextBtn = view.findViewById(R.id.nextBtn);
nextBtn.setOnClickListener(view1 -> {
if (externalSurveyResult == 2) {
openExternalSurvey(externalSurveyUrl);
} else {
submitSurveyAnswers(mStudyResponse.getSurveys().get(curSurveyIndex), true);
}
submitSurveyAnswers(mStudyResponse.getSurveys().get(curSurveyIndex), true);
});
}

Expand All @@ -154,10 +154,10 @@ public void handleOpenExternalSurvey(View view, String url) {
}



private void openExternalSurvey(String url){
private void openExternalSurvey(String url) {
// TODO handle passing embedded data
Intent intent = new Intent(context, SurveyWebViewActivity.class);
intent.putExtra("url",url);
intent.putExtra("url", url);
context.startActivity(intent);
handleCompleteSurvey();
}
Expand Down Expand Up @@ -263,6 +263,11 @@ public void onSubmitSurveyResponseReceived(SubmitSurveyResponse response) {
}
// Mark survey as done
handleCompleteSurvey();
// Check if the external survey needs to be opened after responding to a hero question
if (externalSurveyResult == SurveyUtils.EXTERNAL_SURVEY_WITH_HERO_QUESTION) {
handleExternalSurvey();
return;
}
// Don't show survey question bottom sheet if we don't have another questions
if (haveOnlyHeroQuestion()) return;
updateSurveyPath = response.getSurveyResponse().getId();
Expand All @@ -272,6 +277,17 @@ public void onSubmitSurveyResponseReceived(SubmitSurveyResponse response) {
});
}


/**
* Handles the external survey process after responding to a hero question.
*/
private void handleExternalSurvey() {
if (externalSurveyUrl == null) return;

externalSurveyResult = SurveyUtils.DEFAULT_SURVEY;
openExternalSurvey(externalSurveyUrl);
}

/**
* Mark current survey as done
*/
Expand All @@ -286,7 +302,7 @@ public void onSubmitSurveyFail() {
}

public void setCurrentStop(ObaStop stop) {
if(stop == null || !isVisibleOnStops) return;
if (stop == null || !isVisibleOnStops) return;
this.currentStop = stop;
Log.d("CurrentStopID", currentStop.getId() + " ");
Log.d("CurrentStopRoutes", Arrays.toString(currentStop.getRouteIds()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class SurveyUtils {
public static final String EXTERNAL_SURVEY = "external_survey";
public static final String TEXT_QUESTION = "text";

public static final int DEFAULT_SURVEY = 0;
public static final int EXTERNAL_SURVEY_WITHOUT_HERO_QUESTION = 1;
public static final int EXTERNAL_SURVEY_WITH_HERO_QUESTION = 2;

Expand Down Expand Up @@ -133,6 +134,7 @@ public static Integer getCurrentSurveyIndex(StudyResponse studyResponse, Context
* @return true if the survey should be shown for the current stop, otherwise false.
*/
private static boolean showSurvey(ObaStop currentStop, List<String> visibleStopsList, List<String> visibleRouteList) {
if(currentStop == null || currentStop.getId() == null) return false;
// If both visibleStopsList and visibleRouteList are null, show the survey by default.
if (visibleRouteList == null && visibleStopsList == null) {
return true;
Expand Down Expand Up @@ -304,7 +306,7 @@ private static String getHeroQuestionAnswers(String type, View view) {
/**
* Checks if the current survey for the user has an external survey.
*
* @return 0 if it's a normal survey.
* @return 0 if it's a default survey.
* 1 if there is an external survey without a hero question.
* 2 if there is an external survey with a hero question.
*/
Expand Down

0 comments on commit 8f1a166

Please sign in to comment.