Skip to content

Commit

Permalink
Apply toSpanned for answer options (google#2689)
Browse files Browse the repository at this point in the history
* Use toSpanned

* Fix test

* Remove toString

* Add toSpanned for review mode

* Add toSpanned for dialog

* Revert "Remove toString"

This reverts commit 8eb6781.

* Trigger CI

---------

Co-authored-by: Jing Tang <[email protected]>
  • Loading branch information
FikriMilano and jingtang10 authored Nov 5, 2024
1 parent 6977691 commit e7c8c33
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.android.fhir.datacapture.extensions

import android.content.Context
import android.text.Spanned
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.views.factories.localDate
import com.google.android.fhir.datacapture.views.factories.localTime
Expand Down Expand Up @@ -57,6 +58,8 @@ fun Type.asStringValue(): String {
fun Type.displayString(context: Context): String =
getDisplayString(this, context) ?: context.getString(R.string.not_answered)

fun Type.displayStringSpanned(context: Context): Spanned = displayString(context).toSpanned()

/** Returns value as string depending on the [Type] of element. */
fun Type.getValueAsString(context: Context): String =
getValueString(this) ?: context.getString(R.string.not_answered)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.extensions.itemAnswerOptionImage
import com.google.android.fhir.datacapture.extensions.optionExclusive
import com.google.android.fhir.datacapture.extensions.toSpanned
import com.google.android.fhir.datacapture.views.factories.OptionSelectOption
import com.google.android.fhir.datacapture.views.factories.QuestionnaireItemDialogSelectViewModel
import com.google.android.fhir.datacapture.views.factories.SelectedOptions
Expand Down Expand Up @@ -208,7 +209,7 @@ private class OptionSelectAdapter(val multiSelectEnabled: Boolean) :
} else {
(holder as OptionSelectViewHolder.OptionSingle).radioButton
}
compoundButton.text = item.option.displayString
compoundButton.text = item.option.displayString.toSpanned()
compoundButton.setCompoundDrawablesRelative(
item.option.item.itemAnswerOptionImage(compoundButton.context),
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import androidx.lifecycle.lifecycleScope
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.extensions.ChoiceOrientationTypes
import com.google.android.fhir.datacapture.extensions.choiceOrientation
import com.google.android.fhir.datacapture.extensions.displayString
import com.google.android.fhir.datacapture.extensions.displayStringSpanned
import com.google.android.fhir.datacapture.extensions.itemAnswerOptionImage
import com.google.android.fhir.datacapture.extensions.optionExclusive
import com.google.android.fhir.datacapture.extensions.tryUnwrapContext
Expand Down Expand Up @@ -104,7 +104,7 @@ internal object CheckBoxGroupViewHolderFactory :
val checkbox =
checkboxLayout.findViewById<CheckBox>(R.id.check_box).apply {
id = viewId
text = answerOption.value.displayString(header.context)
text = answerOption.value.displayStringSpanned(header.context)
setCompoundDrawablesRelative(
answerOption.itemAnswerOptionImage(checkboxGroup.context),
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.google.android.fhir.datacapture.extensions.getRequiredOrOptionalText
import com.google.android.fhir.datacapture.extensions.getValidationErrorMessage
import com.google.android.fhir.datacapture.extensions.itemControl
import com.google.android.fhir.datacapture.extensions.localizedFlyoverSpanned
import com.google.android.fhir.datacapture.extensions.toSpanned
import com.google.android.fhir.datacapture.extensions.tryUnwrapContext
import com.google.android.fhir.datacapture.validation.ValidationResult
import com.google.android.fhir.datacapture.views.HeaderView
Expand Down Expand Up @@ -76,12 +77,12 @@ internal object QuestionnaireItemDialogSelectViewHolderFactory :

val questionnaireItem = questionnaireViewItem.questionnaireItem
val selectedOptions = questionnaireViewItem.extractInitialOptions(holder.header.context)
holder.summary.text = selectedOptions.selectedSummary
holder.summary.text = selectedOptions.selectedSummary.toSpanned()
selectedOptionsJob =
activity.lifecycleScope.launch {
// Listen for changes to selected options to update summary + FHIR data model
viewModel.getSelectedOptionsFlow(questionnaireItem.linkId).collect { selectedOptions ->
holder.summary.text = selectedOptions.selectedSummary
holder.summary.text = selectedOptions.selectedSummary.toSpanned()
updateAnswers(selectedOptions)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.google.android.fhir.datacapture.views.factories

import android.content.Context
import android.graphics.drawable.Drawable
import android.text.Spanned
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -33,6 +34,7 @@ import com.google.android.fhir.datacapture.extensions.getValidationErrorMessage
import com.google.android.fhir.datacapture.extensions.identifierString
import com.google.android.fhir.datacapture.extensions.itemAnswerOptionImage
import com.google.android.fhir.datacapture.extensions.localizedFlyoverSpanned
import com.google.android.fhir.datacapture.extensions.toSpanned
import com.google.android.fhir.datacapture.extensions.tryUnwrapContext
import com.google.android.fhir.datacapture.validation.ValidationResult
import com.google.android.fhir.datacapture.views.HeaderView
Expand Down Expand Up @@ -92,8 +94,8 @@ internal object DropDownViewHolderFactory :
answerOptionList
.firstOrNull { it.answerId == selectedAnswerIdentifier }
?.let {
autoCompleteTextView.setText(it.answerOptionString)
autoCompleteTextView.setSelection(it.answerOptionString.length)
autoCompleteTextView.setText(it.answerOptionStringSpanned())
autoCompleteTextView.setSelection(it.answerOptionStringSpanned().length)
autoCompleteTextView.setCompoundDrawablesRelative(
it.answerOptionImage,
null,
Expand All @@ -105,7 +107,7 @@ internal object DropDownViewHolderFactory :
autoCompleteTextView.onItemClickListener =
AdapterView.OnItemClickListener { _, _, position, _ ->
val selectedItem = adapter.getItem(position)
autoCompleteTextView.setText(selectedItem?.answerOptionString, false)
autoCompleteTextView.setText(selectedItem?.answerOptionStringSpanned(), false)
autoCompleteTextView.setCompoundDrawablesRelative(
adapter.getItem(position)?.answerOptionImage,
null,
Expand Down Expand Up @@ -165,7 +167,7 @@ internal class AnswerOptionDropDownArrayAdapter(
val answerOption: DropDownAnswerOption? = getItem(position)
val answerOptionTextView =
listItemView?.findViewById<View>(R.id.answer_option_textview) as TextView
answerOptionTextView.text = answerOption?.answerOptionString
answerOptionTextView.text = answerOption?.answerOptionStringSpanned()
answerOptionTextView.setCompoundDrawablesRelative(
answerOption?.answerOptionImage,
null,
Expand All @@ -187,4 +189,6 @@ internal data class DropDownAnswerOption(
override fun toString(): String {
return this.answerOptionString
}

fun answerOptionStringSpanned(): Spanned = answerOptionString.toSpanned()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import androidx.lifecycle.lifecycleScope
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.extensions.ChoiceOrientationTypes
import com.google.android.fhir.datacapture.extensions.choiceOrientation
import com.google.android.fhir.datacapture.extensions.displayString
import com.google.android.fhir.datacapture.extensions.displayStringSpanned
import com.google.android.fhir.datacapture.extensions.itemAnswerOptionImage
import com.google.android.fhir.datacapture.extensions.tryUnwrapContext
import com.google.android.fhir.datacapture.validation.Invalid
Expand Down Expand Up @@ -113,7 +113,7 @@ internal object RadioGroupViewHolderFactory :
val radioButton =
radioButtonItem.findViewById<RadioButton>(R.id.radio_button).apply {
id = viewId
text = answerOption.value.displayString(header.context)
text = answerOption.value.displayStringSpanned(header.context)
setCompoundDrawablesRelative(
answerOption.itemAnswerOptionImage(radioGroup.context),
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import com.google.android.fhir.datacapture.extensions.getHeaderViewVisibility
import com.google.android.fhir.datacapture.extensions.getLocalizedInstructionsSpanned
import com.google.android.fhir.datacapture.extensions.localizedFlyoverSpanned
import com.google.android.fhir.datacapture.extensions.localizedPrefixSpanned
import com.google.android.fhir.datacapture.extensions.toSpanned
import com.google.android.fhir.datacapture.extensions.updateTextAndVisibility
import com.google.android.fhir.datacapture.validation.Invalid
import com.google.android.fhir.datacapture.views.QuestionnaireViewItem
Expand Down Expand Up @@ -90,7 +91,7 @@ internal object ReviewViewHolderFactory : QuestionnaireItemViewHolderFactory(R.l
answerView.visibility = GONE
}
else -> {
answerView.text = questionnaireViewItem.answerString(answerView.context)
answerView.text = questionnaireViewItem.answerString(answerView.context).toSpanned()
answerView.visibility = VISIBLE
if (questionnaireViewItem.validationResult is Invalid) {
errorView.findViewById<TextView>(R.id.error_text_view).text =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ class CheckBoxGroupViewHolderFactoryTest {
val checkBoxGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.checkbox_group)
val children = checkBoxGroup.children.asIterable().filterIsInstance<CheckBox>()
children.forEachIndexed { index, view ->
assertThat(view.text).isEqualTo(questionnaire.answerOption[index].valueCoding.display)
assertThat(view.text.toString())
.isEqualTo(questionnaire.answerOption[index].valueCoding.display)
assertThat(view.layoutParams.width).isEqualTo(ViewGroup.LayoutParams.MATCH_PARENT)
}
}
Expand Down Expand Up @@ -161,7 +162,8 @@ class CheckBoxGroupViewHolderFactoryTest {
val checkBoxGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.checkbox_group)
val children = checkBoxGroup.children.asIterable().filterIsInstance<CheckBox>()
children.forEachIndexed { index, view ->
assertThat(view.text).isEqualTo(questionnaire.answerOption[index].valueCoding.display)
assertThat(view.text.toString())
.isEqualTo(questionnaire.answerOption[index].valueCoding.display)
assertThat(view.layoutParams.width).isEqualTo(ViewGroup.LayoutParams.WRAP_CONTENT)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ class RadioGroupViewHolderFactoryTest {
val radioGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.radio_group)
val children = radioGroup.children.asIterable().filterIsInstance<RadioButton>()
children.forEachIndexed { index, view ->
assertThat(view.text).isEqualTo(questionnaire.answerOption[index].valueCoding.display)
assertThat(view.text.toString())
.isEqualTo(questionnaire.answerOption[index].valueCoding.display)
assertThat(view.layoutParams.width).isEqualTo(ViewGroup.LayoutParams.MATCH_PARENT)
}
}
Expand Down Expand Up @@ -154,7 +155,8 @@ class RadioGroupViewHolderFactoryTest {
val radioGroup = viewHolder.itemView.findViewById<ConstraintLayout>(R.id.radio_group)
val children = radioGroup.children.asIterable().filterIsInstance<RadioButton>()
children.forEachIndexed { index, view ->
assertThat(view.text).isEqualTo(questionnaire.answerOption[index].valueCoding.display)
assertThat(view.text.toString())
.isEqualTo(questionnaire.answerOption[index].valueCoding.display)
assertThat(view.layoutParams.width).isEqualTo(ViewGroup.LayoutParams.WRAP_CONTENT)
}
}
Expand Down
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 @@ -180,7 +180,7 @@ class ReviewViewHolderFactoryTest {
),
)

assertThat(viewHolder.itemView.findViewById<TextView>(R.id.answer_text_view).text)
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.answer_text_view).text.toString())
.isEqualTo(
ApplicationProvider.getApplicationContext<Application>().getString(R.string.not_answered),
)
Expand All @@ -206,7 +206,7 @@ class ReviewViewHolderFactoryTest {
),
)

assertThat(viewHolder.itemView.findViewById<TextView>(R.id.answer_text_view).text)
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.answer_text_view).text.toString())
.isEqualTo(ApplicationProvider.getApplicationContext<Application>().getString(R.string.yes))
}

Expand Down Expand Up @@ -321,7 +321,7 @@ class ReviewViewHolderFactoryTest {
),
)

assertThat(viewHolder.itemView.findViewById<TextView>(R.id.answer_text_view).text)
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.answer_text_view).text.toString())
.isEqualTo("Yes")
}

Expand All @@ -340,7 +340,7 @@ class ReviewViewHolderFactoryTest {
),
)

assertThat(viewHolder.itemView.findViewById<TextView>(R.id.answer_text_view).text)
assertThat(viewHolder.itemView.findViewById<TextView>(R.id.answer_text_view).text.toString())
.isEqualTo("Not Answered")
}

Expand Down

0 comments on commit e7c8c33

Please sign in to comment.