From b93abb9555ee0765ec7151335f0467c3845dcd54 Mon Sep 17 00:00:00 2001 From: Diogo Date: Sun, 27 Oct 2024 15:47:21 +0000 Subject: [PATCH] jump to quote base --- .../chat/simplex/common/model/SimpleXAPI.kt | 10 +++++++-- .../simplex/common/views/chat/ChatSections.kt | 10 ++++++++- .../simplex/common/views/chat/ChatView.kt | 21 +++++++++++++++++-- .../common/views/chat/item/FramedItemView.kt | 8 ++++++- .../commonMain/resources/MR/base/strings.xml | 2 ++ 5 files changed, 45 insertions(+), 6 deletions(-) diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt index 29d45a3b9b..d7ebc86859 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt @@ -861,8 +861,12 @@ object ChatController { suspend fun apiGetChat(rh: Long?, type: ChatType, id: Long, pagination: ChatPagination = ChatPagination.Last(ChatPagination.INITIAL_COUNT), search: String = ""): Chat? { val r = sendCmd(rh, CC.ApiGetChat(type, id, pagination, search)) if (r is CR.ApiChat) return if (rh == null) r.chat else r.chat.copy(remoteHostId = rh) - Log.e(TAG, "apiGetChat bad response: ${r.responseType} ${r.details}") - AlertManager.shared.showAlertMsg(generalGetString(MR.strings.failed_to_parse_chat_title), generalGetString(MR.strings.contact_developers)) + if (r is CR.ChatCmdError && r.chatError is ChatError.ChatErrorStore && r.chatError.storeError is StoreError.ChatItemNotFound) { + AlertManager.shared.showAlertMsg(generalGetString(MR.strings.failed_to_get_chat_item_not_found_title), generalGetString(MR.strings.failed_to_get_chat_item_not_found_description)) + } else { + Log.e(TAG, "apiGetChat bad response: ${r.responseType} ${r.details}") + AlertManager.shared.showAlertMsg(generalGetString(MR.strings.failed_to_parse_chat_title), generalGetString(MR.strings.contact_developers)) + } return null } @@ -3461,11 +3465,13 @@ sealed class ChatPagination { class Last(val count: Int): ChatPagination() class After(val chatItemId: Long, val count: Int): ChatPagination() class Before(val chatItemId: Long, val count: Int): ChatPagination() + class Around(val chatItemId: Long, val count: Int): ChatPagination() val cmdString: String get() = when (this) { is Last -> "count=${this.count}" is After -> "after=${this.chatItemId} count=${this.count}" is Before -> "before=${this.chatItemId} count=${this.count}" + is Around -> "around=${this.chatItemId} count=${this.count}" } companion object { diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt index e295713cea..4691c80dff 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatSections.kt @@ -2,6 +2,7 @@ package chat.simplex.common.views.chat import androidx.compose.runtime.snapshots.SnapshotStateList import chat.simplex.common.model.* +import java.util.UUID data class SectionItems ( val mergeCategory: CIMergeCategory?, @@ -83,4 +84,11 @@ fun List.putIntoSections(revealedItems: Set): List } return sections -} \ No newline at end of file +} + +suspend fun apiLoadMessagesAroundItem(chatInfo: ChatInfo, chatModel: ChatModel, aroundItemId: Long, rhId: Long?) { + val pagination = ChatPagination.Around(aroundItemId, ChatPagination.PRELOAD_COUNT) + val chat = chatModel.controller.apiGetChat(rhId, chatInfo.chatType, chatInfo.apiId, pagination) ?: return + if (chatModel.chatId.value != chat.id) return + chatModel.chatItems.addAll(0, chat.chatItems) +} diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt index 7801ea2315..504e5af012 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/ChatView.kt @@ -983,8 +983,9 @@ fun BoxWithConstraintsScope.ChatItemsList( scope.launch { listState.scrollToItem(0) } } } + val preloadItems = remember { mutableStateOf(true) } - PreloadItems(chatInfo.id, listState, ChatPagination.UNTIL_PRELOAD_COUNT, loadPrevMessages) + PreloadItems(chatInfo.id, listState, ChatPagination.UNTIL_PRELOAD_COUNT, preloadItems.value, loadPrevMessages) Spacer(Modifier.size(8.dp)) val reversedChatItems by remember { derivedStateOf { chatModel.chatItems.asReversed() } } @@ -993,8 +994,21 @@ fun BoxWithConstraintsScope.ChatItemsList( val maxHeightRounded = with(LocalDensity.current) { maxHeight.roundToPx() } val scrollToItem: (Long) -> Unit = { itemId: Long -> val index = reversedChatItems.indexOfFirst { it.id == itemId } + println("here") if (index != -1) { scope.launch { listState.animateScrollToItem(kotlin.math.min(reversedChatItems.lastIndex, index + 1), -maxHeightRounded) } + } else { + preloadItems.value = false + withBGApi { + apiLoadMessagesAroundItem(rhId = remoteHostId, chatModel = chatModel, chatInfo = chatInfo, aroundItemId = itemId) + println("there") + + val idx = reversedChatItems.indexOfFirst { it.id == itemId } + scope.launch { listState.animateScrollToItem(kotlin.math.min(reversedChatItems.lastIndex, idx + 1), -maxHeightRounded) } +// } finally { +// preloadItems.value = true +// } + } } } // TODO: Having this block on desktop makes ChatItemsList() to recompose twice on chatModel.chatId update instead of once @@ -1419,6 +1433,7 @@ fun PreloadItems( chatId: String, listState: LazyListState, remaining: Int = 10, + enabled: Boolean, onLoadMore: (ChatId) -> Unit, ) { // Prevent situation when initial load and load more happens one after another after selecting a chat with long scroll position from previous selection @@ -1446,7 +1461,9 @@ fun PreloadItems( } .filter { it > 0 } .collect { - onLoadMore.value(chatId.value) + if (enabled) { + onLoadMore.value(chatId.value) + } } } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt index f346402957..04199e56e8 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/views/chat/item/FramedItemView.kt @@ -128,7 +128,13 @@ fun FramedItemView( .fillMaxWidth() .combinedClickable( onLongClick = { showMenu.value = true }, - onClick = { scrollToItem(qi.itemId?: return@combinedClickable) } + onClick = { + if (qi.itemId == null) { + AlertManager.shared.showAlertMsg(generalGetString(MR.strings.failed_to_get_chat_item_not_found_title), generalGetString(MR.strings.failed_to_get_chat_item_not_found_description)) + } else { + scrollToItem(qi.itemId) + } + } ) .onRightClick { showMenu.value = true } ) { diff --git a/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml b/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml index fd6988d5e8..a19bde3b21 100644 --- a/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml +++ b/apps/multiplatform/common/src/commonMain/resources/MR/base/strings.xml @@ -101,6 +101,8 @@ Error loading XFTP servers Error updating network configuration Failed to load chat + Message no longer available + The quoted message you are trying to access has been deleted. Failed to load chats Please update the app and contact developers. Error creating profile!