Skip to content

Commit

Permalink
[ANDROAPP-4826] Refactor section warning generation to mapper
Browse files Browse the repository at this point in the history
  • Loading branch information
andresmr committed Feb 21, 2024
1 parent d1f126a commit f57365d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
1 change: 1 addition & 0 deletions form/src/main/java/org/dhis2/form/model/FormSection.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
5 changes: 1 addition & 4 deletions form/src/main/java/org/dhis2/form/ui/Form.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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))
Expand Down
11 changes: 9 additions & 2 deletions form/src/main/java/org/dhis2/form/ui/mapper/FormSectionMapper.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand All @@ -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
},
),
)
}
Expand Down

0 comments on commit f57365d

Please sign in to comment.