From 9cd7a7fdb0a49430580caf682e5a94e4e22b1dc9 Mon Sep 17 00:00:00 2001 From: Stanislav Dmitrenko <7953703+avently@users.noreply.github.com> Date: Mon, 22 Aug 2022 16:02:46 +0300 Subject: [PATCH] Re-apply new chat instance on chatId changes (#962) * Re-apply new chat instance on chatId changes * Fixed incorrectly calculated counters on floating buttons * Show chat at the bottom of a view instead of at the top --- .../chat/simplex/app/views/chat/ChatView.kt | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 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 841c858732..095f3aec15 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 @@ -56,6 +56,22 @@ fun ChatView(chatModel: ChatModel) { val attachmentBottomSheetState = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden) val scope = rememberCoroutineScope() + LaunchedEffect(Unit) { + // snapshotFlow here is because it reacts much faster on changes in chatModel.chatId.value. + // With LaunchedEffect(chatModel.chatId.value) there is a noticeable delay before reconstruction of the view + snapshotFlow { chatModel.chatId.value } + .distinctUntilChanged() + .collect { + activeChat = if (chatModel.chatId.value == null) { + null + } else { + // Redisplay the whole hierarchy if the chat is different to make going from groups to direct chat working correctly + // Also for situation when chatId changes after clicking in notification, etc + chatModel.getChat(chatModel.chatId.value!!) + } + } + } + if (activeChat == null || user == null) { chatModel.chatId.value = null } else { @@ -121,11 +137,7 @@ fun ChatView(chatModel: ChatModel) { it.chatInfo is ChatInfo.Direct && it.chatInfo.contact.contactId == contactId } if (c != null) { - withApi { - openChat(c.chatInfo, chatModel) - // Redisplay the whole hierarchy if the chat is different to make going from groups to direct chat working correctly - activeChat = c - } + withApi { openChat(c.chatInfo, chatModel) } } }, loadPrevMessages = { cInfo -> @@ -262,7 +274,7 @@ fun ChatLayout( modifier = Modifier.navigationBarsWithImePadding(), floatingActionButton = { floatingButton.value() }, ) { contentPadding -> - BoxWithConstraints(Modifier.padding(contentPadding)) { + BoxWithConstraints(Modifier.fillMaxHeight().padding(contentPadding)) { ChatItemsList( user, chat, unreadCount, composeState, chatItems, searchValue, useLinkPreviews, openDirectChat, loadPrevMessages, deleteMessage, @@ -447,7 +459,7 @@ fun BoxWithConstraintsScope.ChatItemsList( Spacer(Modifier.size(8.dp)) val reversedChatItems by remember { derivedStateOf { chatItems.reversed() } } - LazyColumn(state = listState, reverseLayout = true) { + LazyColumn(Modifier.align(Alignment.BottomCenter), state = listState, reverseLayout = true) { itemsIndexed(reversedChatItems) { i, cItem -> CompositionLocalProvider( // Makes horizontal and vertical scrolling to coexist nicely. @@ -558,6 +570,11 @@ fun BoxWithConstraintsScope.FloatingButtons( firstVisibleIndex = it firstItemIsVisible = firstVisibleIndex == 0 } + } + + LaunchedEffect(listState) { + // When both snapshotFlows located in one LaunchedEffect second block will never be called because coroutine is paused on first block + // so separate them into two LaunchedEffects snapshotFlow { listState.layoutInfo.visibleItemsInfo.lastIndex } .distinctUntilChanged() .collect {