From 1246b9e376a8aafc52907664b41f1bdd4274e72a Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Mon, 12 Dec 2022 18:34:26 +0300 Subject: [PATCH] android: Chat auto-scrolling behaviour (#1556) Co-authored-by: Evgeny Poberezkin <2769109+epoberezkin@users.noreply.github.com> --- .../main/java/chat/simplex/app/views/chat/ChatView.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt index 0c8d8ec749..0b3a55b140 100644 --- a/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt +++ b/apps/android/app/src/main/java/chat/simplex/app/views/chat/ChatView.kt @@ -617,17 +617,21 @@ private fun ScrollToBottom(chatId: ChatId, listState: LazyListState, chatItems: // Don't autoscroll next time until it will be needed shouldAutoScroll = false to chatId } + val scrollDistance = with(LocalDensity.current) { -39.dp.toPx() } /* * Since we use key with each item in LazyColumn, LazyColumn will not autoscroll to bottom item. We need to do it ourselves. - * When the first visible item (from bottom) is fully visible we can autoscroll to 0 item + * When the first visible item (from bottom) is visible (even partially) we can autoscroll to 0 item. Or just scrollBy small distance otherwise * */ LaunchedEffect(Unit) { snapshotFlow { chatItems.lastOrNull()?.id } .distinctUntilChanged() - .filter { listState.firstVisibleItemIndex == 0 && listState.firstVisibleItemScrollOffset == 0 } .filter { listState.layoutInfo.visibleItemsInfo.firstOrNull()?.key != it } .collect { - listState.animateScrollToItem(0) + if (listState.firstVisibleItemIndex == 0) { + listState.animateScrollToItem(0) + } else { + listState.animateScrollBy(scrollDistance) + } } } }