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

Quantity unit options with initial quantity value #2395

Merged
merged 13 commits into from
Feb 26, 2024
4 changes: 2 additions & 2 deletions catalog/src/main/assets/behavior_calculated_expression.json
maimoonak marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"item": [
{
"linkId": "a-birthdate",
"text": "Birth Date",
"text": "Birth Date (select age to auto calculate if not known)",
"type": "date",
"extension": [
{
Expand All @@ -17,7 +17,7 @@
},
{
"linkId": "a-age-years",
"text": "Age years",
"text": "Age",
"type": "quantity",
"extension": [
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 Google LLC
* Copyright 2022-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 @@ -29,7 +29,7 @@ import org.hl7.fhir.r4.model.Enumeration
* client, we have to call the desired api on the GenericType passed to the [Enumeration] and get
* the desired value by calling the api's as described above.
*/
internal fun Enumeration<*>.toCoding(): Coding {
internal fun Enumeration<*>.toCodingUnit(): Coding {
jingtang10 marked this conversation as resolved.
Show resolved Hide resolved
val enumeration = this
return Coding().apply {
display =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,15 +582,12 @@ internal val Questionnaire.QuestionnaireItemComponent.unitOption: List<Coding>
return this.extension
.filter { it.url == EXTENSION_QUESTIONNAIRE_UNIT_OPTION_URL }
.map { it.value as Coding }
.ifEmpty {
.plus(
// https://build.fhir.org/ig/HL7/sdc/behavior.html#initial
maimoonak marked this conversation as resolved.
Show resolved Hide resolved
// quantity given as initial without value is for default unit reference purpose
if (this.hasInitial()) {
listOf(this.initialFirstRep.valueQuantity.toCoding())
} else {
listOf()
}
}
this.initial.map { it.valueQuantity.toCodingUnit() },
)
.distinctBy { it.code }
}

// ********************************************************************************************** //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,11 @@ internal fun Coding.toCodeType(): CodeType {
return CodeType(code)
}

/** Converts Quantity to Coding. */
internal fun Quantity.toCoding(): Coding {
/**
* Converts Quantity to Coding type. The resulting Coding properties are equivalent of Coding.system
* = Quantity.system Coding.code = Quantity.code Coding.display = Quantity.unit
*/
internal fun Quantity.toCodingUnit(): Coding {
return Coding(this.system, this.code, this.unit)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2023 Google LLC
* Copyright 2022-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 All @@ -24,7 +24,7 @@ import com.google.android.fhir.datacapture.extensions.logicalId
import com.google.android.fhir.datacapture.extensions.questionnaireLaunchContexts
import com.google.android.fhir.datacapture.extensions.targetStructureMap
import com.google.android.fhir.datacapture.extensions.toCodeType
import com.google.android.fhir.datacapture.extensions.toCoding
import com.google.android.fhir.datacapture.extensions.toCodingUnit
import com.google.android.fhir.datacapture.extensions.toIdType
import com.google.android.fhir.datacapture.extensions.toUriType
import com.google.android.fhir.datacapture.extensions.validateLaunchContextExtensions
Expand Down Expand Up @@ -743,7 +743,7 @@ private fun Base.asExpectedType(
return when {
questionnaireItemType == Questionnaire.QuestionnaireItemType.REFERENCE ->
asExpectedReferenceType()
this is Enumeration<*> -> toCoding()
this is Enumeration<*> -> toCodingUnit()
this is IdType -> StringType(idPart)
else -> this as Type
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import androidx.core.widget.doAfterTextChanged
import com.google.android.fhir.datacapture.R
import com.google.android.fhir.datacapture.extensions.getRequiredOrOptionalText
import com.google.android.fhir.datacapture.extensions.localizedFlyoverSpanned
import com.google.android.fhir.datacapture.extensions.toCoding
import com.google.android.fhir.datacapture.extensions.toCodingUnit
import com.google.android.fhir.datacapture.extensions.unitOption
import com.google.android.fhir.datacapture.validation.Invalid
import com.google.android.fhir.datacapture.validation.NotValidated
Expand Down Expand Up @@ -187,15 +187,12 @@ internal object QuantityViewHolderFactory :
}

val unit =
questionnaireViewItem.answers.singleOrNull()?.valueQuantity?.let {
Coding(it.system, it.code, it.unit)
}
questionnaireViewItem.answers.singleOrNull()?.valueQuantity?.toCodingUnit()
?: questionnaireViewItem.draftAnswer?.let { if (it is Coding) it else null }
?: questionnaireViewItem.questionnaireItem
.takeIf { it.hasInitial() }
?.initialFirstRep
?: questionnaireViewItem.questionnaireItem.initial
?.firstOrNull()
?.valueQuantity
?.toCoding()
?.toCodingUnit()
unitAutoCompleteTextView.setText(unit?.display ?: "")

val unitAdapter =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Google LLC
* Copyright 2022-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 @@ -30,7 +30,7 @@ class MoreEnumerationsTest {
@Test
fun enumeration_should_return_coding() {
val maleEnumerationCoding =
Enumeration(Enumerations.AdministrativeGenderEnumFactory(), "male").toCoding()
Enumeration(Enumerations.AdministrativeGenderEnumFactory(), "male").toCodingUnit()
jingtang10 marked this conversation as resolved.
Show resolved Hide resolved
val coding =
Coding().apply {
display = "Male"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,46 @@ class MoreQuestionnaireItemComponentsTest {
assertThat((result[0].equalsDeep(Coding("http://unit.org", "yr", "years"))))
}

@Test
fun `unitOption should return list with de-duplicated coding when multiple initial values of type quantity is defined`() {
val question =
Questionnaire.QuestionnaireItemComponent().apply {
addInitial(
Questionnaire.QuestionnaireItemInitialComponent(
Quantity().apply {
this.system = "http://unit.org"
this.code = "yr"
this.unit = "years"
},
),
)
addInitial(
Questionnaire.QuestionnaireItemInitialComponent(
Quantity().apply {
this.system = "http://unit.org"
this.code = "yr"
this.unit = "years"
},
),
)
addInitial(
Questionnaire.QuestionnaireItemInitialComponent(
Quantity().apply {
this.system = "http://unit.org"
this.code = "mo"
this.unit = "months"
},
),
)
}

val result = question.unitOption

assertThat(result).hasSize(2)
assertThat((result[0].equalsDeep(Coding("http://unit.org", "yr", "years"))))
assertThat((result[1].equalsDeep(Coding("http://unit.org", "mo", "months"))))
}

@Test
fun createQuestionResponseWithoutGroupAndNestedQuestions() {
val question =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ class MoreTypesTest {
this.unit = "years"
this.system = "http://unit.org"
}
val result = quantity.toCoding()
val result = quantity.toCodingUnit()
assertThat(result.equalsDeep(Coding("http://unit.org", "yr", "years")))
}

Expand Down
Loading