Skip to content

Commit

Permalink
fix: [ANDROAPP-6137] Category-Combo-section-shows-incorrect-number-of…
Browse files Browse the repository at this point in the history
…-fields (#3659)

* fix: [ANDROAPP-6137] take into account number of fields if section is for event category combo, refactor for cognitive complexity

* fix: [ANDROAPP-6137] femove unused methods and variable
  • Loading branch information
xavimolloy authored May 29, 2024
1 parent 1156ebf commit 2d3b4e7
Showing 1 changed file with 52 additions and 34 deletions.
86 changes: 52 additions & 34 deletions form/src/main/java/org/dhis2/form/ui/Form.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -31,11 +28,10 @@ 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
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
Expand Down Expand Up @@ -71,17 +67,26 @@ 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))
.background(
Color.White,
shape = RoundedCornerShape(
topStart = Spacing.Spacing16,
topEnd = Spacing.Spacing16,
bottomStart = Spacing.Spacing0,
bottomEnd = Spacing.Spacing0,
),
)
.clickable(
interactionSource = MutableInteractionSource(),
interactionSource = remember {
MutableInteractionSource()
},
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()) {
Expand All @@ -101,12 +106,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(),
Expand All @@ -132,12 +138,7 @@ fun Form(
resources = resources,
focusManager = focusManager,
onNextClicked = {
if (index == section.fields.size - 1) {
onNextSection()
focusNext.value = true
} else {
focusManager.moveFocus(FocusDirection.Down)
}
manageOnNextEvent(focusManager, index, section, onNextSection)
},
)
}
Expand All @@ -155,6 +156,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<Int, Int> {
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<FormSection>): Boolean {
return if (sections.size == 1) {
val section = sections.first()
Expand Down Expand Up @@ -192,22 +226,6 @@ fun NoFieldsWarning(resources: ResourceManager) {
}
}

private fun FocusManager.moveFocusNext(focusNext: MutableState<Boolean>) {
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>): FormSection? {
val currentIndex = sections.indexOf(section)
if (currentIndex != -1 && currentIndex < sections.size - 1) {
Expand Down

0 comments on commit 2d3b4e7

Please sign in to comment.