mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2024-12-17 17:20:21 +01:00
update min unread item in memory when app is running
This commit is contained in:
@@ -400,6 +400,9 @@ final class ChatModel: ObservableObject {
|
||||
}
|
||||
if case .rcvNew = cItem.meta.itemStatus {
|
||||
unreadCollector.changeUnreadCounter(cInfo.id, by: 1)
|
||||
if chats[i].chatStats.minUnreadItemId == 0 {
|
||||
chats[i].chatStats.minUnreadItemId = cItem.id
|
||||
}
|
||||
}
|
||||
popChatCollector.throttlePopChat(cInfo.id, currentPosition: i)
|
||||
} else {
|
||||
@@ -486,6 +489,10 @@ final class ChatModel: ObservableObject {
|
||||
if let pItem = chat.chatItems.last, pItem.id == cItem.id {
|
||||
chat.chatItems = [ChatItem.deletedItemDummy()]
|
||||
}
|
||||
if chat.chatStats.minUnreadItemId == cItem.id {
|
||||
let oldestUnreadItem = chat.chatItems.last { $0.isRcvNew }
|
||||
chat.chatStats.minUnreadItemId = oldestUnreadItem?.id ?? 0
|
||||
}
|
||||
}
|
||||
// remove from current chat
|
||||
if chatId == cInfo.id {
|
||||
@@ -643,6 +650,14 @@ final class ChatModel: ObservableObject {
|
||||
}
|
||||
}
|
||||
self.unreadCollector.changeUnreadCounter(cInfo.id, by: -itemIds.count)
|
||||
if let chat = getChat(cInfo.id), itemIds.contains(chat.chatStats.minUnreadItemId) {
|
||||
let firstUnreadItem = im.reversedChatItems.last(where: { $0.isRcvNew})
|
||||
chat.chatStats = ChatStats(
|
||||
unreadCount: chat.chatStats.unreadCount,
|
||||
minUnreadItemId: firstUnreadItem?.id ?? 0,
|
||||
unreadChat: chat.chatStats.unreadChat
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private let unreadCollector = UnreadCollector()
|
||||
|
||||
@@ -370,21 +370,26 @@ struct ChatView: View {
|
||||
}
|
||||
ChatView.FloatingButtonModel.shared.totalUnread = chat.chatStats.unreadCount
|
||||
sectionModel.resetSections(items: im.reversedChatItems)
|
||||
|
||||
|
||||
let minUnreadItemId = chat.chatStats.minUnreadItemId
|
||||
if minUnreadItemId > 0, !im.reversedChatItems.contains(where: { $0.id == minUnreadItemId }) {
|
||||
Task {
|
||||
if let reversedPage = await loadItemsAround(chat.chatInfo, minUnreadItemId) {
|
||||
await MainActor.run {
|
||||
let reversedPageToAppend = self.sectionModel.handleSectionInsertion(
|
||||
candidateSection: .current,
|
||||
reversedPage: reversedPage,
|
||||
allItems: im.reversedChatItems
|
||||
)
|
||||
im.reversedChatItems.append(contentsOf: reversedPageToAppend)
|
||||
|
||||
withAnimation {
|
||||
scrollModel.scrollToItem(id: minUnreadItemId, position: .middle)
|
||||
if minUnreadItemId > 0 {
|
||||
if im.reversedChatItems.contains(where: { $0.id == minUnreadItemId }) {
|
||||
withAnimation {
|
||||
scrollModel.scrollToItem(id: minUnreadItemId, position: .middle)
|
||||
}
|
||||
} else {
|
||||
Task {
|
||||
if let reversedPage = await loadItemsAround(chat.chatInfo, minUnreadItemId) {
|
||||
await MainActor.run {
|
||||
let reversedPageToAppend = self.sectionModel.handleSectionInsertion(
|
||||
candidateSection: .current,
|
||||
reversedPage: reversedPage,
|
||||
allItems: im.reversedChatItems
|
||||
)
|
||||
im.reversedChatItems.append(contentsOf: reversedPageToAppend)
|
||||
withAnimation {
|
||||
scrollModel.scrollToItem(id: minUnreadItemId, position: .middle, animated: false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user