From 1144cee572455626a4cf883983403084d4bc77f5 Mon Sep 17 00:00:00 2001 From: Marco Romano Date: Wed, 13 Sep 2023 11:02:25 +0200 Subject: [PATCH] Poll Creation: Switch focus to newly added option field when clicking "Add option". - Also scrolls the list to make the "Add option" button visible for further additions. --- .../features/poll/impl/create/CreatePollView.kt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/features/poll/impl/src/main/kotlin/io/element/android/features/poll/impl/create/CreatePollView.kt b/features/poll/impl/src/main/kotlin/io/element/android/features/poll/impl/create/CreatePollView.kt index e69d23fd32..462ff4e48a 100644 --- a/features/poll/impl/src/main/kotlin/io/element/android/features/poll/impl/create/CreatePollView.kt +++ b/features/poll/impl/src/main/kotlin/io/element/android/features/poll/impl/create/CreatePollView.kt @@ -26,6 +26,7 @@ import androidx.compose.foundation.layout.imePadding 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.text.KeyboardOptions import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Add @@ -77,6 +78,7 @@ fun CreatePollView( onDismiss = { state.eventSink(CreatePollEvents.HideConfirmation) } ) val questionFocusRequester = remember { FocusRequester() } + val answerFocusRequester = remember { FocusRequester() } LaunchedEffect(Unit) { questionFocusRequester.requestFocus() } @@ -103,12 +105,14 @@ fun CreatePollView( ) }, ) { paddingValues -> + val lazyListState = rememberLazyListState() LazyColumn( modifier = Modifier .padding(paddingValues) .consumeWindowInsets(paddingValues) .imePadding() .fillMaxSize(), + state = lazyListState, ) { item { Column { @@ -137,6 +141,8 @@ fun CreatePollView( } } itemsIndexed(state.answers) { index, answer -> + val isLastItem = index == state.answers.size - 1 + val hasAdditionalOptions = state.answers.size > 2 ListItem( headlineContent = { OutlinedTextField( @@ -144,7 +150,9 @@ fun CreatePollView( onValueChange = { state.eventSink(CreatePollEvents.SetAnswer(index, it)) }, - modifier = Modifier.fillMaxWidth(), + modifier = Modifier + .then(if (isLastItem) Modifier.focusRequester(answerFocusRequester) else Modifier) + .fillMaxWidth(), placeholder = { Text(text = stringResource(id = R.string.screen_create_poll_answer_hint, index + 1)) }, @@ -162,6 +170,10 @@ fun CreatePollView( }, style = if (answer.canDelete) ListItemStyle.Destructive else ListItemStyle.Default, ) + LaunchedEffect(isLastItem, hasAdditionalOptions) { + lazyListState.animateScrollToItem(state.answers.size + 1) + if (isLastItem && hasAdditionalOptions) answerFocusRequester.requestFocus() + } } if (state.canAddAnswer) { item {