Skip to content

Commit

Permalink
fix: [ANDROAPP-6667] display warning/error on complete
Browse files Browse the repository at this point in the history
Signed-off-by: Manu Muñoz <[email protected]>
  • Loading branch information
mmmateos committed Dec 11, 2024
1 parent 6a70824 commit ca60a04
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 38 deletions.
4 changes: 2 additions & 2 deletions form/src/main/java/org/dhis2/form/data/FormRepositoryImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ class FormRepositoryImpl(
EventStatus.COMPLETED -> {
FieldsWithWarningResult(
fieldUidWarningList = itemsWithWarning,
canComplete = false,
canComplete = ruleEffectsResult?.canComplete ?: false,
onCompleteMessage = ruleEffectsResult?.messageOnComplete,
eventResultDetails = EventResultDetails(
formValueStore.eventState(),
Expand Down Expand Up @@ -443,7 +443,7 @@ class FormRepositoryImpl(

EventStatus.COMPLETED -> {
SuccessfulResult(
canComplete = false,
canComplete = ruleEffectsResult?.canComplete ?: true,
onCompleteMessage = ruleEffectsResult?.messageOnComplete,
eventResultDetails = EventResultDetails(
formValueStore.eventState(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,15 @@ class FormResultDialogProvider(
eventState: EventStatus?,
result: DataIntegrityCheckResult,
): Pair<BottomSheetDialogUiModel, List<FieldWithIssue>> {
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
Expand Down Expand Up @@ -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)
}
Expand All @@ -101,7 +107,7 @@ class FormResultDialogProvider(
Pair(notSavedModel, emptyList())
}
is SuccessfulResult -> {
Pair(model, emptyList())
Pair(model, onCompleteMessages)
}
}
}
Expand Down Expand Up @@ -166,44 +172,54 @@ class FormResultDialogProvider(
errorFields: List<FieldWithIssue> = emptyList(),
mandatoryFields: List<String> = emptyList(),
warningFields: List<FieldWithIssue> = emptyList(),
onCompleteFields: List<FieldWithIssue> = emptyList(),
): List<FieldWithIssue> {
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<FieldWithIssue> {
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<FieldWithIssue>,
mandatoryFields: Map<String, String>,
warningFields: List<FieldWithIssue>,
errorOnComplete: Boolean,
onCompleteFields: List<FieldWithIssue>,
) = 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 }
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
}
}
}
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit ca60a04

Please sign in to comment.