From f57365d5605ad7f233dc2f2889bb451ab7e5b192 Mon Sep 17 00:00:00 2001
From: andresmr <andres@dhis2.org>
Date: Wed, 21 Feb 2024 11:38:54 +0100
Subject: [PATCH] [ANDROAPP-4826] Refactor section warning generation to mapper

---
 .../src/main/java/org/dhis2/form/model/FormSection.kt |  1 +
 form/src/main/java/org/dhis2/form/ui/Form.kt          |  5 +----
 .../org/dhis2/form/ui/mapper/FormSectionMapper.kt     | 11 +++++++++--
 3 files changed, 11 insertions(+), 6 deletions(-)

diff --git a/form/src/main/java/org/dhis2/form/model/FormSection.kt b/form/src/main/java/org/dhis2/form/model/FormSection.kt
index 302c3b3b4b..83325b54a4 100644
--- a/form/src/main/java/org/dhis2/form/model/FormSection.kt
+++ b/form/src/main/java/org/dhis2/form/model/FormSection.kt
@@ -8,6 +8,7 @@ data class FormSection(
     val description: String? = null,
     val state: SectionState,
     val fields: List<FieldUiModel>,
+    var warningMessage: Int? = null,
 ) {
     fun completedFields() = fields.count { it.value != null }
     fun errorCount() = fields.count { it.error != null }
diff --git a/form/src/main/java/org/dhis2/form/ui/Form.kt b/form/src/main/java/org/dhis2/form/ui/Form.kt
index 2984d23041..d9974702ee 100644
--- a/form/src/main/java/org/dhis2/form/ui/Form.kt
+++ b/form/src/main/java/org/dhis2/form/ui/Form.kt
@@ -109,9 +109,6 @@ fun Form(
                 }
             }
 
-            val sectionMessage =
-                if (section.fields.isEmpty()) resources.getString(R.string.form_without_fields) else null
-
             Section(
                 title = section.title,
                 isLastSection = getNextSection(section, sections) == null,
@@ -121,7 +118,7 @@ fun Form(
                 state = section.state,
                 errorCount = section.errorCount(),
                 warningCount = section.warningCount(),
-                warningMessage = sectionMessage,
+                warningMessage = section.warningMessage?.let { resources.getString(it) },
                 onNextSection = onNextSection,
                 onSectionClick = {
                     intentHandler.invoke(FormIntent.OnSection(section.uid))
diff --git a/form/src/main/java/org/dhis2/form/ui/mapper/FormSectionMapper.kt b/form/src/main/java/org/dhis2/form/ui/mapper/FormSectionMapper.kt
index 17cfe0e794..118e47ae2a 100644
--- a/form/src/main/java/org/dhis2/form/ui/mapper/FormSectionMapper.kt
+++ b/form/src/main/java/org/dhis2/form/ui/mapper/FormSectionMapper.kt
@@ -1,5 +1,6 @@
 package org.dhis2.form.ui.mapper
 
+import org.dhis2.form.R
 import org.dhis2.form.model.FieldUiModel
 import org.dhis2.form.model.FieldUiModelImpl
 import org.dhis2.form.model.FormSection
@@ -13,6 +14,8 @@ class FormSectionMapper {
         if (hasSections(items)) {
             items.forEach { item ->
                 if (item is SectionUiModelImpl) {
+                    val fields = items.filterIsInstance<FieldUiModelImpl>()
+                        .filter { it.programStageSection == item.uid }
                     sections.add(
                         FormSection(
                             uid = item.uid,
@@ -23,8 +26,12 @@ class FormSectionMapper {
                                 false -> SectionState.CLOSE
                                 null -> SectionState.FIXED
                             },
-                            fields = items.filterIsInstance<FieldUiModelImpl>()
-                                .filter { it.programStageSection == item.uid },
+                            fields = fields,
+                            warningMessage = if (fields.isEmpty()) {
+                                R.string.form_without_fields
+                            } else {
+                                null
+                            },
                         ),
                     )
                 }