Skip to content

Commit

Permalink
Hoist check to viewmodel
Browse files Browse the repository at this point in the history
  • Loading branch information
chungwwei committed Feb 6, 2025
1 parent f0fde4b commit 45d4416
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Google LLC
* Copyright 2023-2025 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 @@ -25,6 +25,7 @@ import androidx.lifecycle.viewModelScope
import ca.uhn.fhir.context.FhirContext
import ca.uhn.fhir.context.FhirVersionEnum
import ca.uhn.fhir.parser.IParser
import com.google.android.fhir.compareTo
import com.google.android.fhir.datacapture.enablement.EnablementEvaluator
import com.google.android.fhir.datacapture.expressions.EnabledAnswerOptionsEvaluator
import com.google.android.fhir.datacapture.extensions.EntryMode
Expand Down Expand Up @@ -77,12 +78,16 @@ import kotlinx.coroutines.flow.update
import kotlinx.coroutines.flow.withIndex
import kotlinx.coroutines.launch
import org.hl7.fhir.r4.model.DateTimeType
import org.hl7.fhir.r4.model.DateType
import org.hl7.fhir.r4.model.DecimalType
import org.hl7.fhir.r4.model.IntegerType
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent
import org.hl7.fhir.r4.model.QuestionnaireResponse
import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemAnswerComponent
import org.hl7.fhir.r4.model.QuestionnaireResponse.QuestionnaireResponseItemComponent
import org.hl7.fhir.r4.model.Resource
import org.hl7.fhir.r4.model.Type
import timber.log.Timber

internal class QuestionnaireViewModel(application: Application, state: SavedStateHandle) :
Expand Down Expand Up @@ -204,7 +209,9 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
item: QuestionnaireItemComponent,
questionnaireItemToParentMap: ItemToParentMap,
) {
CheckMinAndMaxExtensionValues(item.minValue, item.maxValue)
for (child in item.item) {
CheckMinAndMaxExtensionValues(child.minValue, child.maxValue)
questionnaireItemToParentMap[child] = item
buildParentList(child, questionnaireItemToParentMap)
}
Expand Down Expand Up @@ -485,6 +492,10 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
}
}

// fun validateMinvalueExtensionAndMaxValueExtension() {
// questionnaire.
// }

/** Clears all the answers from the questionnaire response by iterating through each item. */
fun clearAllAnswers() {
questionnaireResponse.allItems.forEach { it.answer = emptyList() }
Expand Down Expand Up @@ -1141,6 +1152,22 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
block()
}
}

private fun CheckMinAndMaxExtensionValues(minValue: Type?, maxValue: Type?) {
if (minValue == null || maxValue == null) {
return
}
if (
(minValue is IntegerType && maxValue is IntegerType) ||
(minValue is DecimalType && maxValue is DecimalType) ||
(minValue is DateType && maxValue is DateType) ||
(minValue is DateTimeType && maxValue is DateTimeType)
) {
if (minValue > maxValue) {
throw IllegalArgumentException("minValue cannot be greater than maxValue")
}
}
}
}

typealias ItemToParentMap = MutableMap<QuestionnaireItemComponent, QuestionnaireItemComponent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,6 @@ internal object EditTextIntegerViewHolderFactory :
InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED,
) {

override fun bind(questionnaireViewItem: QuestionnaireViewItem) {
super.bind(questionnaireViewItem)

val minValue = (questionnaireViewItem.minAnswerValue as? IntegerType)?.value
val maxValue = (questionnaireViewItem.maxAnswerValue as? IntegerType)?.value

if (minValue != null && maxValue != null && minValue > maxValue) {
throw IllegalArgumentException("minValue cannot be greater than maxValue")
}
}

override suspend fun handleInput(
editable: Editable,
questionnaireViewItem: QuestionnaireViewItem,
Expand Down Expand Up @@ -102,7 +91,6 @@ internal object EditTextIntegerViewHolderFactory :
questionnaireViewItem,
questionnaireViewItem.validationResult,
)

val minValue =
(questionnaireViewItem.minAnswerValue as? IntegerType)?.value ?: Int.MIN_VALUE
val maxValue =
Expand Down

0 comments on commit 45d4416

Please sign in to comment.