From 8402be9bd942a507a1884036d869869d4cc63322 Mon Sep 17 00:00:00 2001 From: Fikri Milano Date: Mon, 16 Dec 2024 16:30:30 +0700 Subject: [PATCH] Update task status on draft submission (#3642) * Trigger task status update on draft submission * Fix QR properties assignment If this function launches another coroutine, any value assigned within this function will be null if accessed from outside before the coroutine completes, as the assignment process has not yet finished at the time of access. * spotless * spotless --- .../ui/questionnaire/QuestionnaireActivity.kt | 8 +++ .../questionnaire/QuestionnaireViewModel.kt | 54 +++++++++---------- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt index 9ed3132b57..effb4fdf9d 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireActivity.kt @@ -387,6 +387,14 @@ class QuestionnaireActivity : BaseMultiLanguageActivity() { lifecycleScope.launch { retrieveQuestionnaireResponse()?.let { questionnaireResponse -> viewModel.saveDraftQuestionnaire(questionnaireResponse, questionnaireConfig) + setResult( + Activity.RESULT_OK, + Intent().apply { + putExtra(QUESTIONNAIRE_RESPONSE, questionnaireResponse as Serializable) + putExtra(QUESTIONNAIRE_CONFIG, questionnaireConfig as Parcelable) + putExtra(ON_RESULT_TYPE, ActivityOnResultType.QUESTIONNAIRE.name) + }, + ) finish() } } diff --git a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt index ecbe982df5..8ad2172d7f 100644 --- a/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt +++ b/android/quest/src/main/java/org/smartregister/fhircore/quest/ui/questionnaire/QuestionnaireViewModel.kt @@ -706,42 +706,40 @@ constructor( * This function saves [QuestionnaireResponse] as draft if any of the [QuestionnaireResponse.item] * has an answer. */ - fun saveDraftQuestionnaire( + suspend fun saveDraftQuestionnaire( questionnaireResponse: QuestionnaireResponse, questionnaireConfig: QuestionnaireConfig, ) { - viewModelScope.launch { - val hasPages = questionnaireResponse.item.any { it.hasItem() } - val questionnaireHasAnswer = - questionnaireResponse.item.any { - if (!hasPages) { - it.answer.any { answerComponent -> answerComponent.hasValue() } - } else { - questionnaireResponse.item.any { page -> - page.item.any { pageItem -> - pageItem.answer.any { answerComponent -> answerComponent.hasValue() } - } + val hasPages = questionnaireResponse.item.any { it.hasItem() } + val questionnaireHasAnswer = + questionnaireResponse.item.any { + if (!hasPages) { + it.answer.any { answerComponent -> answerComponent.hasValue() } + } else { + questionnaireResponse.item.any { page -> + page.item.any { pageItem -> + pageItem.answer.any { answerComponent -> answerComponent.hasValue() } } } } - questionnaireResponse.questionnaire = - questionnaireConfig.id.asReference(ResourceType.Questionnaire).reference - if ( - !questionnaireConfig.resourceIdentifier.isNullOrBlank() && - questionnaireConfig.resourceType != null - ) { - questionnaireResponse.subject = - questionnaireConfig.resourceIdentifier!!.asReference( - questionnaireConfig.resourceType!!, - ) } - if (questionnaireHasAnswer) { - questionnaireResponse.status = QuestionnaireResponse.QuestionnaireResponseStatus.INPROGRESS - defaultRepository.addOrUpdate( - addMandatoryTags = true, - resource = questionnaireResponse, + questionnaireResponse.questionnaire = + questionnaireConfig.id.asReference(ResourceType.Questionnaire).reference + if ( + !questionnaireConfig.resourceIdentifier.isNullOrBlank() && + questionnaireConfig.resourceType != null + ) { + questionnaireResponse.subject = + questionnaireConfig.resourceIdentifier!!.asReference( + questionnaireConfig.resourceType!!, ) - } + } + if (questionnaireHasAnswer) { + questionnaireResponse.status = QuestionnaireResponse.QuestionnaireResponseStatus.INPROGRESS + defaultRepository.addOrUpdate( + addMandatoryTags = true, + resource = questionnaireResponse, + ) } }