diff --git a/collect_app/src/androidTest/java/org/odk/collect/android/feature/formentry/ExternalSelectsTest.kt b/collect_app/src/androidTest/java/org/odk/collect/android/feature/formentry/ExternalSelectsTest.kt index 35ab9601b82..4d8e59c04b0 100644 --- a/collect_app/src/androidTest/java/org/odk/collect/android/feature/formentry/ExternalSelectsTest.kt +++ b/collect_app/src/androidTest/java/org/odk/collect/android/feature/formentry/ExternalSelectsTest.kt @@ -56,4 +56,31 @@ class ExternalSelectsTest { .assertText("File: $formsDirPath/search_and_select-media/nombre.csv is missing.") .assertText("File: $formsDirPath/search_and_select-media/nombre2.csv is missing.") } + + @Test // https://github.com/getodk/collect/issues/6801 + fun searchFunctionWorksWellWithLastSaved() { + rule.startAtMainMenu() + // Fill out and finalize the first form + .copyForm("search-with-last-saved.xml", listOf("fruits.csv")) + .startBlankForm("Search with last-saved") + .clickOnText("Mango") + .swipeToNextQuestion("Select fruit 2") + .clickOnText("Oranges") + .swipeToEndScreen() + .clickFinalize() + + // Start a new form to verify that answers from the previous form are retained + .startBlankForm("Search with last-saved") + .swipeToNextQuestion("Select fruit 2") + .clickGoToArrow() + .assertHierarchyItem(0, "Select fruit 1", "Mango") + .assertHierarchyItem(1, "Select fruit 2", "Oranges") + + // Change an answer in a field-list and verify no errors occur + .clickOnQuestion("Select fruit 2") + .clickOnText("Strawberries") + .clickGoToArrow() + .assertHierarchyItem(0, "Select fruit 1", "Mango") + .assertHierarchyItem(1, "Select fruit 2", "Strawberries") + } } diff --git a/collect_app/src/main/java/org/odk/collect/android/dynamicpreload/ExternalDataUtil.java b/collect_app/src/main/java/org/odk/collect/android/dynamicpreload/ExternalDataUtil.java index fddf9501431..84740cf97bb 100644 --- a/collect_app/src/main/java/org/odk/collect/android/dynamicpreload/ExternalDataUtil.java +++ b/collect_app/src/main/java/org/odk/collect/android/dynamicpreload/ExternalDataUtil.java @@ -22,6 +22,8 @@ import org.javarosa.core.model.SelectChoice; import org.javarosa.core.model.condition.EvaluationContext; +import org.javarosa.core.model.data.IAnswerData; +import org.javarosa.core.model.data.helper.Selection; import org.javarosa.core.model.instance.FormInstance; import org.javarosa.form.api.FormEntryCaption; import org.javarosa.form.api.FormEntryPrompt; @@ -166,12 +168,18 @@ public static XPathFuncExpr getSearchXPathExpression(String appearance) { public static ArrayList populateExternalChoices(FormEntryPrompt formEntryPrompt, XPathFuncExpr xpathfuncexpr, FormController formController) throws FileNotFoundException { try { + IAnswerData selectedValue = formEntryPrompt.getAnswerValue(); + Selection selection = null; + if (selectedValue != null) { + selection = (Selection) selectedValue.getValue(); + } List selectChoices = formEntryPrompt.getSelectChoices(); ArrayList returnedChoices = new ArrayList<>(); for (SelectChoice selectChoice : selectChoices) { String value = selectChoice.getValue(); if (isAnInteger(value)) { // treat this as a static choice + attachChoiceToSelectionIfMatch(selection, selectChoice); returnedChoices.add(selectChoice); } else { String displayColumns = formEntryPrompt.getSelectChoiceText(selectChoice); @@ -201,6 +209,7 @@ public static ArrayList populateExternalChoices(FormEntryPrompt fo @SuppressWarnings("unchecked") List dynamicChoices = (ArrayList) eval; for (SelectChoice dynamicChoice : dynamicChoices) { + attachChoiceToSelectionIfMatch(selection, dynamicChoice); returnedChoices.add(dynamicChoice); } } else { @@ -228,6 +237,16 @@ public static ArrayList populateExternalChoices(FormEntryPrompt fo } } + private static void attachChoiceToSelectionIfMatch(Selection selection, SelectChoice selectChoice) { + if (selection == null || selection.index != -1) { + return; + } + + if (selection.getValue().equals(selectChoice.getValue())) { + selection.attachChoice(selectChoice); + } + } + /** * We could simple return new String(displayColumns + "," + valueColumn) but we want to handle * the cases 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 48f520738d2..7ba6776e141 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 @@ -161,7 +161,6 @@ public ODKView( LifecycleOwner viewLifecycle ) { super(context); - updateQuestions(questionPrompts); this.viewLifecycle = viewLifecycle; this.audioPlayer = audioPlayer; @@ -209,6 +208,7 @@ public ODKView( setupAudioErrors(); autoplayIfNeeded(advancingPage); + updateQuestions(questionPrompts); } private void setupAudioErrors() { diff --git a/test-forms/src/main/resources/forms/search-with-last-saved.xml b/test-forms/src/main/resources/forms/search-with-last-saved.xml new file mode 100644 index 00000000000..4fab2fed89c --- /dev/null +++ b/test-forms/src/main/resources/forms/search-with-last-saved.xml @@ -0,0 +1,55 @@ + + + + Search with last-saved + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + name_key + + + + + + + + name_key + + + + + + + + \ No newline at end of file