Skip to content

Commit

Permalink
Added Builder to Questionnaire Fragment. (#1847)
Browse files Browse the repository at this point in the history
* Added Builder to Questionnaire Fragment and made the fragment closed

* Added tests

* Review comments

* Fixed failin test

* Review comments
  • Loading branch information
aditya-07 committed Feb 14, 2023
1 parent d4210a0 commit 5c9a79f
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 70 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 Google LLC
* Copyright 2022 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 @@ -16,11 +16,12 @@

package com.google.android.fhir.catalog

import com.google.android.fhir.datacapture.QuestionnaireFragment
import com.google.android.fhir.datacapture.QuestionnaireFragment.QuestionnaireItemViewHolderFactoryMatcher
import com.google.android.fhir.datacapture.contrib.views.barcode.QuestionnaireItemBarCodeReaderViewHolderFactory

class CustomQuestionnaireFragment : QuestionnaireFragment() {
override fun getCustomQuestionnaireItemViewHolderFactoryMatchers():
// TODO Remove this file and move this code to maybe a custom view in catalog app?
class CustomQuestionnaireFragment /*: QuestionnaireFragment()*/ {
/*override*/ fun getCustomQuestionnaireItemViewHolderFactoryMatchers():
List<QuestionnaireItemViewHolderFactoryMatcher> {
return listOf(
QuestionnaireItemViewHolderFactoryMatcher(CustomNumberPickerFactory) { questionnaireItem ->
Expand All @@ -30,9 +31,8 @@ class CustomQuestionnaireFragment : QuestionnaireFragment() {
},
QuestionnaireItemViewHolderFactoryMatcher(QuestionnaireItemBarCodeReaderViewHolderFactory) {
questionnaireItem ->
questionnaireItem.getExtensionByUrl(
QuestionnaireItemBarCodeReaderViewHolderFactory.WIDGET_EXTENSION
)
questionnaireItem
.getExtensionByUrl(QuestionnaireItemBarCodeReaderViewHolderFactory.WIDGET_EXTENSION)
.let {
if (it == null) false
else it.value.toString() == QuestionnaireItemBarCodeReaderViewHolderFactory.WIDGET_TYPE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,8 @@ import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.add
import androidx.fragment.app.commit
import androidx.fragment.app.replace
import androidx.fragment.app.setFragmentResultListener
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
Expand Down Expand Up @@ -146,14 +143,12 @@ class DemoQuestionnaireFragment : Fragment() {
if (childFragmentManager.findFragmentByTag(QUESTIONNAIRE_FRAGMENT_TAG) == null) {
childFragmentManager.commit {
setReorderingAllowed(true)
add<QuestionnaireFragment>(
add(
R.id.container,
tag = QUESTIONNAIRE_FRAGMENT_TAG,
args =
bundleOf(
QuestionnaireFragment.EXTRA_QUESTIONNAIRE_JSON_STRING to
viewModel.getQuestionnaireJson()
)
QuestionnaireFragment.builder()
.setQuestionnaire(viewModel.getQuestionnaireJson())
.build(),
QUESTIONNAIRE_FRAGMENT_TAG
)
}
}
Expand All @@ -179,13 +174,10 @@ class DemoQuestionnaireFragment : Fragment() {
}
childFragmentManager.commit {
setReorderingAllowed(true)
replace<QuestionnaireFragment>(
replace(
R.id.container,
tag = QUESTIONNAIRE_FRAGMENT_TAG,
args =
bundleOf(
QuestionnaireFragment.EXTRA_QUESTIONNAIRE_JSON_STRING to questionnaireJsonString
)
QuestionnaireFragment.builder().setQuestionnaire(questionnaireJsonString).build(),
QUESTIONNAIRE_FRAGMENT_TAG
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package com.google.android.fhir.datacapture

import android.app.Application
import com.google.android.fhir.datacapture.DataCaptureConfig.Provider
import com.google.android.fhir.datacapture.QuestionnaireFragment.QuestionnaireItemViewHolderFactoryMatcher
import com.google.android.fhir.datacapture.QuestionnaireFragment.QuestionnaireItemViewHolderFactoryMatchersProvider
import org.hl7.fhir.r4.context.SimpleWorkerContext
import org.hl7.fhir.r4.model.Coding
import org.hl7.fhir.r4.model.Resource
Expand Down Expand Up @@ -53,6 +55,15 @@ data class DataCaptureConfig(
* https://build.fhir.org/ig/HL7/sdc/expressions.html#fhirquery for more details.
*/
var xFhirQueryResolver: XFhirQueryResolver? = null,

/**
* A [QuestionnaireItemViewHolderFactoryMatchersProviderFactory] may be set by the client to
* provide [QuestionnaireItemViewHolderFactoryMatcher]s to add custom questionnaire components or
* override the behaviour of existing components in the sdc.
*/
var questionnaireItemViewHolderFactoryMatchersProviderFactory:
QuestionnaireItemViewHolderFactoryMatchersProviderFactory? =
null
) {

internal val simpleWorkerContext: SimpleWorkerContext by lazy {
Expand Down Expand Up @@ -93,3 +104,17 @@ interface ExternalAnswerValueSetResolver {
fun interface XFhirQueryResolver {
suspend fun resolve(xFhirQuery: String): List<Resource>
}

/**
* Factory to create [QuestionnaireItemViewHolderFactoryMatchersProvider] for the
* [QuestionnaireFragment] to provide [List] of [QuestionnaireItemViewHolderFactoryMatcher]. The
* developers may provide the factory to the library via [DataCaptureConfig] to add custom
* questionnaire components or override the behaviour of existing components in the sdc.
*
* See the
* [developer guide](https://github.com/google/android-fhir/wiki/SDCL:-Customize-how-a-Questionnaire-is-displayed#custom-questionnaire-components)
* for more information.
*/
fun interface QuestionnaireItemViewHolderFactoryMatchersProviderFactory {
fun get(provider: String): QuestionnaireItemViewHolderFactoryMatchersProvider
}
Loading

0 comments on commit 5c9a79f

Please sign in to comment.