Skip to content

Commit

Permalink
Fix questionnaire prepopulation using initialExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
LZRS committed May 7, 2024
1 parent 7882d96 commit dd523e8
Showing 1 changed file with 57 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import androidx.core.os.bundleOf
import androidx.fragment.app.commit
import androidx.lifecycle.lifecycleScope
import com.google.android.fhir.datacapture.QuestionnaireFragment
import com.google.android.fhir.logicalId
import com.google.android.fhir.datacapture.extensions.logicalId
import com.google.android.fhir.datacapture.mapping.ResourceMapper
import com.google.android.gms.location.FusedLocationProviderClient
import com.google.android.gms.location.LocationServices
import dagger.hilt.android.AndroidEntryPoint
Expand Down Expand Up @@ -278,69 +279,83 @@ class QuestionnaireActivity : BaseMultiLanguageActivity() {
)
finish()
}
val questionnaireFragmentBuilder =
QuestionnaireFragment.builder()
.setQuestionnaire(questionnaire.json())
.setCustomQuestionnaireItemViewHolderFactoryMatchersProvider(
OPENSRP_ITEM_VIEWHOLDER_FACTORY_MATCHERS_PROVIDER,
)
.showAsterisk(questionnaireConfig.showRequiredTextAsterisk)
.showRequiredText(questionnaireConfig.showRequiredText)

val questionnaireSubjectType = questionnaire.subjectType.firstOrNull()?.code
val resourceType =
questionnaireConfig.resourceType ?: questionnaireSubjectType?.let { ResourceType.valueOf(it) }
val resourceIdentifier = questionnaireConfig.resourceIdentifier

if (resourceType != null && !resourceIdentifier.isNullOrEmpty()) {
// Add subject and other configured resource to launchContext
val launchContextResources =
LinkedList<Resource>().apply {
// Add subject and other configured resource to launchContext
val launchContextResources =
LinkedList<Resource>().apply {

Check warning on line 290 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L289-L290

Added lines #L289 - L290 were not covered by tests
if (resourceType != null && !resourceIdentifier.isNullOrEmpty()) {
viewModel.loadResource(resourceType, resourceIdentifier)?.let { add(it) }
addAll(
// Exclude the subject resource its already added
viewModel.retrievePopulationResources(
actionParameters.filterNot {
it.paramType == ActionParameterType.QUESTIONNAIRE_RESPONSE_POPULATION_RESOURCE &&
resourceType == it.resourceType &&
resourceIdentifier.equals(it.value, ignoreCase = true)
},
),
)
val actionParametersExcludingSubject =

Check warning on line 293 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L293

Added line #L293 was not covered by tests
actionParameters.filterNot {
it.paramType == ActionParameterType.QUESTIONNAIRE_RESPONSE_POPULATION_RESOURCE &&
resourceType == it.resourceType &&
resourceIdentifier.equals(it.value, ignoreCase = true)
}
addAll(viewModel.retrievePopulationResources(actionParametersExcludingSubject))

Check warning on line 299 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L299

Added line #L299 was not covered by tests
} else {
addAll(viewModel.retrievePopulationResources(actionParameters))
}

if (launchContextResources.isNotEmpty()) {
questionnaireFragmentBuilder.setQuestionnaireLaunchContextMap(
launchContextResources.associate {
Pair(it.resourceType.name.lowercase(), it.encodeResourceToString())
},
)
}
val launchContextStringMap =
launchContextResources.associate {
Pair(it.resourceType.name.lowercase(), it.encodeResourceToString())

Check warning on line 306 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L304-L306

Added lines #L304 - L306 were not covered by tests
}

// Populate questionnaire with latest QuestionnaireResponse
if (questionnaireConfig.isEditable()) {
val latestQuestionnaireResponse =
viewModel.searchLatestQuestionnaireResponse(
// Populate questionnaire with initial default values
ResourceMapper.populate(
questionnaire,
launchContexts = launchContextResources.associateBy { it.resourceType.name.lowercase() },

Check warning on line 312 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L310-L312

Added lines #L310 - L312 were not covered by tests
)

// Populate questionnaire with latest QuestionnaireResponse
val questionnaireResponse: QuestionnaireResponse?

Check warning on line 316 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L316

Added line #L316 was not covered by tests

if (
resourceType != null &&
!resourceIdentifier.isNullOrEmpty() &&
questionnaireConfig.isEditable()
) {
val validQuestionnaireResponse =
viewModel
.searchLatestQuestionnaireResponse(

Check warning on line 325 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L323-L325

Added lines #L323 - L325 were not covered by tests
resourceId = resourceIdentifier,
resourceType = resourceType,
questionnaireId = questionnaire.logicalId,
)

val questionnaireResponse =
?.takeIf { viewModel.validateQuestionnaireResponse(questionnaire, it, this) }
questionnaireResponse =

Check warning on line 331 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L331

Added line #L331 was not covered by tests
if (validQuestionnaireResponse != null) {
QuestionnaireResponse().apply {
item = latestQuestionnaireResponse?.item
item = validQuestionnaireResponse.item

Check warning on line 334 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L334

Added line #L334 was not covered by tests
// Clearing the text prompts the SDK to re-process the content, which includes HTML
clearText()
}

if (viewModel.validateQuestionnaireResponse(questionnaire, questionnaireResponse, this)) {
questionnaireFragmentBuilder.setQuestionnaireResponse(questionnaireResponse.json())
} else {
showToast(getString(R.string.error_populating_questionnaire))
null

Check warning on line 340 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L340

Added line #L340 was not covered by tests
}
}
} else {
questionnaireResponse = null

Check warning on line 343 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L343

Added line #L343 was not covered by tests
}
return questionnaireFragmentBuilder

return QuestionnaireFragment.builder()
.setQuestionnaire(questionnaire.json())
.setCustomQuestionnaireItemViewHolderFactoryMatchersProvider(
OPENSRP_ITEM_VIEWHOLDER_FACTORY_MATCHERS_PROVIDER,

Check warning on line 349 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L346-L349

Added lines #L346 - L349 were not covered by tests
)
.showAsterisk(questionnaireConfig.showRequiredTextAsterisk)
.showRequiredText(questionnaireConfig.showRequiredText)
.apply {
launchContextStringMap

Check warning on line 354 in android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt

View check run for this annotation

Codecov / codecov/patch

android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt#L353-L354

Added lines #L353 - L354 were not covered by tests
.takeIf { it.isNotEmpty() }
?.let { setQuestionnaireLaunchContextMap(it) }
questionnaireResponse?.let { setQuestionnaireResponse(it.json()) }
}
}

private fun Resource.json(): String = this.encodeResourceToString()
Expand Down

0 comments on commit dd523e8

Please sign in to comment.