ios: dynamically mark items read

This commit is contained in:
spaced4ndy
2025-05-05 12:32:55 +04:00
parent 92787df8a3
commit 0ea1363a27
4 changed files with 21 additions and 21 deletions
+12 -12
View File
@@ -809,7 +809,7 @@ final class ChatModel: ObservableObject {
im.reversedChatItems.first?.isLiveDummy == true
}
func markAllChatItemsRead(_ cInfo: ChatInfo) {
func markAllChatItemsRead(_ chatIM: ItemsModel, _ cInfo: ChatInfo) {
// update preview
_updateChat(cInfo.id) { chat in
self.decreaseUnreadCounter(user: self.currentUser!, chat: chat)
@@ -820,7 +820,7 @@ final class ChatModel: ObservableObject {
if chatId == cInfo.id {
var i = 0
while i < im.reversedChatItems.count {
markChatItemRead_(i)
markChatItemRead_(chatIM, i)
i += 1
}
im.chatState.itemsRead(nil, im.reversedChatItems.reversed())
@@ -850,21 +850,21 @@ final class ChatModel: ObservableObject {
}
}
func markChatItemsRead(_ cInfo: ChatInfo, _ itemIds: [ChatItem.ID], _ mentionsRead: Int) {
func markChatItemsRead(_ chatIM: ItemsModel, _ cInfo: ChatInfo, _ itemIds: [ChatItem.ID], _ mentionsRead: Int) {
if self.chatId == cInfo.id {
var unreadItemIds: Set<ChatItem.ID> = []
var i = 0
var ids = Set(itemIds)
while i < im.reversedChatItems.count && !ids.isEmpty {
let item = im.reversedChatItems[i]
while i < chatIM.reversedChatItems.count && !ids.isEmpty {
let item = chatIM.reversedChatItems[i]
if ids.contains(item.id) && item.isRcvNew {
markChatItemRead_(i)
markChatItemRead_(chatIM, i)
unreadItemIds.insert(item.id)
ids.remove(item.id)
}
i += 1
}
im.chatState.itemsRead(unreadItemIds, im.reversedChatItems.reversed())
chatIM.chatState.itemsRead(unreadItemIds, chatIM.reversedChatItems.reversed())
}
self.unreadCollector.changeUnreadCounter(cInfo.id, by: -itemIds.count, unreadMentions: -mentionsRead)
}
@@ -960,13 +960,13 @@ final class ChatModel: ObservableObject {
}
}
private func markChatItemRead_(_ i: Int) {
let meta = im.reversedChatItems[i].meta
private func markChatItemRead_(_ chatIM: ItemsModel, _ i: Int) {
let meta = chatIM.reversedChatItems[i].meta
if case .rcvNew = meta.itemStatus {
im.reversedChatItems[i].meta.itemStatus = .rcvRead
im.reversedChatItems[i].viewTimestamp = .now
chatIM.reversedChatItems[i].meta.itemStatus = .rcvRead
chatIM.reversedChatItems[i].viewTimestamp = .now
if meta.itemLive != true, let ttl = meta.itemTimed?.ttl {
im.reversedChatItems[i].meta.itemTimed?.deleteAt = .now + TimeInterval(ttl)
chatIM.reversedChatItems[i].meta.itemTimed?.deleteAt = .now + TimeInterval(ttl)
}
}
}
+5 -5
View File
@@ -1541,13 +1541,13 @@ func apiGetNetworkStatuses() throws -> [ConnNetworkStatus] {
throw r
}
func markChatRead(_ chat: Chat) async {
func markChatRead(_ im: ItemsModel, _ chat: Chat) async {
do {
if chat.chatStats.unreadCount > 0 {
let cInfo = chat.chatInfo
try await apiChatRead(type: cInfo.chatType, id: cInfo.apiId, scope: cInfo.groupChatScope())
await MainActor.run {
withAnimation { ChatModel.shared.markAllChatItemsRead(cInfo) }
withAnimation { ChatModel.shared.markAllChatItemsRead(im, cInfo) }
}
}
if chat.chatStats.unreadChat {
@@ -1570,11 +1570,11 @@ func markChatUnread(_ chat: Chat, unreadChat: Bool = true) async {
}
}
func apiMarkChatItemsRead(_ cInfo: ChatInfo, _ itemIds: [ChatItem.ID], mentionsRead: Int) async {
func apiMarkChatItemsRead(_ im: ItemsModel, _ cInfo: ChatInfo, _ itemIds: [ChatItem.ID], mentionsRead: Int) async {
do {
try await apiChatItemsRead(type: cInfo.chatType, id: cInfo.apiId, scope: cInfo.groupChatScope(), itemIds: itemIds)
DispatchQueue.main.async {
ChatModel.shared.markChatItemsRead(cInfo, itemIds, mentionsRead)
await MainActor.run {
ChatModel.shared.markChatItemsRead(im, cInfo, itemIds, mentionsRead)
}
} catch {
logger.error("apiChatItemsRead error: \(responseError(error))")
+3 -3
View File
@@ -755,7 +755,7 @@ struct ChatView: View {
.contextMenu {
Button {
Task {
await markChatRead(chat)
await markChatRead(im, chat)
}
} label: {
Label("Mark read", systemImage: "checkmark")
@@ -1281,7 +1281,7 @@ struct ChatView: View {
let (itemIds, unreadMentions) = unreadItemIds(range)
if !itemIds.isEmpty {
waitToMarkRead {
await apiMarkChatItemsRead(chat.chatInfo, itemIds, mentionsRead: unreadMentions)
await apiMarkChatItemsRead(im, chat.chatInfo, itemIds, mentionsRead: unreadMentions)
if (im.secondaryIMFilter != nil) {
m.decreaseGroupSupportChatsUnreadCounter(chat.chatInfo.id, by: itemIds.count )
}
@@ -1289,7 +1289,7 @@ struct ChatView: View {
}
} else if chatItem.isRcvNew {
waitToMarkRead {
await apiMarkChatItemsRead(chat.chatInfo, [chatItem.id], mentionsRead: chatItem.meta.userMention ? 1 : 0)
await apiMarkChatItemsRead(im, chat.chatInfo, [chatItem.id], mentionsRead: chatItem.meta.userMention ? 1 : 0)
if (im.secondaryIMFilter != nil) {
m.decreaseGroupSupportChatsUnreadCounter(chat.chatInfo.id)
}
@@ -276,7 +276,7 @@ struct ChatListNavLink: View {
@ViewBuilder private func markReadButton() -> some View {
if chat.chatStats.unreadCount > 0 || chat.chatStats.unreadChat {
Button {
Task { await markChatRead(chat) }
Task { await markChatRead(ItemsModel.shared, chat) }
} label: {
SwipeLabel(NSLocalizedString("Read", comment: "swipe action"), systemImage: "checkmark", inverted: oneHandUI)
}