diff --git a/collect_app/src/main/java/org/odk/collect/android/formentry/ODKView.java b/collect_app/src/main/java/org/odk/collect/android/formentry/ODKView.java index 5b6ce651c1b..2cc938c09a9 100644 --- a/collect_app/src/main/java/org/odk/collect/android/formentry/ODKView.java +++ b/collect_app/src/main/java/org/odk/collect/android/formentry/ODKView.java @@ -54,6 +54,7 @@ import org.javarosa.form.api.FormEntryCaption; import org.javarosa.form.api.FormEntryPrompt; import org.odk.collect.android.R; +import org.odk.collect.android.activities.FormFillingActivity; import org.odk.collect.android.application.Collect; import org.odk.collect.android.audio.AudioHelper; import org.odk.collect.android.exception.ExternalParamsException; @@ -196,7 +197,8 @@ public ODKView( viewLifecycle, new FileRequesterImpl(intentLauncher, externalAppIntentProvider, formController), new StringRequesterImpl(intentLauncher, externalAppIntentProvider, formController), - formController + formController, + (FormFillingActivity) context ); widgets = new ArrayList<>(); diff --git a/collect_app/src/main/java/org/odk/collect/android/widgets/WidgetFactory.java b/collect_app/src/main/java/org/odk/collect/android/widgets/WidgetFactory.java index b21d324b042..0b6f5977a1b 100644 --- a/collect_app/src/main/java/org/odk/collect/android/widgets/WidgetFactory.java +++ b/collect_app/src/main/java/org/odk/collect/android/widgets/WidgetFactory.java @@ -31,6 +31,7 @@ import org.odk.collect.android.formentry.questions.QuestionDetails; import org.odk.collect.android.geo.MapConfiguratorProvider; import org.odk.collect.android.javarosawrapper.FormController; +import org.odk.collect.android.listeners.AdvanceToNextListener; import org.odk.collect.android.storage.StoragePathProvider; import org.odk.collect.android.utilities.Appearances; import org.odk.collect.android.utilities.ExternalWebPageHelper; @@ -89,6 +90,7 @@ public class WidgetFactory { private final FileRequester fileRequester; private final StringRequester stringRequester; private final FormController formController; + private final AdvanceToNextListener advanceToNextListener; public WidgetFactory(Activity activity, boolean readOnlyOverride, @@ -103,7 +105,9 @@ public WidgetFactory(Activity activity, LifecycleOwner viewLifecycle, FileRequester fileRequester, StringRequester stringRequester, - FormController formController) { + FormController formController, + AdvanceToNextListener advanceToNextListener + ) { this.activity = activity; this.readOnlyOverride = readOnlyOverride; this.useExternalRecorder = useExternalRecorder; @@ -118,6 +122,7 @@ public WidgetFactory(Activity activity, this.fileRequester = fileRequester; this.stringRequester = stringRequester; this.formController = formController; + this.advanceToNextListener = advanceToNextListener; } public QuestionWidget createWidgetFromPrompt(FormEntryPrompt prompt, PermissionsProvider permissionsProvider) { @@ -315,7 +320,7 @@ private QuestionWidget getSelectOneWidget(String appearance, QuestionDetails que } else if (appearance.contains(Appearances.IMAGE_MAP)) { questionWidget = new SelectOneImageMapWidget(activity, questionDetails, isQuick, formEntryViewModel); } else if (appearance.contains(Appearances.MAP)) { - questionWidget = new SelectOneFromMapWidget(activity, questionDetails, isQuick); + questionWidget = new SelectOneFromMapWidget(activity, questionDetails, isQuick, advanceToNextListener); } else { questionWidget = new SelectOneWidget(activity, questionDetails, isQuick, formController, formEntryViewModel); } diff --git a/collect_app/src/main/java/org/odk/collect/android/widgets/items/SelectOneFromMapWidget.kt b/collect_app/src/main/java/org/odk/collect/android/widgets/items/SelectOneFromMapWidget.kt index 8b37c5926b4..bdae54e291f 100644 --- a/collect_app/src/main/java/org/odk/collect/android/widgets/items/SelectOneFromMapWidget.kt +++ b/collect_app/src/main/java/org/odk/collect/android/widgets/items/SelectOneFromMapWidget.kt @@ -26,15 +26,11 @@ import org.odk.collect.permissions.PermissionListener class SelectOneFromMapWidget( context: Context, questionDetails: QuestionDetails, - private val autoAdvance: Boolean + private val autoAdvance: Boolean, + private val autoAdvanceListener: AdvanceToNextListener ) : QuestionWidget(context, questionDetails), WidgetDataReceiver { - lateinit var autoAdvanceListener: AdvanceToNextListener - init { - if (context is AdvanceToNextListener) { - autoAdvanceListener = context - } render() } diff --git a/collect_app/src/test/java/org/odk/collect/android/widgets/WidgetFactoryTest.java b/collect_app/src/test/java/org/odk/collect/android/widgets/WidgetFactoryTest.java index 120d93911ab..4721556a489 100644 --- a/collect_app/src/test/java/org/odk/collect/android/widgets/WidgetFactoryTest.java +++ b/collect_app/src/test/java/org/odk/collect/android/widgets/WidgetFactoryTest.java @@ -37,7 +37,7 @@ public class WidgetFactoryTest { @Before public void setup() { Activity activity = CollectHelpers.buildThemedActivity(WidgetTestActivity.class).get(); - widgetFactory = new WidgetFactory(activity, false, false, null, null, null, null, mock(FormEntryViewModel.class), null, null, null, null, null, null); + widgetFactory = new WidgetFactory(activity, false, false, null, null, null, null, mock(FormEntryViewModel.class), null, null, null, null, null, null, mock()); } @Test diff --git a/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectOneFromMapWidgetTest.kt b/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectOneFromMapWidgetTest.kt index 76e49025777..88e89a95163 100644 --- a/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectOneFromMapWidgetTest.kt +++ b/collect_app/src/test/java/org/odk/collect/android/widgets/items/SelectOneFromMapWidgetTest.kt @@ -84,7 +84,8 @@ class SelectOneFromMapWidgetTest { val widget = SelectOneFromMapWidget( activityController.get(), QuestionDetails(promptWithAnswer(null)), - false + false, + mock() ) assertThat( @@ -109,7 +110,7 @@ class SelectOneFromMapWidgetTest { val prompt = promptWithAnswer(null) val widget = - SelectOneFromMapWidget(activity, QuestionDetails(prompt), false) + SelectOneFromMapWidget(activity, QuestionDetails(prompt), false, mock()) whenever(formEntryViewModel.getQuestionPrompt(prompt.index)).doReturn(prompt) widget.binding.button.performClick() @@ -131,7 +132,8 @@ class SelectOneFromMapWidgetTest { val widget = SelectOneFromMapWidget( activityController.get(), QuestionDetails(promptWithAnswer(null)), - false + false, + mock() ) permissionsProvider.setPermissionGranted(false) @@ -153,7 +155,7 @@ class SelectOneFromMapWidgetTest { .withAnswer(SelectOneData(choices[0].selection())) .build() - val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false) + val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false, mock()) assertThat(widget.binding.answer.text, equalTo("A")) } @@ -163,7 +165,8 @@ class SelectOneFromMapWidgetTest { val widget = SelectOneFromMapWidget( activityController.get(), QuestionDetails(promptWithAnswer(null)), - false + false, + mock() ) assertThat( @@ -180,7 +183,8 @@ class SelectOneFromMapWidgetTest { val widget = SelectOneFromMapWidget( activityController.get(), QuestionDetails(promptWithAnswer(answer)), - false + false, + mock() ) assertThat(widget.answer, equalTo(answer)) } @@ -193,7 +197,8 @@ class SelectOneFromMapWidgetTest { val widget = SelectOneFromMapWidget( activityController.get(), QuestionDetails(promptWithAnswer(answer)), - false + false, + mock() ) widget.clearAnswer() assertThat(widget.answer, equalTo(null)) @@ -207,7 +212,8 @@ class SelectOneFromMapWidgetTest { val widget = SelectOneFromMapWidget( activityController.get(), QuestionDetails(promptWithAnswer(answer)), - false + false, + mock() ) val mockValueChangedListener = mockValueChangedListener(widget) @@ -224,7 +230,7 @@ class SelectOneFromMapWidgetTest { .withAnswer(SelectOneData(choices[0].selection())) .build() - val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false) + val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false, mock()) widget.clearAnswer() assertThat(widget.binding.answer.text, equalTo("")) @@ -235,7 +241,8 @@ class SelectOneFromMapWidgetTest { val widget = SelectOneFromMapWidget( activityController.get(), QuestionDetails(promptWithAnswer(null)), - false + false, + mock() ) val selectChoice = selectChoice(value = "a", index = 101) @@ -253,7 +260,7 @@ class SelectOneFromMapWidgetTest { mapOf(choices[0] to "A", choices[1] to "B") ) .build() - val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false) + val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false, mock()) widget.setData(SelectOneData(choices[1].selection())) assertThat(widget.binding.answer.text, equalTo("B")) @@ -267,7 +274,7 @@ class SelectOneFromMapWidgetTest { .withSelectChoiceText(mapOf(choices[0] to "A")) .build() - val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false) + val widget = SelectOneFromMapWidget(activityController.get(), QuestionDetails(prompt), false, mock()) val mockValueChangedListener = mockValueChangedListener(widget) widget.setData(SelectOneData(choices[0].selection())) @@ -295,7 +302,7 @@ class SelectOneFromMapWidgetTest { .build() val widget = - SelectOneFromMapWidget(activity, QuestionDetails(prompt), false) + SelectOneFromMapWidget(activity, QuestionDetails(prompt), false, mock()) widget.setData(SelectOneData(choices[1].selection())) whenever(formEntryViewModel.getQuestionPrompt(prompt.index)).doReturn(prompt) @@ -315,13 +322,13 @@ class SelectOneFromMapWidgetTest { @Test fun `setData calls AdvanceToNextListener when the 'quick' appearance is used`() { + val listener = mock() val widget = SelectOneFromMapWidget( activityController.get(), QuestionDetails(promptWithAnswer(null)), - true + true, + listener ) - val listener = mock() - widget.autoAdvanceListener = listener val selectChoice = selectChoice(value = "a", index = 101) val answer = SelectOneData(selectChoice.selection()) @@ -331,13 +338,13 @@ class SelectOneFromMapWidgetTest { @Test fun `setData does not call AdvanceToNextListener when the 'quick' appearance is not used`() { + val listener = mock() val widget = SelectOneFromMapWidget( activityController.get(), QuestionDetails(promptWithAnswer(null)), - false + false, + listener ) - val listener = mock() - widget.autoAdvanceListener = listener val selectChoice = selectChoice(value = "a", index = 101) val answer = SelectOneData(selectChoice.selection())