Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configurable drop down answer option. #2526

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google LLC
* Copyright 2023-2024 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -349,6 +349,33 @@ class DropDownViewHolderFactoryEspressoTest {
assertThat((answerHolder!!.single().value as Reference).id).isEqualTo("ref_2")
}

@Test
fun shouldSetDefaultValueFromInitialProperty() {
val questionnaireViewItem =
QuestionnaireViewItem(
answerOptions("Coding 1", "Coding 2", "Coding 3", "Coding 4", "Coding 5"),
responseOptions(),
validationResult = NotValidated,
answersChangedCallback = { _, _, _, _ -> },
)
questionnaireViewItem.questionnaireItem.addInitial(
Questionnaire.QuestionnaireItemInitialComponent().apply {
this.value = StringType("Select Answer")
},
)
runOnUI {
viewHolder.bind(questionnaireViewItem)
viewHolder.itemView.findViewById<AutoCompleteTextView>(R.id.auto_complete).showDropDown()
}
onView(withId(R.id.auto_complete)).perform(delayMainThread())
onView(withText("Select Answer"))
.inRoot(isPlatformPopup())
.check(matches(isDisplayed()))
.perform(click())
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.auto_complete).text.toString())
.isEqualTo("Select Answer")
}

/** Method to run code snippet on UI/main thread */
private fun runOnUI(action: () -> Unit) {
activityScenarioRule.scenario.onActivity { action() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ internal object DropDownViewHolderFactory :
hint = questionnaireViewItem.enabledDisplayItems.localizedFlyoverSpanned
helperText = getRequiredOrOptionalText(questionnaireViewItem, context)
}
val initialAnswerString =
if (questionnaireViewItem.questionnaireItem.hasInitial()) {
questionnaireViewItem.questionnaireItem.initial.first().valueStringType.valueAsString
} else {
context.getString(R.string.hyphen)
}
Comment on lines +70 to +75
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the initial answer is not something the ui shoudl get involved with. the questionnaire view model should have already handled this.

val answerOptionList =
this.questionnaireViewItem.enabledAnswerOptions
.map {
Expand All @@ -80,8 +86,8 @@ internal object DropDownViewHolderFactory :
answerOptionList.add(
0,
DropDownAnswerOption(
context.getString(R.string.hyphen),
context.getString(R.string.hyphen),
initialAnswerString,
initialAnswerString,
Comment on lines +89 to +90
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one thing you do want to be careful is that if there's a clash between the user defined default value and one of the answer options from the questionnaire. in this case you might want to have a mechanism to give them distinct names -- this is a very very edge case though and i'm happy for you to just add a todo in the code.

null,
),
)
Expand Down
Loading