From 656c10976b2ca53dbc40306ab2495ace1ed94d24 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Sun, 16 Oct 2022 14:41:01 +0300 Subject: [PATCH] android: Fix autoscroll to bottom when going from group to direct (#1209) * android: Fix autoscroll to bottom when going from group to direct - and makes scrolling smoother because of -1 number of recompose * Rename --- .../chat/simplex/app/views/chat/ChatView.kt | 27 +++++++++++-------- 1 file changed, 16 insertions(+), 11 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 2849218d8e..1fc14e1027 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 @@ -48,7 +48,6 @@ import kotlinx.coroutines.flow.* import kotlinx.datetime.Clock import java.io.File import kotlin.math.sign -import kotlin.math.roundToInt @Composable fun ChatView(chatModel: ChatModel) { @@ -447,16 +446,7 @@ fun BoxWithConstraintsScope.ChatItemsList( val scope = rememberCoroutineScope() val uriHandler = LocalUriHandler.current val cxt = LocalContext.current - // Helps to scroll to bottom after moving from Group to Direct chat - // and prevents scrolling to bottom on orientation change - var shouldAutoScroll by rememberSaveable { mutableStateOf(true) } - LaunchedEffect(chat.chatInfo.apiId, chat.chatInfo.chatType, shouldAutoScroll) { - if (shouldAutoScroll && listState.firstVisibleItemIndex != 0) { - scope.launch { listState.scrollToItem(0) } - } - // Don't autoscroll next time until it will be needed - shouldAutoScroll = false - } + ScrollToBottom(chat.id, listState) var prevSearchEmptiness by rememberSaveable { mutableStateOf(searchValue.value.isEmpty()) } // Scroll to bottom when search value changes from something to nothing and back LaunchedEffect(searchValue.value.isEmpty()) { @@ -578,6 +568,21 @@ fun BoxWithConstraintsScope.ChatItemsList( FloatingButtons(chatItems, unreadCount, chat.chatStats.minUnreadItemId, searchValue, markRead, setFloatingButton, listState) } +@Composable +private fun ScrollToBottom(chatId: ChatId, listState: LazyListState) { + val scope = rememberCoroutineScope() + // Helps to scroll to bottom after moving from Group to Direct chat + // and prevents scrolling to bottom on orientation change + var shouldAutoScroll by rememberSaveable { mutableStateOf(true to chatId) } + LaunchedEffect(chatId, shouldAutoScroll) { + if ((shouldAutoScroll.first || shouldAutoScroll.second != chatId) && listState.firstVisibleItemIndex != 0) { + scope.launch { listState.scrollToItem(0) } + } + // Don't autoscroll next time until it will be needed + shouldAutoScroll = false to chatId + } +} + @Composable fun BoxWithConstraintsScope.FloatingButtons( chatItems: List,