From ca60a04af8f3d86b2b1621799d17a6b2e1d60b79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manu=20Mu=C3=B1oz?= Date: Wed, 11 Dec 2024 17:14:25 +0100 Subject: [PATCH] fix: [ANDROAPP-6667] display warning/error on complete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Manu Muñoz --- .../org/dhis2/form/data/FormRepositoryImpl.kt | 4 +- .../ui/provider/FormResultDialogProvider.kt | 76 +++++++++++-------- .../bottomsheet/BottomSheetDialogContent.kt | 14 ++-- 3 files changed, 56 insertions(+), 38 deletions(-) diff --git a/form/src/main/java/org/dhis2/form/data/FormRepositoryImpl.kt b/form/src/main/java/org/dhis2/form/data/FormRepositoryImpl.kt index 0895fd5083..08b9d85074 100644 --- a/form/src/main/java/org/dhis2/form/data/FormRepositoryImpl.kt +++ b/form/src/main/java/org/dhis2/form/data/FormRepositoryImpl.kt @@ -285,7 +285,7 @@ class FormRepositoryImpl( EventStatus.COMPLETED -> { FieldsWithWarningResult( fieldUidWarningList = itemsWithWarning, - canComplete = false, + canComplete = ruleEffectsResult?.canComplete ?: false, onCompleteMessage = ruleEffectsResult?.messageOnComplete, eventResultDetails = EventResultDetails( formValueStore.eventState(), @@ -443,7 +443,7 @@ class FormRepositoryImpl( EventStatus.COMPLETED -> { SuccessfulResult( - canComplete = false, + canComplete = ruleEffectsResult?.canComplete ?: true, onCompleteMessage = ruleEffectsResult?.messageOnComplete, eventResultDetails = EventResultDetails( formValueStore.eventState(), diff --git a/form/src/main/java/org/dhis2/form/ui/provider/FormResultDialogProvider.kt b/form/src/main/java/org/dhis2/form/ui/provider/FormResultDialogProvider.kt index 0d3b58e96c..727eab537a 100644 --- a/form/src/main/java/org/dhis2/form/ui/provider/FormResultDialogProvider.kt +++ b/form/src/main/java/org/dhis2/form/ui/provider/FormResultDialogProvider.kt @@ -33,11 +33,15 @@ class FormResultDialogProvider( eventState: EventStatus?, result: DataIntegrityCheckResult, ): Pair> { + val onCompleteMessages = getOnCompleteMessage( + canComplete, + onCompleteMessage, + ) val dialogType = getDialogType( errorFields, emptyMandatoryFields, warningFields, - !canComplete && onCompleteMessage != null, + onCompleteMessages, ) val showSkipButton = when { dialogType == DialogType.WARNING || dialogType == DialogType.SUCCESSFUL -> true @@ -74,12 +78,14 @@ class FormResultDialogProvider( result.fieldUidErrorList, result.mandatoryFields.keys.toList(), result.warningFields, + onCompleteMessages, ) Pair(model, fieldsWithIssues) } is FieldsWithWarningResult -> { val fieldsWithIssues = getFieldsWithIssues( warningFields = result.fieldUidWarningList, + onCompleteFields = onCompleteMessages, ) return Pair(model, fieldsWithIssues) } @@ -101,7 +107,7 @@ class FormResultDialogProvider( Pair(notSavedModel, emptyList()) } is SuccessfulResult -> { - Pair(model, emptyList()) + Pair(model, onCompleteMessages) } } } @@ -166,44 +172,54 @@ class FormResultDialogProvider( errorFields: List = emptyList(), mandatoryFields: List = emptyList(), warningFields: List = emptyList(), + onCompleteFields: List = emptyList(), ): List { - return errorFields.plus( - mandatoryFields.map { - FieldWithIssue( - "uid", - it, - IssueType.MANDATORY, - provider.provideMandatoryField(), - ) - }, - ).plus(warningFields) + return onCompleteFields + .plus(errorFields) + .plus( + mandatoryFields.map { + FieldWithIssue( + "uid", + it, + IssueType.MANDATORY, + provider.provideMandatoryField(), + ) + }, + ).plus(warningFields) + } + + private fun getOnCompleteMessage( + canComplete: Boolean, + onCompleteMessage: String?, + ): List { + val issueOnComplete = onCompleteMessage?.let { + FieldWithIssue( + fieldUid = "", + fieldName = it, + issueType = when (canComplete) { + false -> IssueType.ERROR_ON_COMPLETE + else -> IssueType.WARNING_ON_COMPLETE + }, + message = "", + ) + } + return issueOnComplete?.let { listOf(it) } ?: emptyList() } private fun getDialogType( errorFields: List, mandatoryFields: Map, warningFields: List, - errorOnComplete: Boolean, + onCompleteFields: List, ) = when { - errorOnComplete -> { + onCompleteFields.any { it.issueType == IssueType.ERROR_ON_COMPLETE } -> DialogType.COMPLETE_ERROR - } - - errorFields.isNotEmpty() -> { - DialogType.ERROR - } - - mandatoryFields.isNotEmpty() -> { - DialogType.MANDATORY - } - - warningFields.isNotEmpty() -> { + errorFields.isNotEmpty() -> DialogType.ERROR + mandatoryFields.isNotEmpty() -> DialogType.MANDATORY + warningFields.isNotEmpty() || + onCompleteFields.any { it.issueType == IssueType.WARNING_ON_COMPLETE } -> DialogType.WARNING - } - - else -> { - DialogType.SUCCESSFUL - } + else -> DialogType.SUCCESSFUL } enum class DialogType { ERROR, MANDATORY, WARNING, SUCCESSFUL, COMPLETE_ERROR } } diff --git a/ui-components/src/main/java/org/dhis2/ui/dialogs/bottomsheet/BottomSheetDialogContent.kt b/ui-components/src/main/java/org/dhis2/ui/dialogs/bottomsheet/BottomSheetDialogContent.kt index cd81b20c7b..c5e8720ab0 100644 --- a/ui-components/src/main/java/org/dhis2/ui/dialogs/bottomsheet/BottomSheetDialogContent.kt +++ b/ui-components/src/main/java/org/dhis2/ui/dialogs/bottomsheet/BottomSheetDialogContent.kt @@ -243,11 +243,13 @@ fun IssueItem(fieldWithIssue: FieldWithIssue, onClick: () -> Unit) { color = textPrimary, fontSize = 14.sp, ) - Text( - text = fieldWithIssue.message, - color = textSecondary, - fontSize = 14.sp, - ) + if (fieldWithIssue.message.isNotEmpty()) { + Text( + text = fieldWithIssue.message, + color = textSecondary, + fontSize = 14.sp, + ) + } } } } @@ -308,7 +310,7 @@ fun DialogPreview3() { fun DialogPreview4() { val fieldsWithIssues = listOf( FieldWithIssue("Uid", "Age", IssueType.ERROR, ERROR_MESSAGE), - FieldWithIssue("Uid", DATE_BIRTH, IssueType.ERROR, ERROR_MESSAGE), + FieldWithIssue("Uid", DATE_BIRTH, IssueType.ERROR, ""), FieldWithIssue("Uid", DATE_BIRTH, IssueType.ERROR, ERROR_MESSAGE), FieldWithIssue("Uid", DATE_BIRTH, IssueType.ERROR, ERROR_MESSAGE), FieldWithIssue("Uid", DATE_BIRTH, IssueType.ERROR, ERROR_MESSAGE),