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

Add custom callback for autocomplete #2764

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
21 changes: 21 additions & 0 deletions catalog/src/main/assets/component_auto_complete.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@
}
}
]
},
{
"extension": [
{
"url": "http://hl7.org/fhir/StructureDefinition/questionnaire-itemControl",
"valueCodeableConcept": {
"coding": [
{
"system": "http://hl7.org/fhir/questionnaire-item-control",
"code": "autocomplete"
}
]
}
}
],
"linkId": "2",
"text": "Procedure",
"type": "choice",
"required": true,
"repeats": false,
"answerValueSet": "https://my.url/fhir/ValueSet/my-valueset"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@ import com.google.android.fhir.FhirEngine
import com.google.android.fhir.FhirEngineConfiguration
import com.google.android.fhir.FhirEngineProvider
import com.google.android.fhir.datacapture.DataCaptureConfig
import com.google.android.fhir.datacapture.ExternalAnswerValueSetResolver
import com.google.android.fhir.search.search
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import org.hl7.fhir.r4.model.Bundle
import org.hl7.fhir.r4.model.Coding

class CatalogApplication : Application(), DataCaptureConfig.Provider {
// Only initiate the FhirEngine when used for the first time, not when the app is created.
Expand All @@ -44,6 +47,26 @@ class CatalogApplication : Application(), DataCaptureConfig.Provider {
xFhirQueryResolver = { fhirEngine.search(it).map { it.resource } },
questionnaireItemViewHolderFactoryMatchersProviderFactory =
ContribQuestionnaireItemViewHolderFactoryMatchersProviderFactory,
valueSetResolverExternal =
object : ExternalAnswerValueSetResolver {
override suspend fun resolve(uri: String, query: String?): List<Coding> {
delay(1000)
// Here we can call out to our FHIR terminology server with the provided uri and query
if (uri == "https://my.url/fhir/ValueSet/my-valueset" && !query.isNullOrBlank()) {
return listOf(
Coding().apply {
code = "a"
display = "Custom response A"
},
Coding().apply {
code = "b"
display = "Custom response B"
},
)
}
return emptyList()
}
},
)

CoroutineScope(Dispatchers.IO).launch {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 Google LLC
* Copyright 2022-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 Down Expand Up @@ -74,12 +74,18 @@ data class DataCaptureConfig(
* allows the library to render answer options to `choice` and `open-choice` type questions more
* dynamically.
*
* Optional query parameter can be used to accept the search string from user input for server-side
* filtering.
*
* NOTE: The result of the resolution may be cached to improve performance. In other words, the
* resolver may be called only once after which the same answer value set may be used multiple times
* in the UI to populate answer options.
*
* @param uri The uri used to identify the questionnaire item
* @param query The text input from the user
*/
interface ExternalAnswerValueSetResolver {
suspend fun resolve(uri: String): List<Coding>
suspend fun resolve(uri: String, query: String?): List<Coding>
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 Google LLC
* Copyright 2022-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 Down
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 Down Expand Up @@ -76,6 +76,7 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.flow.withIndex
import kotlinx.coroutines.launch
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.DateTimeType
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent
Expand Down Expand Up @@ -389,6 +390,23 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
modificationCount.update { it + 1 }
}

/**
* Function to dynamically resolve answer options for the AutoComplete component using
* [ExternalAnswerValueSetResolver.resolve]
*/
private val autoCompleteAnswerOptionResolver: (String, String?, (List<Coding>) -> Unit) -> Unit =
{ query, answerValueSet, callback ->
viewModelScope.launch {
val response =
if (externalValueSetResolver != null && answerValueSet != null) {
externalValueSetResolver!!.resolve(query, answerValueSet)
} else {
emptyList()
}
callback(response)
}
}

private val expressionEvaluator: ExpressionEvaluator =
ExpressionEvaluator(
questionnaire,
Expand Down Expand Up @@ -950,6 +968,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
validationResult = validationResult,
answersChangedCallback = answersChangedCallback,
enabledAnswerOptions = enabledQuestionnaireAnswerOptions,
autoCompleteAnswerOptionResolver = autoCompleteAnswerOptionResolver,
minAnswerValue =
questionnaireItem.minValueCqfCalculatedValueExpression?.let {
expressionEvaluator.evaluateExpressionValue(
Expand Down Expand Up @@ -985,6 +1004,7 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
),
isHelpCardOpen = isHelpCard && isHelpCardOpen,
helpCardStateChangedCallback = helpCardStateChangedCallback,
// suggestions = suggestions,
),
)
add(question)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 Google LLC
* Copyright 2022-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 Down Expand Up @@ -179,7 +179,7 @@ internal class EnabledAnswerOptionsEvaluator(
}
} else {
// Ask the client to provide the answers from an external expanded Valueset.
externalValueSetResolver?.resolve(uri)?.map { coding ->
externalValueSetResolver?.resolve(uri, null)?.map { coding ->
Questionnaire.QuestionnaireItemAnswerOptionComponent(coding.copy())
}
}
Expand Down
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 Down Expand Up @@ -30,6 +30,7 @@ import com.google.android.fhir.datacapture.validation.NotValidated
import com.google.android.fhir.datacapture.validation.Valid
import com.google.android.fhir.datacapture.validation.ValidationResult
import com.google.android.fhir.datacapture.views.factories.QuestionnaireItemViewHolder
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Questionnaire
import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent
import org.hl7.fhir.r4.model.QuestionnaireResponse
Expand Down Expand Up @@ -93,6 +94,9 @@ data class QuestionnaireViewItem(
val helpCardStateChangedCallback: (Boolean, QuestionnaireResponseItemComponent) -> Unit =
{ _, _ ->
},
internal val autoCompleteAnswerOptionResolver: (String, String?, (List<Coding>) -> Unit) -> Unit =
{ _, _, _ ->
},
) {

fun getQuestionnaireResponseItem(): QuestionnaireResponseItemComponent = questionnaireResponseItem
Expand Down
Loading