android, desktop: go to forwarded item or search result (#5666)

* android, desktop: go to forwarded item or search result

* changes

* reactions back

* button appearance

* indentation

* change

* rename variable

* rename function

* rename variable

* rename variable

* fix scroll position
This commit is contained in:
Stanislav Dmitrenko 2025-03-01 01:55:49 +07:00 committed by GitHub
parent 1b757911fa
commit 50232fd179
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 591 additions and 448 deletions

View file

@ -65,6 +65,7 @@ object ChatModel {
// current chat
val chatId = mutableStateOf<String?>(null)
val openAroundItemId: MutableState<Long?> = mutableStateOf(null)
val chatsContext = ChatsContext(null)
val reportsChatsContext = ChatsContext(MsgContentTag.Report)
// declaration of chatsContext should be before any other variable that is taken from ChatsContext class and used in the model, otherwise, strange crash with NullPointerException for "this" parameter in random functions
@ -3111,6 +3112,13 @@ sealed class CIForwardedFrom {
is Group -> chatName
}
val chatTypeApiIdMsgId: Triple<ChatType, Long, Long?>?
get() = when (this) {
Unknown -> null
is Contact -> if (contactId != null) Triple(ChatType.Direct, contactId, chatItemId) else null
is Group -> if (groupId != null) Triple(ChatType.Group, groupId, chatItemId) else null
}
fun text(chatType: ChatType): String =
if (chatType == ChatType.Local) {
if (chatName.isEmpty()) {

View file

@ -1398,7 +1398,8 @@ private suspend fun afterSetChatTTL(rhId: Long?, chatInfo: ChatInfo, progressInd
chat,
navInfo,
contentTag = null,
pagination = pagination
pagination = pagination,
openAroundItemId = null
)
} catch (e: Exception) {
Log.e(TAG, "apiGetChat error: ${e.stackTraceToString()}")

View file

@ -28,13 +28,15 @@ suspend fun apiLoadMessages(
contentTag: MsgContentTag?,
pagination: ChatPagination,
search: String = "",
openAroundItemId: Long? = null,
visibleItemIndexesNonReversed: () -> IntRange = { 0 .. 0 }
) = coroutineScope {
val (chat, navInfo) = chatModel.controller.apiGetChat(rhId, chatType, apiId, contentTag, pagination, search) ?: return@coroutineScope
// For .initial allow the chatItems to be empty as well as chatModel.chatId to not match this chat because these values become set after .initial finishes
if (((chatModel.chatId.value != chat.id || chat.chatItems.isEmpty()) && pagination !is ChatPagination.Initial && pagination !is ChatPagination.Last)
/** When [openAroundItemId] is provided, chatId can be different too */
if (((chatModel.chatId.value != chat.id || chat.chatItems.isEmpty()) && pagination !is ChatPagination.Initial && pagination !is ChatPagination.Last && openAroundItemId == null)
|| !isActive) return@coroutineScope
processLoadedChat(chat, navInfo, contentTag, pagination, visibleItemIndexesNonReversed)
processLoadedChat(chat, navInfo, contentTag, pagination, openAroundItemId, visibleItemIndexesNonReversed)
}
suspend fun processLoadedChat(
@ -42,6 +44,7 @@ suspend fun processLoadedChat(
navInfo: NavigationInfo,
contentTag: MsgContentTag?,
pagination: ChatPagination,
openAroundItemId: Long?,
visibleItemIndexesNonReversed: () -> IntRange = { 0 .. 0 }
) {
val chatState = chatModel.chatStateForContent(contentTag)
@ -67,7 +70,7 @@ suspend fun processLoadedChat(
withChats(contentTag) {
chatItemStatuses.clear()
chatItems.replaceAll(chat.chatItems)
chatModel.chatId.value = chat.chatInfo.id
chatModel.chatId.value = chat.id
splits.value = newSplits
if (chat.chatItems.isNotEmpty()) {
unreadAfterItemId.value = chat.chatItems.last().id
@ -119,10 +122,15 @@ suspend fun processLoadedChat(
}
}
is ChatPagination.Around -> {
newItems.addAll(oldItems)
val newSplits = removeDuplicatesAndUpperSplits(newItems, chat, splits, visibleItemIndexesNonReversed)
val newSplits = if (openAroundItemId == null) {
newItems.addAll(oldItems)
removeDuplicatesAndUpperSplits(newItems, chat, splits, visibleItemIndexesNonReversed)
} else {
emptyList()
}
// currently, items will always be added on top, which is index 0
newItems.addAll(0, chat.chatItems)
withChats(contentTag) {
chatItems.replaceAll(newItems)
splits.value = listOf(chat.chatItems.last().id) + newSplits
@ -130,8 +138,15 @@ suspend fun processLoadedChat(
totalAfter.value = navInfo.afterTotal
unreadTotal.value = chat.chatStats.unreadCount
unreadAfter.value = navInfo.afterUnread
// no need to set it, count will be wrong
// unreadAfterNewestLoaded.value = navInfo.afterUnread
if (openAroundItemId != null) {
unreadAfterNewestLoaded.value = navInfo.afterUnread
chatModel.openAroundItemId.value = openAroundItemId
chatModel.chatId.value = chat.id
} else {
// no need to set it, count will be wrong
// unreadAfterNewestLoaded.value = navInfo.afterUnread
}
}
}
is ChatPagination.Last -> {

View file

@ -133,9 +133,12 @@ fun ChatView(
SimpleXThemeOverride(overrides ?: CurrentColors.collectAsState().value) {
val onSearchValueChanged: (String) -> Unit = onSearchValueChanged@{ value ->
if (searchText.value == value) return@onSearchValueChanged
val c = chatModel.getChat(chatInfo.id) ?: return@onSearchValueChanged
if (chatModel.chatId.value != chatInfo.id) return@onSearchValueChanged
val sameText = searchText.value == value
// showSearch can be false with empty text when it was closed manually after clicking on message from search to load .around it
// (required on Android to have this check to prevent call to search with old text)
val emptyAndClosedSearch = searchText.value.isEmpty() && !showSearch.value && contentTag == null
val c = chatModel.getChat(chatInfo.id)
if (sameText || emptyAndClosedSearch || c == null || chatModel.chatId.value != chatInfo.id) return@onSearchValueChanged
withBGApi {
apiFindMessages(c, value, contentTag)
searchText.value = value
@ -344,7 +347,7 @@ fun ChatView(
val c = chatModel.getChat(chatId)
if (chatModel.chatId.value != chatId) return@ChatLayout
if (c != null) {
apiLoadMessages(c.remoteHostId, c.chatInfo.chatType, c.chatInfo.apiId, contentTag, pagination, searchText.value, visibleItemIndexes)
apiLoadMessages(c.remoteHostId, c.chatInfo.chatType, c.chatInfo.apiId, contentTag, pagination, searchText.value, null, visibleItemIndexes)
}
},
deleteMessage = { itemId, mode ->
@ -602,6 +605,10 @@ fun ChatView(
},
changeNtfsState = { enabled, currentValue -> toggleNotifications(chatRh, chatInfo, enabled, chatModel, currentValue) },
onSearchValueChanged = onSearchValueChanged,
closeSearch = {
showSearch.value = false
searchText.value = ""
},
onComposed,
developerTools = chatModel.controller.appPrefs.developerTools.get(),
showViaProxy = chatModel.controller.appPrefs.showSentViaProxy.get(),
@ -699,6 +706,7 @@ fun ChatLayout(
markChatRead: () -> Unit,
changeNtfsState: (MsgFilter, currentValue: MutableState<MsgFilter>) -> Unit,
onSearchValueChanged: (String) -> Unit,
closeSearch: () -> Unit,
onComposed: suspend (chatId: String) -> Unit,
developerTools: Boolean,
showViaProxy: Boolean,
@ -751,7 +759,7 @@ fun ChatLayout(
useLinkPreviews, linkMode, scrollToItemId, selectedChatItems, showMemberInfo, showChatInfo = info, loadMessages, deleteMessage, deleteMessages, archiveReports,
receiveFile, cancelFile, joinGroup, acceptCall, acceptFeature, openDirectChat, forwardItem,
updateContactStats, updateMemberStats, syncContactConnection, syncMemberConnection, findModelChat, findModelMember,
setReaction, showItemDetails, markItemsRead, markChatRead, remember { { onComposed(it) } }, developerTools, showViaProxy,
setReaction, showItemDetails, markItemsRead, markChatRead, closeSearch, remember { { onComposed(it) } }, developerTools, showViaProxy,
)
}
if (chatInfo is ChatInfo.Group && composeState.value.message.text.isNotEmpty()) {
@ -1160,11 +1168,13 @@ fun BoxScope.ChatItemsList(
showItemDetails: (ChatInfo, ChatItem) -> Unit,
markItemsRead: (List<Long>) -> Unit,
markChatRead: () -> Unit,
closeSearch: () -> Unit,
onComposed: suspend (chatId: String) -> Unit,
developerTools: Boolean,
showViaProxy: Boolean
) {
val searchValueIsEmpty = remember { derivedStateOf { searchValue.value.isEmpty() } }
val searchValueIsNotBlank = remember { derivedStateOf { searchValue.value.isNotBlank() } }
val revealedItems = rememberSaveable(stateSaver = serializableSaver()) { mutableStateOf(setOf<Long>()) }
val contentTag = LocalContentTag.current
// not using reversedChatItems inside to prevent possible derivedState bug in Compose when one derived state access can cause crash asking another derived state
@ -1177,15 +1187,29 @@ fun BoxScope.ChatItemsList(
val reportsCount = reportsCount(chatInfo.id)
val topPaddingToContent = topPaddingToContent(chatView = contentTag == null, contentTag == null && reportsCount > 0)
val topPaddingToContentPx = rememberUpdatedState(with(LocalDensity.current) { topPaddingToContent.roundToPx() })
val numberOfBottomAppBars = numberOfBottomAppBars()
/** determines height based on window info and static height of two AppBars. It's needed because in the first graphic frame height of
* [composeViewHeight] is unknown, but we need to set scroll position for unread messages already so it will be correct before the first frame appears
* */
val maxHeightForList = rememberUpdatedState(
with(LocalDensity.current) { LocalWindowHeight().roundToPx() - topPaddingToContentPx.value - (AppBarHeight * fontSizeSqrtMultiplier * 2).roundToPx() }
with(LocalDensity.current) { LocalWindowHeight().roundToPx() - topPaddingToContentPx.value - (AppBarHeight * fontSizeSqrtMultiplier * numberOfBottomAppBars).roundToPx() }
)
val listState = rememberUpdatedState(rememberSaveable(chatInfo.id, searchValueIsEmpty.value, saver = LazyListState.Saver) {
val index = mergedItems.value.items.indexOfLast { it.hasUnread() }
val resetListState = remember { mutableStateOf(false) }
remember(chatModel.openAroundItemId.value) {
if (chatModel.openAroundItemId.value != null) {
closeSearch()
resetListState.value = !resetListState.value
}
}
val highlightedItems = remember { mutableStateOf(setOf<Long>()) }
val listState = rememberUpdatedState(rememberSaveable(chatInfo.id, searchValueIsEmpty.value, resetListState.value, saver = LazyListState.Saver) {
val openAroundItemId = chatModel.openAroundItemId.value
val index = mergedItems.value.indexInParentItems[openAroundItemId] ?: mergedItems.value.items.indexOfLast { it.hasUnread() }
val reportsState = reportsListState
if (openAroundItemId != null) {
highlightedItems.value += openAroundItemId
chatModel.openAroundItemId.value = null
}
if (reportsState != null) {
reportsListState = null
reportsState
@ -1221,7 +1245,6 @@ 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 {
// In group reports just set the itemId to scroll to so the main ChatView will handle scrolling
@ -1238,7 +1261,7 @@ fun BoxScope.ChatItemsList(
scrollToItemId.value = null }
}
}
LoadLastItems(loadingMoreItems, remoteHostId, chatInfo)
LoadLastItems(loadingMoreItems, resetListState, remoteHostId, chatInfo)
SmallScrollOnNewMessage(listState, reversedChatItems)
val finishedInitialComposition = remember { mutableStateOf(false) }
NotifyChatListOnFinishingComposition(finishedInitialComposition, chatInfo, revealedItems, listState, onComposed)
@ -1296,7 +1319,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, reversedChatItems) }, deleteMessage = deleteMessage, deleteMessages = deleteMessages, archiveReports = archiveReports, 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, showMemberInfo = showMemberInfo, showChatInfo = showChatInfo, 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, searchIsNotBlank = searchValueIsNotBlank, fillMaxWidth = fillMaxWidth, selectedChatItems = selectedChatItems, selectChatItem = { selectUnselectChatItem(true, cItem, revealed, selectedChatItems, reversedChatItems) }, deleteMessage = deleteMessage, deleteMessages = deleteMessages, archiveReports = archiveReports, 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, showMemberInfo = showMemberInfo, showChatInfo = showChatInfo, developerTools = developerTools, showViaProxy = showViaProxy, itemSeparation = itemSeparation, showTimestamp = itemSeparation.timestamp)
}
}
@ -1567,9 +1590,9 @@ fun BoxScope.ChatItemsList(
}
@Composable
private fun LoadLastItems(loadingMoreItems: MutableState<Boolean>, remoteHostId: Long?, chatInfo: ChatInfo) {
private fun LoadLastItems(loadingMoreItems: MutableState<Boolean>, resetListState: State<Boolean>, remoteHostId: Long?, chatInfo: ChatInfo) {
val contentTag = LocalContentTag.current
LaunchedEffect(remoteHostId, chatInfo.id) {
LaunchedEffect(remoteHostId, chatInfo.id, resetListState.value) {
try {
loadingMoreItems.value = true
if (chatModel.chatStateForContent(contentTag).totalAfter.value <= 0) return@LaunchedEffect
@ -1888,6 +1911,17 @@ fun topPaddingToContent(chatView: Boolean, additionalTopBar: Boolean = false): D
}
}
@Composable
private fun numberOfBottomAppBars(): Int {
val oneHandUI = remember { appPrefs.oneHandUI.state }
val chatBottomBar = remember { appPrefs.chatBottomBar.state }
return if (oneHandUI.value && chatBottomBar.value) {
2
} else {
1
}
}
@Composable
private fun FloatingDate(
modifier: Modifier,
@ -2798,6 +2832,7 @@ fun PreviewChatLayout() {
markChatRead = {},
changeNtfsState = { _, _ -> },
onSearchValueChanged = {},
closeSearch = {},
onComposed = {},
developerTools = false,
showViaProxy = false,
@ -2874,6 +2909,7 @@ fun PreviewGroupChatLayout() {
markChatRead = {},
changeNtfsState = { _, _ -> },
onSearchValueChanged = {},
closeSearch = {},
onComposed = {},
developerTools = false,
showViaProxy = false,

View file

@ -202,7 +202,7 @@ fun FramedItemView(
Column(
Modifier
.width(IntrinsicSize.Max)
.padding(start = if (tailRendered) msgTailWidthDp else 0.dp, end = if (sent && tailRendered) msgTailWidthDp else 0.dp)
.padding(start = if (!sent && tailRendered) msgTailWidthDp else 0.dp, end = if (sent && tailRendered) msgTailWidthDp else 0.dp)
) {
PriorityLayout(Modifier, CHAT_IMAGE_LAYOUT_ID) {
@Composable

View file

@ -210,8 +210,26 @@ suspend fun openGroupChat(rhId: Long?, groupId: Long, contentTag: MsgContentTag?
suspend fun openChat(rhId: Long?, chatInfo: ChatInfo, contentTag: MsgContentTag? = null) = openChat(rhId, chatInfo.chatType, chatInfo.apiId, contentTag)
private suspend fun openChat(rhId: Long?, chatType: ChatType, apiId: Long, contentTag: MsgContentTag? = null) =
apiLoadMessages(rhId, chatType, apiId, contentTag, ChatPagination.Initial(ChatPagination.INITIAL_COUNT))
suspend fun openChat(
rhId: Long?,
chatType: ChatType,
apiId: Long,
contentTag: MsgContentTag? = null,
openAroundItemId: Long? = null
) =
apiLoadMessages(
rhId,
chatType,
apiId,
contentTag,
if (openAroundItemId != null) {
ChatPagination.Around(openAroundItemId, ChatPagination.INITIAL_COUNT)
} else {
ChatPagination.Initial(ChatPagination.INITIAL_COUNT)
},
"",
openAroundItemId
)
suspend fun openLoadedChat(chat: Chat, contentTag: MsgContentTag? = null) {
withChats(contentTag) {