@@ -72,121 +72,133 @@ fun Form(
72
72
}
73
73
}
74
74
}
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
+ }
96
95
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
+ }
101
100
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))
110
106
}
107
+ } ? : run {
108
+ focusManager.clearFocus()
111
109
}
110
+ }
112
111
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
116
114
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 ,
141
138
),
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
+ )
157
154
}
158
- },
159
- )
160
- }
161
- item(sections.size - 1 ) {
162
- Spacer (modifier = Modifier .height(Spacing .Spacing120 ))
163
- }
155
+ }
156
+ },
157
+ )
164
158
}
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()
165
172
} 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
+ ),
167
198
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
+ )
190
202
}
191
203
}
192
204
0 commit comments