Skip to content

Commit 63aa296

Browse files
authored
fix: [ANDROAPP-5749] focus when changing section (#3463)
1 parent 47dfd5f commit 63aa296

File tree

1 file changed

+26
-5
lines changed
  • form/src/main/java/org/dhis2/form/ui

1 file changed

+26
-5
lines changed

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,18 @@ import androidx.compose.foundation.lazy.itemsIndexed
1515
import androidx.compose.foundation.lazy.rememberLazyListState
1616
import androidx.compose.runtime.Composable
1717
import androidx.compose.runtime.LaunchedEffect
18+
import androidx.compose.runtime.MutableState
1819
import androidx.compose.runtime.derivedStateOf
20+
import androidx.compose.runtime.mutableStateOf
1921
import androidx.compose.runtime.remember
2022
import androidx.compose.runtime.rememberCoroutineScope
2123
import androidx.compose.ui.Modifier
2224
import androidx.compose.ui.focus.FocusDirection
25+
import androidx.compose.ui.focus.FocusManager
2326
import androidx.compose.ui.graphics.Color
2427
import androidx.compose.ui.platform.LocalFocusManager
2528
import androidx.compose.ui.unit.dp
29+
import kotlinx.coroutines.CoroutineScope
2630
import kotlinx.coroutines.launch
2731
import org.dhis2.commons.resources.ResourceManager
2832
import org.dhis2.form.model.FieldUiModel
@@ -55,6 +59,7 @@ fun Form(
5559
}
5660
}
5761
}
62+
val focusNext = remember { mutableStateOf(false) }
5863
LazyColumn(
5964
modifier = Modifier
6065
.fillMaxSize()
@@ -75,11 +80,10 @@ fun Form(
7580
val isSectionOpen = remember(section.state) {
7681
derivedStateOf { section.state == SectionState.OPEN }
7782
}
78-
LaunchedEffect(isSectionOpen.value) {
79-
if (isSectionOpen.value) {
80-
scrollState.animateScrollToItem(sections.indexOf(section))
81-
focusManager.moveFocus(FocusDirection.Next)
82-
}
83+
84+
LaunchIfTrue(isSectionOpen.value) {
85+
scrollState.animateScrollToItem(sections.indexOf(section))
86+
focusManager.moveFocusNext(focusNext)
8387
}
8488

8589
val onNextSection: () -> Unit = {
@@ -124,6 +128,7 @@ fun Form(
124128
onNextClicked = {
125129
if (index == section.fields.size - 1) {
126130
onNextSection()
131+
focusNext.value = true
127132
} else {
128133
focusManager.moveFocus(FocusDirection.Down)
129134
}
@@ -140,6 +145,22 @@ fun Form(
140145
}
141146
}
142147

148+
private fun FocusManager.moveFocusNext(focusNext: MutableState<Boolean>) {
149+
if (focusNext.value) {
150+
this.moveFocus(FocusDirection.Next)
151+
focusNext.value = false
152+
}
153+
}
154+
155+
@Composable
156+
private fun LaunchIfTrue(key: Boolean, block: suspend CoroutineScope.() -> Unit) {
157+
LaunchedEffect(key) {
158+
if (key) {
159+
block()
160+
}
161+
}
162+
}
163+
143164
private fun getNextSection(section: FormSection, sections: List<FormSection>): FormSection? {
144165
val currentIndex = sections.indexOf(section)
145166
if (currentIndex != -1 && currentIndex < sections.size - 1) {

0 commit comments

Comments
 (0)