android, desktop: scroll to quoted item without known id (#5254)

This commit is contained in:
Stanislav Dmitrenko
2024-11-26 14:22:24 +00:00
committed by GitHub
parent 25893177d0
commit 8c1abcccfb
4 changed files with 42 additions and 6 deletions
@@ -10,6 +10,16 @@ import kotlin.math.min
const val TRIM_KEEP_COUNT = 200
suspend fun apiLoadSingleMessage(
rhId: Long?,
chatType: ChatType,
apiId: Long,
itemId: Long
): ChatItem? = coroutineScope {
val (chat, _) = chatModel.controller.apiGetChat(rhId, chatType, apiId, ChatPagination.Around(itemId, 0), "") ?: return@coroutineScope null
chat.chatItems.firstOrNull()
}
suspend fun apiLoadMessages(
rhId: Long?,
chatType: ChatType,
@@ -979,10 +979,12 @@ fun BoxScope.ChatItemsList(
}
}
val remoteHostIdUpdated = rememberUpdatedState(remoteHostId)
val chatInfoUpdated = rememberUpdatedState(chatInfo)
val highlightedItems = remember { mutableStateOf(setOf<Long>()) }
val scope = rememberCoroutineScope()
val scrollToItem: (Long) -> Unit = remember { scrollToItem(loadingMoreItems, highlightedItems, chatInfoUpdated, maxHeight, scope, reversedChatItems, mergedItems, listState, loadMessages) }
val scrollToQuotedItemFromItem: (Long) -> Unit = remember { findQuotedItemFromItem(remoteHostIdUpdated, chatInfoUpdated, scope, scrollToItem) }
LoadLastItems(loadingMoreItems, remoteHostId, chatInfo)
SmallScrollOnNewMessage(listState, chatModel.chatItems)
@@ -1042,7 +1044,7 @@ fun BoxScope.ChatItemsList(
highlightedItems.value = setOf()
}
}
ChatItemView(remoteHostId, chatInfo, cItem, composeState, provider, useLinkPreviews = useLinkPreviews, linkMode = linkMode, revealed = revealed, highlighted = highlighted, range = range, fillMaxWidth = fillMaxWidth, selectedChatItems = selectedChatItems, selectChatItem = { selectUnselectChatItem(true, cItem, revealed, selectedChatItems) }, deleteMessage = deleteMessage, deleteMessages = deleteMessages, receiveFile = receiveFile, cancelFile = cancelFile, joinGroup = joinGroup, acceptCall = acceptCall, acceptFeature = acceptFeature, openDirectChat = openDirectChat, forwardItem = forwardItem, updateContactStats = updateContactStats, updateMemberStats = updateMemberStats, syncContactConnection = syncContactConnection, syncMemberConnection = syncMemberConnection, findModelChat = findModelChat, findModelMember = findModelMember, scrollToItem = scrollToItem, setReaction = setReaction, showItemDetails = showItemDetails, reveal = reveal, developerTools = developerTools, showViaProxy = showViaProxy, itemSeparation = itemSeparation, showTimestamp = itemSeparation.timestamp)
ChatItemView(remoteHostId, chatInfo, cItem, composeState, provider, useLinkPreviews = useLinkPreviews, linkMode = linkMode, revealed = revealed, highlighted = highlighted, range = range, fillMaxWidth = fillMaxWidth, selectedChatItems = selectedChatItems, selectChatItem = { selectUnselectChatItem(true, cItem, revealed, selectedChatItems) }, deleteMessage = deleteMessage, deleteMessages = deleteMessages, receiveFile = receiveFile, cancelFile = cancelFile, joinGroup = joinGroup, acceptCall = acceptCall, acceptFeature = acceptFeature, openDirectChat = openDirectChat, forwardItem = forwardItem, updateContactStats = updateContactStats, updateMemberStats = updateMemberStats, syncContactConnection = syncContactConnection, syncMemberConnection = syncMemberConnection, findModelChat = findModelChat, findModelMember = findModelMember, scrollToItem = scrollToItem, scrollToQuotedItemFromItem = scrollToQuotedItemFromItem, setReaction = setReaction, showItemDetails = showItemDetails, reveal = reveal, developerTools = developerTools, showViaProxy = showViaProxy, itemSeparation = itemSeparation, showTimestamp = itemSeparation.timestamp)
}
}
@@ -1867,6 +1869,27 @@ private fun scrollToItem(
}
}
private fun findQuotedItemFromItem(
rhId: State<Long?>,
chatInfo: State<ChatInfo>,
scope: CoroutineScope,
scrollToItem: (Long) -> Unit
): (Long) -> Unit = { itemId: Long ->
scope.launch(Dispatchers.Default) {
val item = apiLoadSingleMessage(rhId.value, chatInfo.value.chatType, chatInfo.value.apiId, itemId)
if (item != null) {
withChats {
updateChatItem(chatInfo.value, item)
}
if (item.quotedItem?.itemId != null) {
scrollToItem(item.quotedItem.itemId)
} else {
showQuotedItemDoesNotExistAlert()
}
}
}
}
val chatViewScrollState = MutableStateFlow(false)
fun addGroupMembers(groupInfo: GroupInfo, rhId: Long?, view: Any? = null, close: (() -> Unit)? = null) {
@@ -71,6 +71,7 @@ fun ChatItemView(
joinGroup: (Long, () -> Unit) -> Unit,
acceptCall: (Contact) -> Unit,
scrollToItem: (Long) -> Unit,
scrollToQuotedItemFromItem: (Long) -> Unit,
acceptFeature: (Contact, ChatFeature, Int?) -> Unit,
openDirectChat: (Long) -> Unit,
forwardItem: (ChatInfo, ChatItem) -> Unit,
@@ -155,7 +156,7 @@ fun ChatItemView(
) {
@Composable
fun framedItemView() {
FramedItemView(cInfo, cItem, uriHandler, imageProvider, linkMode = linkMode, showViaProxy = showViaProxy, showMenu, showTimestamp = showTimestamp, tailVisible = itemSeparation.largeGap, receiveFile, onLinkLongClick, scrollToItem)
FramedItemView(cInfo, cItem, uriHandler, imageProvider, linkMode = linkMode, showViaProxy = showViaProxy, showMenu, showTimestamp = showTimestamp, tailVisible = itemSeparation.largeGap, receiveFile, onLinkLongClick, scrollToItem, scrollToQuotedItemFromItem)
}
fun deleteMessageQuestionText(): String {
@@ -1087,6 +1088,7 @@ fun PreviewChatItemView(
joinGroup = { _, _ -> },
acceptCall = { _ -> },
scrollToItem = {},
scrollToQuotedItemFromItem = {},
acceptFeature = { _, _, _ -> },
openDirectChat = { _ -> },
forwardItem = { _, _ -> },
@@ -1130,6 +1132,7 @@ fun PreviewChatItemViewDeletedContent() {
joinGroup = { _, _ -> },
acceptCall = { _ -> },
scrollToItem = {},
scrollToQuotedItemFromItem = {},
acceptFeature = { _, _, _ -> },
openDirectChat = { _ -> },
forwardItem = { _, _ -> },
@@ -39,6 +39,7 @@ fun FramedItemView(
receiveFile: (Long) -> Unit,
onLinkLongClick: (link: String) -> Unit = {},
scrollToItem: (Long) -> Unit = {},
scrollToQuotedItemFromItem: (Long) -> Unit = {},
) {
val sent = ci.chatDir.sent
val chatTTL = chatInfo.timedMessagesTTL
@@ -130,11 +131,10 @@ fun FramedItemView(
.combinedClickable(
onLongClick = { showMenu.value = true },
onClick = {
val itemId = qi.itemId
if (itemId != null) {
scrollToItem(itemId)
if (qi.itemId != null) {
scrollToItem(qi.itemId)
} else {
showQuotedItemDoesNotExistAlert()
scrollToQuotedItemFromItem(ci.id)
}
}
)