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

Question text with * if required field is true. #1643

Closed
Closed
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
Expand Up @@ -18,6 +18,7 @@ package com.google.android.fhir.datacapture

import android.text.Spanned
import androidx.core.text.HtmlCompat
import androidx.core.text.toSpanned
import com.google.android.fhir.getLocalizedText
import org.hl7.fhir.r4.model.BooleanType
import org.hl7.fhir.r4.model.CodeType
Expand Down Expand Up @@ -183,6 +184,19 @@ private fun String.toSpanned(): Spanned {
val Questionnaire.QuestionnaireItemComponent.localizedTextSpanned: Spanned?
get() = textElement?.getLocalizedText()?.toSpanned()

/**
* `*` value is appended to [Questionnaire.QuestionnaireItemComponent.localizedTextSpanned] if
* [Questionnaire.QuestionnaireItemComponent.required] is true.
*/
internal val Questionnaire.QuestionnaireItemComponent.localizedTextSpannedWithAsterisk: Spanned?
get() {
return if (required) {
localizedTextSpanned?.toString()?.plus("*")?.toSpanned()
} else {
localizedTextSpanned
}
}

/**
* Localized and spanned value of [Questionnaire.QuestionnaireItemComponent.prefix] if translation
* is present. Default value otherwise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import android.widget.TextView
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.localizedInstructionsSpanned
import com.google.android.fhir.datacapture.localizedPrefixSpanned
import com.google.android.fhir.datacapture.localizedTextSpanned
import com.google.android.fhir.datacapture.localizedTextSpannedWithAsterisk
import org.hl7.fhir.r4.model.Questionnaire

internal class QuestionnaireGroupTypeHeaderView(context: Context, attrs: AttributeSet?) :
Expand All @@ -40,7 +40,7 @@ internal class QuestionnaireGroupTypeHeaderView(context: Context, attrs: Attribu
val hint = findViewById<TextView>(R.id.hint)
initHelpButton(this, questionnaireItem)
prefix.updateTextAndVisibility(questionnaireItem.localizedPrefixSpanned)
question.updateTextAndVisibility(questionnaireItem.localizedTextSpanned)
question.updateTextAndVisibility(questionnaireItem.localizedTextSpannedWithAsterisk)
hint.updateTextAndVisibility(questionnaireItem.localizedInstructionsSpanned)
visibility = getViewGroupVisibility(prefix, question, hint)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import com.google.android.fhir.datacapture.hasHelpButton
import com.google.android.fhir.datacapture.localizedHelpSpanned
import com.google.android.fhir.datacapture.localizedInstructionsSpanned
import com.google.android.fhir.datacapture.localizedPrefixSpanned
import com.google.android.fhir.datacapture.localizedTextSpanned
import com.google.android.fhir.datacapture.localizedTextSpannedWithAsterisk
import com.google.android.material.card.MaterialCardView
import org.hl7.fhir.r4.model.Questionnaire

Expand All @@ -49,7 +49,7 @@ internal class QuestionnaireItemHeaderView(context: Context, attrs: AttributeSet

fun bind(questionnaireItem: Questionnaire.QuestionnaireItemComponent) {
prefix.updateTextAndVisibility(questionnaireItem.localizedPrefixSpanned)
question.updateTextAndVisibility(questionnaireItem.localizedTextSpanned)
question.updateTextAndVisibility(questionnaireItem.localizedTextSpannedWithAsterisk)
hint.updateTextAndVisibility(questionnaireItem.localizedInstructionsSpanned)
initHelpButton(this, questionnaireItem)
// Make the entire view GONE if there is nothing to show. This is to avoid an empty row in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,27 @@ class MoreQuestionnaireItemComponentsTest {
assertThat(questionnaireItem.localizedTextSpanned.toString()).isEqualTo("Thông tin bệnh nhân")
}

@Test
fun `localizedTextSpannedWithAsterisk returns localizedTextSpanned appended by *`() {
val questionnaireItem =
Questionnaire.QuestionnaireItemComponent().apply {
text = "Patient Information"
required = true
}
Locale.setDefault(Locale.US)
assertThat(questionnaireItem.localizedTextSpannedWithAsterisk.toString())
.isEqualTo("Patient Information*")
}

@Test
fun `localizedTextSpannedWithAsterisk returns localizedTextSpanned`() {
val questionnaireItem =
Questionnaire.QuestionnaireItemComponent().apply { text = "Patient Information" }
Locale.setDefault(Locale.US)
assertThat(questionnaireItem.localizedTextSpannedWithAsterisk.toString())
.isEqualTo("Patient Information")
}

@Test
fun localizedPrefixSpanned_noPrefix_shouldReturnNull() {
val questionnaireItem = Questionnaire.QuestionnaireItemComponent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ class QuestionnaireGroupTypeHeaderViewTest {
assertThat(view.findViewById<TextView>(R.id.question).text.toString()).isEqualTo("Question?")
}

@Test
fun `shows * at the end of question text`() {
view.bind(
Questionnaire.QuestionnaireItemComponent().apply {
text = "Question?"
required = true
}
)

assertThat(view.findViewById<TextView>(R.id.question).text.toString()).isEqualTo("Question?*")
}

@Test
fun `shows instructions`() {
view.bind(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,18 @@ class QuestionnaireItemHeaderViewTest {
assertThat(view.findViewById<TextView>(R.id.question).text.toString()).isEqualTo("Question?")
}

@Test
fun `shows * at the end of question text`() {
view.bind(
Questionnaire.QuestionnaireItemComponent().apply {
text = "Question?"
required = true
}
)

assertThat(view.findViewById<TextView>(R.id.question).text.toString()).isEqualTo("Question?*")
}

@Test
fun `shows instructions`() {
view.bind(
Expand Down