Skip to content

Commit d1f126a

Browse files
committed
[ANDROAPP-4826] Show no fields warnings if one section and is NO_HEADER
1 parent b312dfe commit d1f126a

File tree

3 files changed

+132
-112
lines changed

3 files changed

+132
-112
lines changed

form/src/main/java/org/dhis2/form/ui/Form.kt

Lines changed: 116 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -72,121 +72,133 @@ fun Form(
7272
}
7373
}
7474
}
75-
if (sections.isNotEmpty()) {
76-
val focusNext = remember { mutableStateOf(false) }
77-
LazyColumn(
78-
modifier = Modifier
79-
.fillMaxSize()
80-
.background(Color.White)
81-
.clickable(
82-
interactionSource = MutableInteractionSource(),
83-
indication = null,
84-
onClick = { focusManager.clearFocus() },
85-
),
86-
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 16.dp),
87-
state = scrollState,
88-
) {
89-
this.itemsIndexed(
90-
items = sections,
91-
key = { _, fieldUiModel -> fieldUiModel.uid },
92-
) { _, section ->
93-
val isSectionOpen = remember(section.state) {
94-
derivedStateOf { section.state == SectionState.OPEN }
95-
}
75+
val focusNext = remember { mutableStateOf(false) }
76+
LazyColumn(
77+
modifier = Modifier
78+
.fillMaxSize()
79+
.background(Color.White)
80+
.clickable(
81+
interactionSource = MutableInteractionSource(),
82+
indication = null,
83+
onClick = { focusManager.clearFocus() },
84+
),
85+
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 16.dp),
86+
state = scrollState,
87+
) {
88+
this.itemsIndexed(
89+
items = sections,
90+
key = { _, fieldUiModel -> fieldUiModel.uid },
91+
) { _, section ->
92+
val isSectionOpen = remember(section.state) {
93+
derivedStateOf { section.state == SectionState.OPEN }
94+
}
9695

97-
LaunchIfTrue(isSectionOpen.value) {
98-
scrollState.animateScrollToItem(sections.indexOf(section))
99-
focusManager.moveFocusNext(focusNext)
100-
}
96+
LaunchIfTrue(isSectionOpen.value) {
97+
scrollState.animateScrollToItem(sections.indexOf(section))
98+
focusManager.moveFocusNext(focusNext)
99+
}
101100

102-
val onNextSection: () -> Unit = {
103-
getNextSection(section, sections)?.let {
104-
intentHandler.invoke(FormIntent.OnSection(it.uid))
105-
scope.launch {
106-
scrollState.animateScrollToItem(sections.indexOf(it))
107-
}
108-
} ?: run {
109-
focusManager.clearFocus()
101+
val onNextSection: () -> Unit = {
102+
getNextSection(section, sections)?.let {
103+
intentHandler.invoke(FormIntent.OnSection(it.uid))
104+
scope.launch {
105+
scrollState.animateScrollToItem(sections.indexOf(it))
110106
}
107+
} ?: run {
108+
focusManager.clearFocus()
111109
}
110+
}
112111

113-
val sectionMessage = if (section.fields.isEmpty()) resources.getString(R.string.form_without_fields) else null
114-
115-
val sectionState = if (section.fields.isEmpty()) SectionState.FIXED else section.state
112+
val sectionMessage =
113+
if (section.fields.isEmpty()) resources.getString(R.string.form_without_fields) else null
116114

117-
Section(
118-
title = section.title,
119-
isLastSection = getNextSection(section, sections) == null,
120-
description = if (section.fields.isNotEmpty()) section.description else null,
121-
completedFields = section.completedFields(),
122-
totalFields = section.fields.size,
123-
state = sectionState,
124-
errorCount = section.errorCount(),
125-
warningCount = section.warningCount(),
126-
warningMessage = sectionMessage,
127-
onNextSection = onNextSection,
128-
onSectionClick = {
129-
intentHandler.invoke(FormIntent.OnSection(section.uid))
130-
},
131-
content = {
132-
if (section.fields.isNotEmpty()) {
133-
section.fields.forEachIndexed { index, fieldUiModel ->
134-
fieldUiModel.setCallback(callback)
135-
FieldProvider(
136-
modifier = Modifier.animateItemPlacement(
137-
animationSpec = tween(
138-
durationMillis = 500,
139-
easing = LinearOutSlowInEasing,
140-
),
115+
Section(
116+
title = section.title,
117+
isLastSection = getNextSection(section, sections) == null,
118+
description = if (section.fields.isNotEmpty()) section.description else null,
119+
completedFields = section.completedFields(),
120+
totalFields = section.fields.size,
121+
state = section.state,
122+
errorCount = section.errorCount(),
123+
warningCount = section.warningCount(),
124+
warningMessage = sectionMessage,
125+
onNextSection = onNextSection,
126+
onSectionClick = {
127+
intentHandler.invoke(FormIntent.OnSection(section.uid))
128+
},
129+
content = {
130+
if (section.fields.isNotEmpty()) {
131+
section.fields.forEachIndexed { index, fieldUiModel ->
132+
fieldUiModel.setCallback(callback)
133+
FieldProvider(
134+
modifier = Modifier.animateItemPlacement(
135+
animationSpec = tween(
136+
durationMillis = 500,
137+
easing = LinearOutSlowInEasing,
141138
),
142-
fieldUiModel = fieldUiModel,
143-
uiEventHandler = uiEventHandler,
144-
intentHandler = intentHandler,
145-
resources = resources,
146-
focusManager = focusManager,
147-
onNextClicked = {
148-
if (index == section.fields.size - 1) {
149-
onNextSection()
150-
focusNext.value = true
151-
} else {
152-
focusManager.moveFocus(FocusDirection.Down)
153-
}
154-
},
155-
)
156-
}
139+
),
140+
fieldUiModel = fieldUiModel,
141+
uiEventHandler = uiEventHandler,
142+
intentHandler = intentHandler,
143+
resources = resources,
144+
focusManager = focusManager,
145+
onNextClicked = {
146+
if (index == section.fields.size - 1) {
147+
onNextSection()
148+
focusNext.value = true
149+
} else {
150+
focusManager.moveFocus(FocusDirection.Down)
151+
}
152+
},
153+
)
157154
}
158-
},
159-
)
160-
}
161-
item(sections.size - 1) {
162-
Spacer(modifier = Modifier.height(Spacing.Spacing120))
163-
}
155+
}
156+
},
157+
)
164158
}
159+
item(sections.size - 1) {
160+
Spacer(modifier = Modifier.height(Spacing.Spacing120))
161+
}
162+
}
163+
if (shouldDisplayNoFieldsWarning(sections)) {
164+
NoFieldsWarning(resources)
165+
}
166+
}
167+
168+
fun shouldDisplayNoFieldsWarning(sections: List<FormSection>): Boolean {
169+
return if (sections.size == 1) {
170+
val section = sections.first()
171+
section.state == SectionState.NO_HEADER && section.fields.isEmpty()
165172
} else {
166-
Column(
173+
false
174+
}
175+
}
176+
177+
@Composable
178+
fun NoFieldsWarning(resources: ResourceManager) {
179+
Column(
180+
modifier = Modifier
181+
.padding(Spacing.Spacing16),
182+
) {
183+
InfoBar(
184+
infoBarData = InfoBarData(
185+
text = resources.getString(R.string.form_without_fields),
186+
icon = {
187+
Icon(
188+
imageVector = Icons.Outlined.ErrorOutline,
189+
contentDescription = "no fields",
190+
tint = SurfaceColor.Warning,
191+
)
192+
},
193+
color = SurfaceColor.Warning,
194+
backgroundColor = SurfaceColor.WarningContainer,
195+
actionText = null,
196+
onClick = null,
197+
),
167198
modifier = Modifier
168-
.padding(Spacing.Spacing16),
169-
) {
170-
InfoBar(
171-
infoBarData = InfoBarData(
172-
text = resources.getString(R.string.form_without_fields),
173-
icon = {
174-
Icon(
175-
imageVector = Icons.Outlined.ErrorOutline,
176-
contentDescription = "no fields",
177-
tint = SurfaceColor.Warning,
178-
)
179-
},
180-
color = SurfaceColor.Warning,
181-
backgroundColor = SurfaceColor.WarningContainer,
182-
actionText = null,
183-
onClick = null,
184-
),
185-
modifier = Modifier
186-
.clip(shape = RoundedCornerShape(Radius.Full))
187-
.background(SurfaceColor.WarningContainer),
188-
)
189-
}
199+
.clip(shape = RoundedCornerShape(Radius.Full))
200+
.background(SurfaceColor.WarningContainer),
201+
)
190202
}
191203
}
192204

form/src/main/java/org/dhis2/form/ui/FormView.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,14 +365,12 @@ class FormView : Fragment() {
365365
val sections = items?.let {
366366
formSectionMapper.mapFromFieldUiModelList(it)
367367
} ?: emptyList()
368-
if (viewModel.loading.value == false) {
369-
Form(
370-
sections = sections,
371-
intentHandler = ::intentHandler,
372-
uiEventHandler = ::uiEventHandler,
373-
resources = Injector.provideResourcesManager(context),
374-
)
375-
}
368+
Form(
369+
sections = sections,
370+
intentHandler = ::intentHandler,
371+
uiEventHandler = ::uiEventHandler,
372+
resources = Injector.provideResourcesManager(context),
373+
)
376374
}
377375
}
378376
} else {

form/src/main/java/org/dhis2/form/ui/mapper/FormSectionMapper.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ class FormSectionMapper {
2929
)
3030
}
3131
}
32+
} else {
33+
sections.add(
34+
FormSection(
35+
uid = "DUMMY",
36+
title = "TITLE",
37+
description = null,
38+
state = SectionState.NO_HEADER,
39+
fields = items.filterIsInstance<FieldUiModelImpl>(),
40+
),
41+
)
3242
}
3343
return sections
3444
}

0 commit comments

Comments
 (0)