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,