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 81fa0481496..c4b7584871f 100644 --- a/form/src/main/java/org/dhis2/form/ui/Form.kt +++ b/form/src/main/java/org/dhis2/form/ui/Form.kt @@ -6,13 +6,19 @@ import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.padding import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.itemsIndexed import androidx.compose.foundation.lazy.rememberLazyListState +import androidx.compose.foundation.shape.RoundedCornerShape +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 @@ -21,6 +27,7 @@ import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope import androidx.compose.ui.Modifier +import androidx.compose.ui.draw.clip import androidx.compose.ui.focus.FocusDirection import androidx.compose.ui.focus.FocusManager import androidx.compose.ui.graphics.Color @@ -29,13 +36,19 @@ 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.model.FieldUiModel import org.dhis2.form.model.FormSection import org.dhis2.form.ui.event.RecyclerViewUiEvents import org.dhis2.form.ui.intent.FormIntent import org.dhis2.form.ui.provider.inputfield.FieldProvider +import org.hisp.dhis.mobile.ui.designsystem.component.InfoBar +import org.hisp.dhis.mobile.ui.designsystem.component.InfoBarData import org.hisp.dhis.mobile.ui.designsystem.component.Section import org.hisp.dhis.mobile.ui.designsystem.component.SectionState +import org.hisp.dhis.mobile.ui.designsystem.theme.Radius +import org.hisp.dhis.mobile.ui.designsystem.theme.Spacing +import org.hisp.dhis.mobile.ui.designsystem.theme.SurfaceColor @OptIn(ExperimentalFoundationApi::class) @Composable @@ -59,20 +72,20 @@ fun Form( } } } - val focusNext = remember { mutableStateOf(false) } - LazyColumn( - modifier = Modifier - .fillMaxSize() - .background(Color.White) - .clickable( - interactionSource = MutableInteractionSource(), - indication = null, - onClick = { focusManager.clearFocus() }, - ), - contentPadding = PaddingValues(horizontal = 16.dp, vertical = 16.dp), - state = scrollState, - ) { - if (sections.isNotEmpty()) { + if (sections.isNotEmpty()) { + val focusNext = remember { mutableStateOf(false) } + LazyColumn( + modifier = Modifier + .fillMaxSize() + .background(Color.White) + .clickable( + interactionSource = MutableInteractionSource(), + indication = null, + onClick = { focusManager.clearFocus() }, + ), + contentPadding = PaddingValues(horizontal = 16.dp, vertical = 16.dp), + state = scrollState, + ) { this.itemsIndexed( items = sections, key = { _, fieldUiModel -> fieldUiModel.uid }, @@ -100,48 +113,76 @@ fun Form( Section( title = section.title, isLastSection = getNextSection(section, sections) == null, - description = section.description, + description = if (sections.size >= 2 && section.fields.isNotEmpty()) section.description else null, completedFields = section.completedFields(), totalFields = section.fields.size, state = section.state, errorCount = section.errorCount(), warningCount = section.warningCount(), + warningMessage = resources.getString(R.string.form_without_fields), onNextSection = onNextSection, onSectionClick = { intentHandler.invoke(FormIntent.OnSection(section.uid)) }, content = { - section.fields.forEachIndexed { index, fieldUiModel -> - fieldUiModel.setCallback(callback) - FieldProvider( - modifier = Modifier.animateItemPlacement( - animationSpec = tween( - durationMillis = 500, - easing = LinearOutSlowInEasing, + if (sections.size >= 2 && section.fields.isNotEmpty()) { + section.fields.forEachIndexed { index, fieldUiModel -> + fieldUiModel.setCallback(callback) + FieldProvider( + modifier = Modifier.animateItemPlacement( + animationSpec = tween( + durationMillis = 500, + easing = LinearOutSlowInEasing, + ), ), - ), - fieldUiModel = fieldUiModel, - uiEventHandler = uiEventHandler, - intentHandler = intentHandler, - resources = resources, - focusManager = focusManager, - onNextClicked = { - if (index == section.fields.size - 1) { - onNextSection() - focusNext.value = true - } else { - focusManager.moveFocus(FocusDirection.Down) - } - }, - ) + fieldUiModel = fieldUiModel, + uiEventHandler = uiEventHandler, + intentHandler = intentHandler, + resources = resources, + focusManager = focusManager, + onNextClicked = { + if (index == section.fields.size - 1) { + onNextSection() + focusNext.value = true + } else { + focusManager.moveFocus(FocusDirection.Down) + } + }, + ) + } } }, ) } item(sections.size - 1) { - Spacer(modifier = Modifier.height(120.dp)) + Spacer(modifier = Modifier.height(Spacing.Spacing120)) } } + } else { + Column( + modifier = Modifier + .padding(Spacing.Spacing16), + ) { + InfoBar( + infoBarData = InfoBarData( + text = resources.getString(R.string.form_without_fields), + icon = { + Icon( + imageVector = Icons.Outlined.ErrorOutline, + contentDescription = "no fields", + tint = SurfaceColor.Warning, + ) + }, + color = SurfaceColor.Warning, + backgroundColor = SurfaceColor.WarningContainer, + actionText = null, + onClick = null, + ), + modifier = Modifier + .clip(shape = RoundedCornerShape(Radius.Full)) + .background(SurfaceColor.WarningContainer), + ) + } } }