Skip to content

Commit

Permalink
PR comment updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinmost committed Oct 6, 2021
1 parent a713376 commit daf2736
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal class OptionSelectDialogFragment(
/** Whether multi-select will be enabled or not. */
val multiSelect: Boolean,
/** Whether the "other" option will be exposed for free-form text input. */
val addOtherOptions: Boolean,
val otherOptionsAllowed: Boolean,
)

private val viewModel: QuestionnaireItemDialogSelectViewModel by activityViewModels()
Expand Down Expand Up @@ -79,11 +79,10 @@ internal class OptionSelectDialogFragment(
.setView(view)
.setPositiveButton(android.R.string.ok) { _, _ -> saveSelections(adapter.currentList) }
.setNegativeButton(android.R.string.cancel) { _, _ -> }
.setCancelable(false)
.create()
}

/** Saves the current RecyclerView adapter into the ViewModel. */
/** Saves the current selections in the RecyclerView into the ViewModel. */
private fun saveSelections(currentList: List<OptionSelectRow>) {
lifecycleScope.launch {
viewModel.updateSelectedOptions(
Expand All @@ -103,13 +102,18 @@ internal class OptionSelectDialogFragment(
options.forEach { rows += OptionSelectRow.Option(it) }
if (otherOptions.isEmpty()) {
// No other options selected; just show an "Other" option if the config allows it
if (config.addOtherOptions) {
if (config.otherOptionsAllowed) {
rows += OptionSelectRow.OtherRow(selected = false)
}
} else {
// Other options were selected; show the "Other" option (selected) and any of those custom
// options in EditTexts
rows += OptionSelectRow.OtherRow(selected = true)
if (!config.multiSelect) {
check(otherOptions.size == 1) {
"Multiple 'Other' options selected in single-select mode: $otherOptions"
}
}
rows += otherOptions.map { OptionSelectRow.OtherEditText.fromText(it) }
}
return rows.sanitizeOtherOptionRows(multiSelectEnabled = config.multiSelect)
Expand Down Expand Up @@ -278,7 +282,7 @@ private sealed class OptionSelectRow {
/** A predefined option. */
data class Option(val option: OptionSelectOption) : OptionSelectRow()

/** "Other" option. Only shown if [OptionSelectDialogFragment.Config.addOtherOptions] is true. */
/** "Other" option. Only shown if [OptionSelectDialogFragment.Config.otherOptionsAllowed] is true. */
data class OtherRow(val selected: Boolean) : OptionSelectRow()

/** Text boxes for user to enter "Other" options in. Only shown when [OtherRow] is selected. */
Expand Down Expand Up @@ -318,7 +322,7 @@ private sealed class OptionSelectViewHolder(parent: ViewGroup, layout: Int) :
val checkbox: CheckBox = itemView.findViewById(R.id.checkbox)
}

/** Freeform option, only shown if [OptionSelectDialogFragment.Config.addOtherOptions] is true. */
/** Freeform option, only shown if [OptionSelectDialogFragment.Config.otherOptionsAllowed] is true. */
class OtherEditText(parent: ViewGroup) :
OptionSelectViewHolder(parent, R.layout.questionnaire_item_option_item_other_text) {
val editText: EditText = itemView.findViewById(R.id.edit_text)
Expand All @@ -335,7 +339,7 @@ private sealed class OptionSelectViewHolder(parent: ViewGroup, layout: Int) :
}
}

/** Freeform option, only shown if [OptionSelectDialogFragment.Config.addOtherOptions] is true. */
/** Freeform option, only shown if [OptionSelectDialogFragment.Config.otherOptionsAllowed] is true. */
class OtherAddAnother(parent: ViewGroup) :
OptionSelectViewHolder(parent, R.layout.questionnaire_item_option_item_other_add_another) {
val addAnother: Button = itemView.findViewById(R.id.add_another)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private fun Questionnaire.QuestionnaireItemComponent.buildConfig() =
OptionSelectDialogFragment.Config(
multiSelect = repeats,
// Client had to specify that they want an open-choice control to use "Other" options
addOtherOptions = itemControl == ItemControlTypes.OPEN_CHOICE,
otherOptionsAllowed = itemControl == ItemControlTypes.OPEN_CHOICE,
)

private fun QuestionnaireResponse.QuestionnaireResponseItemComponent.updateAnswers(
Expand Down

0 comments on commit daf2736

Please sign in to comment.