Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [ANDROAPP-6137] Category-Combo-section-shows-incorrect-number-of-fields #3659

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading