Skip to content

Commit

Permalink
Handle dismiss survey actions
Browse files Browse the repository at this point in the history
Signed-off-by: Amr Hossam <[email protected]>
  • Loading branch information
amrhossamdev committed Aug 20, 2024
1 parent d30da92 commit 3ae68f9
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.onebusaway.android.ui.survey;

public interface SurveyActionsListener {
void onSkipSurvey();
void onRemindMeLater();
void onCancelSurvey();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package org.onebusaway.android.ui.survey;

public class SurveyDialogActions {

private static SurveyActionsListener listener;

public static void setDialogActionListener(SurveyActionsListener dialogListener) {
listener = dialogListener;
}

public static void handleSkipSurvey() {
if (listener == null) return;
listener.onSkipSurvey();
}

public static void handleRemindMeLater() {
if (listener == null) return;
listener.onRemindMeLater();
}

public static void handleCancel() {
if (listener == null) return;
listener.onCancelSurvey();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import java.util.List;
import java.util.Objects;

public class SurveyManager {
public class SurveyManager implements SurveyActionsListener {
private final Context context;
private final StudyRequestListener studyRequestListener;
private final SubmitSurveyRequestListener submitSurveyRequestListener;
Expand Down Expand Up @@ -69,6 +69,7 @@ public SurveyManager(Context context, Boolean fromArrivalsList, StudyRequestList
this.studyRequestListener = studyRequestListener;
this.submitSurveyRequestListener = submitSurveyRequestListener;
this.isVisibleOnStops = fromArrivalsList;
setupSurveyDismissDialog();
}

public void requestSurveyData() {
Expand All @@ -87,18 +88,18 @@ private void updateSurveyData() {
switch (externalSurveyResult) {
case SurveyUtils.EXTERNAL_SURVEY_WITHOUT_HERO_QUESTION:
SurveyViewUtils.showSharedInfoDetailsTextView(context, surveyView, questionsList.get(0).getContent().getEmbedded_data_fields());
SurveyViewUtils.showExternalSurveyButtons(surveyView);
SurveyViewUtils.showExternalSurveyButtons(context, surveyView);
handleOpenExternalSurvey(surveyView, questionsList.get(0).getContent().getUrl());
break;
case SurveyUtils.EXTERNAL_SURVEY_WITH_HERO_QUESTION:
externalSurveyUrl = questionsList.get(1).getContent().getUrl();
SurveyViewUtils.showSharedInfoDetailsTextView(context, surveyView, questionsList.get(1).getContent().getEmbedded_data_fields());
SurveyViewUtils.showHeroQuestionButtons(surveyView);
SurveyViewUtils.showHeroQuestionButtons(context, surveyView);
handleNextButton(surveyView);
break;

default:
SurveyViewUtils.showHeroQuestionButtons(surveyView);
SurveyViewUtils.showHeroQuestionButtons(context, surveyView);
handleNextButton(surveyView);
break;
}
Expand Down Expand Up @@ -180,7 +181,7 @@ public void showAllSurveyQuestions() {
surveyBottomSheet = SurveyViewUtils.createSurveyBottomSheetDialog(context);
initSurveyQuestionsBottomSheet(context);
SurveyViewUtils.setupBottomSheetBehavior(surveyBottomSheet);
SurveyViewUtils.setupBottomSheetCloseButton(surveyBottomSheet);
SurveyViewUtils.setupBottomSheetCloseButton(context, surveyBottomSheet);
handleSubmitSurveyButton(Objects.requireNonNull(surveyBottomSheet.findViewById(R.id.submit_btn)));
surveyBottomSheet.show();
}
Expand Down Expand Up @@ -280,8 +281,9 @@ public void onSubmitSurveyResponseReceived(SubmitSurveyResponse response) {


/**
* Handles the external survey process after responding to a hero question.
* Handles the external survey open process after responding to a hero question.
*/

private void handleExternalSurvey() {
if (externalSurveyUrl == null) return;

Expand All @@ -294,9 +296,9 @@ private void handleExternalSurvey() {
*/
public void handleCompleteSurvey() {
StudyResponse.Surveys currentSurvey = mStudyResponse.getSurveys().get(curSurveyIndex);
SurveyDbHelper.markSurveyAsCompleted(context, currentSurvey);
// Remove the hero question view
if (isVisibleOnStops) arrivalsList.removeHeaderView(surveyView);
SurveyDbHelper.markSurveyAsCompletedOrSkipped(context, currentSurvey, SurveyDbHelper.SURVEY_COMPLETED);
// Remove the hero question view from the arrivals list if it was previously visible on the stops view
handleRemoveSurveyFromArrivalsHeader();
}

public void onSubmitSurveyFail() {
Expand All @@ -309,4 +311,35 @@ public void setCurrentStop(ObaStop stop) {
Log.d("CurrentStopID", currentStop.getId() + " ");
Log.d("CurrentStopRoutes", Arrays.toString(currentStop.getRouteIds()));
}

public void handleRemoveSurveyFromArrivalsHeader() {
if (!isVisibleOnStops || arrivalsList == null) return;
arrivalsList.removeHeaderView(surveyView);
}

public void setupSurveyDismissDialog() {
SurveyDialogActions.setDialogActionListener(this);
SurveyViewUtils.createDismissSurveyDialog(context);
}

/**
* Handles skipping the survey. The survey will be marked as skipped in the database with a state value of 2
* indicating it was not completed by the user.
*/
@Override
public void onSkipSurvey() {
handleRemoveSurveyFromArrivalsHeader();
StudyResponse.Surveys currentSurvey = mStudyResponse.getSurveys().get(curSurveyIndex);
SurveyDbHelper.markSurveyAsCompletedOrSkipped(context, currentSurvey, SurveyDbHelper.SURVEY_SKIPPED);
}

@Override
public void onRemindMeLater() {
// TODO: implement
}

@Override
public void onCancelSurvey() {
// By default will dismiss the survey
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

import android.content.Context;
import android.content.SharedPreferences;
import org.json.JSONObject;
import org.json.JSONException;
import java.util.HashMap;
import java.util.Iterator;

import java.util.UUID;

public class SurveyLocalData {
public class SurveyPreferences {

private static final String PREFS_NAME = "survey_pref";
private static final String UUID_KEY = "my_uuid";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ import androidx.room.PrimaryKey
)]
)
data class Survey(
@PrimaryKey val survey_id: Int, val study_id: Int, val name: String
@PrimaryKey val survey_id: Int, val study_id: Int, val name: String, val state:Int
)
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@ import org.onebusaway.android.ui.survey.entity.Survey
import org.onebusaway.android.ui.survey.repository.SurveyRepository

class SurveyDbHelper {

companion object {
private val coroutineScope = CoroutineScope(Dispatchers.IO + SupervisorJob())

const val SURVEY_COMPLETED = 1
const val SURVEY_SKIPPED = 2

@JvmStatic
fun markSurveyAsCompleted(context: Context, survey: StudyResponse.Surveys) {
fun markSurveyAsCompletedOrSkipped(context: Context, survey: StudyResponse.Surveys, state:Int) {
val surveyRepo = SurveyRepository(context)

val newStudy = Study(
survey.study.id, survey.study.name, survey.study.description, true
)
val newSurvey = Survey(survey.id, survey.study.id, survey.name)
val newSurvey = Survey(survey.id, survey.study.id, survey.name, state)
coroutineScope.launch {
try {
surveyRepo.addOrUpdateStudy(newStudy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.onebusaway.android.R;
import org.onebusaway.android.io.elements.ObaStop;
import org.onebusaway.android.io.request.survey.model.StudyResponse;
import org.onebusaway.android.ui.survey.SurveyLocalData;
import org.onebusaway.android.ui.survey.SurveyPreferences;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -35,11 +35,11 @@ public class SurveyUtils {


public static String getUserUUID(Context context) {
if (SurveyLocalData.getUserUUID(context) == null) {
if (SurveyPreferences.getUserUUID(context) == null) {
UUID uuid = UUID.randomUUID();
SurveyLocalData.saveUserUUID(context, uuid);
SurveyPreferences.saveUserUUID(context, uuid);
}
return SurveyLocalData.getUserUUID(context);
return SurveyPreferences.getUserUUID(context);
}

public static List<String> getSelectedCheckBoxAnswer(View view) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.onebusaway.android.ui.survey.utils;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.TypedValue;
Expand All @@ -24,26 +26,31 @@

import org.onebusaway.android.R;
import org.onebusaway.android.io.request.survey.model.StudyResponse;
import org.onebusaway.android.ui.survey.SurveyDialogActions;

import java.util.List;

public class SurveyViewUtils {

public static void showHeroQuestionButtons(View surveyView) {
showCloseBtn(surveyView);
public static void showHeroQuestionButtons(Context context, View surveyView) {
handleCLoseButton(context, surveyView);
Button next = surveyView.findViewById(R.id.nextBtn);
next.setVisibility(View.VISIBLE);
}

public static void showExternalSurveyButtons(View surveyView) {
showCloseBtn(surveyView);
public static void showExternalSurveyButtons(Context context, View surveyView) {
handleCLoseButton(context, surveyView);
Button openExternalSurveyBtn = surveyView.findViewById(R.id.openExternalSurveyBtn);
openExternalSurveyBtn.setVisibility(View.VISIBLE);
}

public static void showCloseBtn(View surveyView) {
public static void handleCLoseButton(Context context, View surveyView) {
ImageButton closeBtn = surveyView.findViewById(R.id.close_btn);
closeBtn.setVisibility(View.VISIBLE);

closeBtn.setOnClickListener(v -> {
createDismissSurveyDialog(context).show();
});
}

public static void showQuestion(Context context, View rootView, StudyResponse.Surveys.Questions heroQuestion, String questionType) {
Expand Down Expand Up @@ -76,6 +83,20 @@ public static void setupBottomSheetBehavior(BottomSheetDialog bottomSheet) {
View parentLayout = bottomSheetDialog.findViewById(R.id.design_bottom_sheet);
if (parentLayout != null) {
BottomSheetBehavior<?> behavior = BottomSheetBehavior.from(parentLayout);
// This disable bottom sheet draggable swipe
behavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
@Override
public void onStateChanged(@NonNull View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_DRAGGING) {
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}

@Override
public void onSlide(@NonNull View bottomSheet, float slideOffset) {
}
});

ViewGroup.LayoutParams layoutParams = parentLayout.getLayoutParams();
layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;
parentLayout.setLayoutParams(layoutParams);
Expand All @@ -85,15 +106,29 @@ public static void setupBottomSheetBehavior(BottomSheetDialog bottomSheet) {
}

public static BottomSheetDialog createSurveyBottomSheetDialog(Context context) {
BottomSheetDialog bottomSheet = new BottomSheetDialog(context);
BottomSheetDialog bottomSheet = new BottomSheetDialog(context) {
@SuppressLint("MissingSuperCall")
@Override
public void onBackPressed() {
// Show a confirmation dialog when back is pressed
handleDismissSurveyBottomSheet(context, this);
}
};
bottomSheet.setContentView(R.layout.survey_questions_view);
return bottomSheet;

}

public static void setupBottomSheetCloseButton(BottomSheetDialog bottomSheet) {
public static void handleDismissSurveyBottomSheet(Context context, BottomSheetDialog surveyBottomSheet) {
new AlertDialog.Builder(context).setMessage(R.string.are_you_sure_you_want_to_dismiss_the_survey).setPositiveButton(context.getString(R.string.rt_yes), (dialog, which) -> {
surveyBottomSheet.hide();
}).setNegativeButton(context.getString(R.string.rt_no), null).show();
}

public static void setupBottomSheetCloseButton(Context context, BottomSheetDialog bottomSheet) {
ImageButton closeBtn = bottomSheet.findViewById(R.id.close_btn);
if (closeBtn != null) {
closeBtn.setOnClickListener(v -> bottomSheet.dismiss());
closeBtn.setOnClickListener(v -> handleDismissSurveyBottomSheet(context, bottomSheet));
}
}

Expand Down Expand Up @@ -189,7 +224,16 @@ private static CheckBox createCheckBox(Context ctx, StudyResponse.Surveys.Questi
return createButton(ctx, question, position, CheckBox.class);
}

public static void showSharedInfoDetailsTextView(Context context,View surveyView, List<String> questions) {
/**
* Displays a TextView containing user information that will be shared with an external survey.
* This method checks if there is any user information (in the form of survey questions) to be shared.
* If available, it constructs a message that lists the shared information and separates each piece of information with a comma.
*
* @param context The Context used to access resources.
* @param surveyView The View containing the TextView where the shared information will be displayed.
* @param questions A List of strings representing the user information that will be shared in the survey.
*/
public static void showSharedInfoDetailsTextView(Context context, View surveyView, List<String> questions) {
if (questions.isEmpty()) return;
TextView sharedInfoTextView = surveyView.findViewById(R.id.shared_info_tv);
StringBuilder surveySharedInfo = new StringBuilder();
Expand All @@ -203,7 +247,33 @@ public static void showSharedInfoDetailsTextView(Context context,View surveyView
}
sharedInfoTextView.setVisibility(View.VISIBLE);
sharedInfoTextView.setText(surveySharedInfo);

}
/**
* Creates an AlertDialog for dismissing a survey.
* This dialog allows the user to either skip the survey, be reminded later, or cancel the action.
*
* @param context The context in which the dialog will be displayed.
* @return The AlertDialog instance to be shown configured for the dismiss survey dialog.
*/
public static AlertDialog.Builder createDismissSurveyDialog(Context context) {
AlertDialog.Builder dismissSurveyDialog =
new AlertDialog.Builder(context)
.setTitle(R.string.dismiss_survey_dialog_title)
.setMessage(R.string.dismiss_survey_dialog_body)
.setNegativeButton(R.string.survey_dismiss_dialog_skip_this_survey, (dialog, which) -> {
SurveyDialogActions.handleSkipSurvey();
})
.setNeutralButton(R.string.remind_me_latter, (dialog, which) -> {
SurveyDialogActions.handleRemindMeLater();
})
.setPositiveButton(R.string.cancel, (dialog, which) -> {
SurveyDialogActions.handleCancel();
})
.setCancelable(true);

dismissSurveyDialog.create();
return dismissSurveyDialog;
}


}
5 changes: 5 additions & 0 deletions onebusaway-android/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1257,4 +1257,9 @@
<string name="submitted_successfully">Submitted Successfully</string>
<string name="please_fill_all_the_questions">Please fill all the questions</string>
<string name="sharing_survey_info_message">"This survey will share your "</string>
<string name="are_you_sure_you_want_to_dismiss_the_survey">Are you sure you want to dismiss the survey?</string>
<string name="dismiss_survey_dialog_title">Confirm Survey Dismissal</string>
<string name="remind_me_latter">Remind Me Later\n</string>
<string name="survey_dismiss_dialog_skip_this_survey">Skip This Survey</string>
<string name="dismiss_survey_dialog_body">Your feedback helps transit agencies and developers improve your experience.</string>
</resources>

0 comments on commit 3ae68f9

Please sign in to comment.