Skip to content

Commit

Permalink
feat: Implement survey reminder functionality and refactor codebase
Browse files Browse the repository at this point in the history
Signed-off-by: Amr Hossam <[email protected]>
  • Loading branch information
amrhossamdev committed Aug 24, 2024
1 parent 4120392 commit 8d13503
Show file tree
Hide file tree
Showing 8 changed files with 189 additions and 138 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.onebusaway.android.io.request.survey;

import org.onebusaway.android.io.request.survey.submit.SubmitSurveyRequestListener;
import org.onebusaway.android.ui.survey.SurveyActionsListener;

public interface SurveyListener extends StudyRequestListener, SubmitSurveyRequestListener {
public interface SurveyListener extends StudyRequestListener, SubmitSurveyRequestListener, SurveyActionsListener {
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
// We don't use the ListFragment because the support library's version of
// the ListFragment doesn't work well with our header.
//
public class ArrivalsListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<ObaArrivalInfoResponse>, ArrivalsListHeader.Controller, SurveyListener {
public class ArrivalsListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<ObaArrivalInfoResponse>, ArrivalsListHeader.Controller {

private static final String TAG = "ArrivalsListFragment";

Expand Down Expand Up @@ -180,9 +180,6 @@ public class ArrivalsListFragment extends ListFragment implements LoaderManager.

private SurveyManager surveyManager;

private boolean isSurveyManagerInitialized = false;


public interface Listener {

/**
Expand Down Expand Up @@ -1763,33 +1760,51 @@ private Dialog createOccupancyDialog() {

private void initSurveyManager(){
// Avoiding doing multiple calls when updating the stop arrivals list
if(isSurveyManagerInitialized) return;
surveyManager = new SurveyManager(requireContext(),this,true);
surveyManager.requestSurveyData();
surveyManager.setCurrentStop(mStop);
surveyManager.initSurveyArrivalsHeaderView(getLayoutInflater());
surveyManager.initSurveyArrivalsList(getListView());
isSurveyManagerInitialized = true;
}
if(surveyManager == null){
View surveyView = getLayoutInflater().inflate(R.layout.item_survey,null);
surveyManager = new SurveyManager(requireContext(), surveyView,true, new SurveyListener() {
@Override
public void onSurveyResponseReceived(StudyResponse response) {
surveyManager.onSurveyResponseReceived(response);
}

@Override
public void onSurveyResponseReceived(StudyResponse response) {
surveyManager.onSurveyResponseReceived(response);
}
@Override
public void onSurveyResponseFail() {
surveyManager.onSurveyResponseFail();
}

@Override
public void onSurveyResponseFail() {
surveyManager.onSurveyResponseFail();
}
@Override
public void onSubmitSurveyResponseReceived(SubmitSurveyResponse response) {
surveyManager.onSubmitSurveyResponseReceived(response);
}

@Override
public void onSubmitSurveyResponseReceived(SubmitSurveyResponse response) {
surveyManager.onSubmitSurveyResponseReceived(response);
}
@Override
public void onSubmitSurveyFail() {
surveyManager.onSubmitSurveyFail();
}

@Override
public void onSubmitSurveyFail() {
surveyManager.onSubmitSurveyFail();

@Override
public void onSkipSurvey() {
surveyManager.onSkipSurvey();
}

@Override
public void onRemindMeLater() {
surveyManager.onRemindMeLater();
}

@Override
public void onCancelSurvey() {
surveyManager.onCancelSurvey();
}

});
surveyManager.requestSurveyData();
surveyManager.setCurrentStop(mStop);
surveyManager.initSurveyArrivalsList(getListView());
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public class HomeActivity extends AppCompatActivity
implements BaseMapFragment.OnFocusChangedListener,
BaseMapFragment.OnProgressBarChangedListener,
ArrivalsListFragment.Listener, NavigationDrawerCallbacks, WeatherRequestListener , RegionCallback,
ObaRegionsTask.Callback, SurveyListener {
ObaRegionsTask.Callback {


interface SlidingPanelController {
Expand Down Expand Up @@ -2150,29 +2150,45 @@ private void setupSurvey() {
}

private void initSurveyManager(View surveyView){
surveyManager = new SurveyManager(this, this, false);
surveyManager.setSurveyView(surveyView);
surveyManager.requestSurveyData();
}
surveyManager = new SurveyManager(this, surveyView,false, new SurveyListener() {
@Override
public void onSurveyResponseReceived(StudyResponse response) {
surveyManager.onSurveyResponseReceived(response);
}

@Override
public void onSurveyResponseReceived(StudyResponse response) {
surveyManager.onSurveyResponseReceived(response);
}
@Override
public void onSurveyResponseFail() {
surveyManager.onSurveyResponseFail();
}

@Override
public void onSurveyResponseFail() {
surveyManager.onSurveyResponseFail();
}
@Override
public void onSubmitSurveyResponseReceived(SubmitSurveyResponse response) {
surveyManager.onSubmitSurveyResponseReceived(response);
}

@Override
public void onSubmitSurveyResponseReceived(SubmitSurveyResponse response) {
surveyManager.onSubmitSurveyResponseReceived(response);
}
@Override
public void onSubmitSurveyFail() {
surveyManager.onSubmitSurveyFail();
}

@Override
public void onSubmitSurveyFail() {
surveyManager.onSubmitSurveyFail();

@Override
public void onSkipSurvey() {
surveyManager.onSkipSurvey();
}

@Override
public void onRemindMeLater() {
surveyManager.onRemindMeLater();
}

@Override
public void onCancelSurvey() {
surveyManager.onCancelSurvey();
}

});
surveyManager.requestSurveyData();
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.onebusaway.android.ui.survey;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
Expand Down Expand Up @@ -39,7 +37,7 @@
import java.util.Arrays;
import java.util.List;

public class SurveyManager implements SurveyActionsListener {
public class SurveyManager extends SurveyViewUtils implements SurveyActionsListener{
private final Context context;
private final StudyRequestListener studyRequestListener;
private final SubmitSurveyRequestListener submitSurveyRequestListener;
Expand All @@ -49,7 +47,7 @@ public class SurveyManager implements SurveyActionsListener {
// Holds current survey ID
private Integer curSurveyID;
// Holds the survey view
private View surveyView;
private final View surveyView;
// Bottom sheet RecyclerView for surveys
private RecyclerView bottomSheetSurveyRecyclerView;
// Button to submit the survey from the bottom sheet
Expand All @@ -67,18 +65,19 @@ public class SurveyManager implements SurveyActionsListener {
// true if we have a question as an external survey
private Integer externalSurveyResult = 0;

public SurveyManager(Context context, SurveyListener surveyListener, Boolean isVisibleOnStops) {
public SurveyManager(Context context, View surveyView,Boolean isVisibleOnStops ,SurveyListener surveyListener) {
super(surveyView, context, surveyListener);
this.context = context;
this.studyRequestListener = surveyListener;
this.submitSurveyRequestListener = surveyListener;
this.surveyView = surveyView;
this.isVisibleOnStops = isVisibleOnStops;
setupSurveyDismissDialog();
}

public void requestSurveyData() {
// Indicates whether the option to show available studies is enabled in preferences
boolean areStudiesEnabled = Application.getPrefs().getBoolean(context.getString(R.string.preference_key_show_available_studies), true);
boolean shouldShowSurvey = SurveyUtils.shouldShowSurveyView(isVisibleOnStops);
boolean shouldShowSurvey = SurveyUtils.shouldShowSurveyView(context, isVisibleOnStops);

if (!areStudiesEnabled || !shouldShowSurvey) return;

Expand Down Expand Up @@ -112,25 +111,25 @@ private void handleSurveyResult(int externalSurveyResult, List<StudyResponse.Sur
}

private void handleExternalSurveyWithoutHero(List<StudyResponse.Surveys.Questions> questionsList) {
SurveyViewUtils.showSharedInfoDetailsTextView(context, surveyView, questionsList.get(0).getContent().getEmbedded_data_fields(), SurveyUtils.EXTERNAL_SURVEY_WITHOUT_HERO_QUESTION);
SurveyViewUtils.showExternalSurveyButtons(context, surveyView);
showSharedInfoDetailsTextView(context, questionsList.get(0).getContent().getEmbedded_data_fields(), SurveyUtils.EXTERNAL_SURVEY_WITHOUT_HERO_QUESTION);
showExternalSurveyButtons();
handleOpenExternalSurvey(surveyView, questionsList.get(0).getContent().getUrl());
}

private void handleExternalSurveyWithHero(List<StudyResponse.Surveys.Questions> questionsList) {
externalSurveyUrl = questionsList.get(1).getContent().getUrl();
SurveyViewUtils.showSharedInfoDetailsTextView(context, surveyView, questionsList.get(1).getContent().getEmbedded_data_fields(), SurveyUtils.EXTERNAL_SURVEY_WITH_HERO_QUESTION);
SurveyViewUtils.showHeroQuestionButtons(context, surveyView);
showSharedInfoDetailsTextView(context, questionsList.get(1).getContent().getEmbedded_data_fields(), SurveyUtils.EXTERNAL_SURVEY_WITH_HERO_QUESTION);
showHeroQuestionButtons();
handleNextButton(surveyView);
}

private void handleDefaultSurvey() {
SurveyViewUtils.showHeroQuestionButtons(context, surveyView);
showHeroQuestionButtons();
handleNextButton(surveyView);
}

private void showHeroQuestion(StudyResponse.Surveys.Questions heroQuestion) {
SurveyViewUtils.showQuestion(context, surveyView, heroQuestion, heroQuestion.getContent().getType());
showQuestion(context, heroQuestion, heroQuestion.getContent().getType());
}


Expand All @@ -155,16 +154,14 @@ public void submitSurveyAnswers(StudyResponse.Surveys survey, boolean heroQuesti
// Reset `launchesUntilSurveyShown` for this session to trigger showing the next survey after the specified launch count
SurveyUtils.launchesUntilSurveyShown = Integer.MAX_VALUE;

JSONArray surveyResponseBody = heroQuestion
? SurveyUtils.getSurveyAnswersRequestBody(survey.getQuestions().get(0), surveyView)
: SurveyUtils.getSurveyAnswersRequestBody(survey.getQuestions());
JSONArray surveyResponseBody = heroQuestion ? SurveyUtils.getSurveyAnswersRequestBody(survey.getQuestions().get(0), surveyView) : SurveyUtils.getSurveyAnswersRequestBody(survey.getQuestions());

// Empty questions
if (surveyResponseBody == null) {
Toast.makeText(context, context.getString(R.string.please_fill_all_the_questions), Toast.LENGTH_SHORT).show();
return;
}
SurveyViewUtils.showProgress(surveyView);
showProgress();
Log.d("SurveyResponseBody", surveyResponseBody.toString());
ObaSubmitSurveyRequest request = new ObaSubmitSurveyRequest.Builder(context, submitSurveyAPIURL).setUserIdentifier(userIdentifier).setSurveyId(survey.getId()).setResponses(surveyResponseBody).setListener(submitSurveyRequestListener).build();
new Thread(request::call).start();
Expand Down Expand Up @@ -245,15 +242,6 @@ private boolean haveOnlyHeroQuestion() {
return questionsSize == 1;
}

@SuppressLint("InflateParams")
public void initSurveyArrivalsHeaderView(LayoutInflater inflater) {
surveyView = inflater.inflate(R.layout.item_survey, null);
}

public void setSurveyView(View surveyView) {
this.surveyView = surveyView;
}

public void initSurveyArrivalsList(ListView arrivalsList) {
this.arrivalsList = arrivalsList;
}
Expand Down Expand Up @@ -317,7 +305,7 @@ public void onSurveyResponseFail() {
public void onSubmitSurveyResponseReceived(SubmitSurveyResponse response) {
// Switch back to the main thread to update UI elements
ContextCompat.getMainExecutor(context).execute(() -> {
SurveyViewUtils.hideProgress(surveyView);
hideProgress();
// User answered the hero question and completed the survey
if (updateSurveyPath != null) {
surveyBottomSheet.hide();
Expand Down Expand Up @@ -367,7 +355,7 @@ private void handleCompleteSurvey() {
}

public void onSubmitSurveyFail() {
SurveyViewUtils.hideProgress(surveyView);
hideProgress();
Log.d("SubmitSurveyFail", curSurveyID + "");
}

Expand Down Expand Up @@ -398,15 +386,6 @@ private void handleRemoveSurvey() {
SurveyUtils.launchesUntilSurveyShown = Integer.MAX_VALUE;
}

/**
* Initializes and displays the survey dismiss dialog.
* Sets the listener for survey actions callbacks
*/
private 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
Expand All @@ -418,14 +397,15 @@ public void onSkipSurvey() {
StudyResponse.Surveys currentSurvey = mStudyResponse.getSurveys().get(curSurveyIndex);
SurveyDbHelper.markSurveyAsCompletedOrSkipped(context, currentSurvey, SurveyDbHelper.SURVEY_SKIPPED);
}

@Override

public void onRemindMeLater() {
// TODO handle reminder button
SurveyUtils.remindUserLater(context);
handleRemoveSurvey();
}

@Override

public void onCancelSurvey() {
// By default will dismiss the survey dialog
}
Expand Down
Loading

0 comments on commit 8d13503

Please sign in to comment.