From d54faec7f68cfa9ee2b4826a87a970950a8117ff Mon Sep 17 00:00:00 2001 From: Xavier Molloy Date: Tue, 28 May 2024 13:47:48 +0200 Subject: [PATCH 1/2] fix: [ANDROAPP-6137] take into account number of fields if section is for event category combo, refactor for cognitive complexity --- form/src/main/java/org/dhis2/form/ui/Form.kt | 63 ++++++++++++++++---- 1 file changed, 52 insertions(+), 11 deletions(-) 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 7056344e33..3c91e79577 100644 --- a/form/src/main/java/org/dhis2/form/ui/Form.kt +++ b/form/src/main/java/org/dhis2/form/ui/Form.kt @@ -36,6 +36,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import org.dhis2.commons.resources.ResourceManager import org.dhis2.form.R +import org.dhis2.form.data.EventRepository import org.dhis2.form.model.FieldUiModel import org.dhis2.form.model.FormSection import org.dhis2.form.ui.event.RecyclerViewUiEvents @@ -75,9 +76,19 @@ fun Form( LazyColumn( modifier = Modifier .fillMaxSize() - .background(Color.White, shape = RoundedCornerShape(topStart = 16.dp, topEnd = 16.dp, bottomStart = 0.dp, bottomEnd = 0.dp)) + .background( + Color.White, + shape = RoundedCornerShape( + topStart = 16.dp, + topEnd = 16.dp, + bottomStart = 0.dp, + bottomEnd = 0.dp, + ), + ) .clickable( - interactionSource = MutableInteractionSource(), + interactionSource = remember { + MutableInteractionSource() + }, indication = null, onClick = { focusManager.clearFocus() }, ), @@ -101,12 +112,13 @@ fun Form( } } + val completedAndTotalFields = totalAndCompletedFields(section) Section( title = section.title, isLastSection = getNextSection(section, sections) == null, - description = if (section.fields.isNotEmpty()) section.description else null, - completedFields = section.completedFields(), - totalFields = section.fields.size, + description = sectionDescription(section), + completedFields = completedAndTotalFields.second, + totalFields = completedAndTotalFields.first, state = section.state, errorCount = section.errorCount(), warningCount = section.warningCount(), @@ -132,12 +144,8 @@ fun Form( resources = resources, focusManager = focusManager, onNextClicked = { - if (index == section.fields.size - 1) { - onNextSection() - focusNext.value = true - } else { - focusManager.moveFocus(FocusDirection.Down) - } + focusNext.value = (index == section.fields.size - 1) + manageOnNextEvent(focusManager, index, section, onNextSection) }, ) } @@ -155,6 +163,39 @@ fun Form( } } +private fun manageOnNextEvent( + focusManager: FocusManager, + index: Int, + section: FormSection, + onNext: () -> Unit, +) { + if (index == section.fields.size - 1) { + onNext() + } else { + focusManager.moveFocus(FocusDirection.Down) + } +} + +private fun sectionDescription(section: FormSection): String? { + return if (section.fields.isNotEmpty()) section.description else null +} + +private fun totalAndCompletedFields(section: FormSection): Pair { + var totalFields = section.fields.size + var completedFields = section.completedFields() + if (section.uid == EventRepository.EVENT_CATEGORY_COMBO_SECTION_UID && section.fields.first().eventCategories != null) { + completedFields = section.fields.first().eventCategories?.associate { category -> + category.options.find { option -> + section.fields.first().value?.split(",")?.contains(option.uid) == true + }?.let { + category.uid to it + } ?: (category.uid to null) + }?.count { it.value != null } ?: 0 + totalFields = section.fields.first().eventCategories?.size ?: 1 + } + return Pair(totalFields, completedFields) +} + fun shouldDisplayNoFieldsWarning(sections: List): Boolean { return if (sections.size == 1) { val section = sections.first() From a74e61e31b87573cd43e2523535f269d315a0673 Mon Sep 17 00:00:00 2001 From: Xavier Molloy Date: Wed, 29 May 2024 10:52:40 +0200 Subject: [PATCH 2/2] fix: [ANDROAPP-6137] femove unused methods and variable --- form/src/main/java/org/dhis2/form/ui/Form.kt | 33 +++----------------- 1 file changed, 5 insertions(+), 28 deletions(-) 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 3c91e79577..ad2ea55870 100644 --- a/form/src/main/java/org/dhis2/form/ui/Form.kt +++ b/form/src/main/java/org/dhis2/form/ui/Form.kt @@ -20,9 +20,6 @@ import androidx.compose.material.Icon import androidx.compose.material.icons.Icons import androidx.compose.material.icons.outlined.ErrorOutline import androidx.compose.runtime.Composable -import androidx.compose.runtime.LaunchedEffect -import androidx.compose.runtime.MutableState -import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier @@ -31,8 +28,6 @@ import androidx.compose.ui.focus.FocusDirection import androidx.compose.ui.focus.FocusManager import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalFocusManager -import androidx.compose.ui.unit.dp -import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.launch import org.dhis2.commons.resources.ResourceManager import org.dhis2.form.R @@ -72,17 +67,16 @@ fun Form( } } } - val focusNext = remember { mutableStateOf(false) } LazyColumn( modifier = Modifier .fillMaxSize() .background( Color.White, shape = RoundedCornerShape( - topStart = 16.dp, - topEnd = 16.dp, - bottomStart = 0.dp, - bottomEnd = 0.dp, + topStart = Spacing.Spacing16, + topEnd = Spacing.Spacing16, + bottomStart = Spacing.Spacing0, + bottomEnd = Spacing.Spacing0, ), ) .clickable( @@ -92,7 +86,7 @@ fun Form( indication = null, onClick = { focusManager.clearFocus() }, ), - contentPadding = PaddingValues(horizontal = 16.dp, vertical = 16.dp), + contentPadding = PaddingValues(horizontal = Spacing.Spacing16, vertical = Spacing.Spacing16), state = scrollState, ) { if (sections.isNotEmpty()) { @@ -144,7 +138,6 @@ fun Form( resources = resources, focusManager = focusManager, onNextClicked = { - focusNext.value = (index == section.fields.size - 1) manageOnNextEvent(focusManager, index, section, onNextSection) }, ) @@ -233,22 +226,6 @@ fun NoFieldsWarning(resources: ResourceManager) { } } -private fun FocusManager.moveFocusNext(focusNext: MutableState) { - if (focusNext.value) { - this.moveFocus(FocusDirection.Next) - focusNext.value = false - } -} - -@Composable -private fun LaunchIfTrue(key: Boolean, block: suspend CoroutineScope.() -> Unit) { - LaunchedEffect(key) { - if (key) { - block() - } - } -} - private fun getNextSection(section: FormSection, sections: List): FormSection? { val currentIndex = sections.indexOf(section) if (currentIndex != -1 && currentIndex < sections.size - 1) {