ios: fix "mark read" chat preview button not always marking unread items as read

This commit is contained in:
spaced4ndy
2024-01-09 13:18:01 +04:00
parent 61c507e7da
commit 50b71b3045
3 changed files with 26 additions and 5 deletions
+19 -2
View File
@@ -844,7 +844,7 @@ func apiRejectContactRequest(contactReqId: Int64) async throws {
throw r
}
func apiChatRead(type: ChatType, id: Int64, itemRange: (Int64, Int64)) async throws {
func apiChatRead(type: ChatType, id: Int64, itemRange: (Int64, Int64)? = nil) async throws {
try await sendCommandOkResp(.apiChatRead(type: type, id: id, itemRange: itemRange))
}
@@ -1044,7 +1044,24 @@ func markChatRead(_ chat: Chat, aboveItem: ChatItem? = nil) async {
await markChatUnread(chat, unreadChat: false)
}
} catch {
logger.error("markChatRead apiChatRead error: \(responseError(error))")
logger.error("markChatRead error: \(responseError(error))")
}
}
func markChatReadAll(_ chat: Chat) async {
do {
if chat.chatStats.unreadCount > 0 {
let cInfo = chat.chatInfo
try await apiChatRead(type: cInfo.chatType, id: cInfo.apiId)
await MainActor.run {
withAnimation { ChatModel.shared.markChatItemsRead(cInfo) }
}
}
if chat.chatStats.unreadChat {
await markChatUnread(chat, unreadChat: false)
}
} catch {
logger.error("markChatReadAll error: \(responseError(error))")
}
}
@@ -210,7 +210,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 markChatReadAll(chat) }
} label: {
Label("Read", systemImage: "checkmark")
}
+6 -2
View File
@@ -115,7 +115,7 @@ public enum ChatCommand {
case apiGetCallInvitations
case apiCallStatus(contact: Contact, callStatus: WebRTCCallStatus)
case apiGetNetworkStatuses
case apiChatRead(type: ChatType, id: Int64, itemRange: (Int64, Int64))
case apiChatRead(type: ChatType, id: Int64, itemRange: (Int64, Int64)?)
case apiChatUnread(type: ChatType, id: Int64, unreadChat: Bool)
case receiveFile(fileId: Int64, encrypted: Bool?, inline: Bool?)
case setFileToReceive(fileId: Int64, encrypted: Bool?)
@@ -265,7 +265,11 @@ public enum ChatCommand {
case .apiGetCallInvitations: return "/_call get"
case let .apiCallStatus(contact, callStatus): return "/_call status @\(contact.apiId) \(callStatus.rawValue)"
case .apiGetNetworkStatuses: return "/_network_statuses"
case let .apiChatRead(type, id, itemRange: (from, to)): return "/_read chat \(ref(type, id)) from=\(from) to=\(to)"
case let .apiChatRead(type, id, itemRange): if let (from, to) = itemRange {
return "/_read chat \(ref(type, id)) from=\(from) to=\(to)"
} else {
return "/_read chat \(ref(type, id))"
}
case let .apiChatUnread(type, id, unreadChat): return "/_unread chat \(ref(type, id)) \(onOff(unreadChat))"
case let .receiveFile(fileId, encrypt, inline): return "/freceive \(fileId)\(onOffParam("encrypt", encrypt))\(onOffParam("inline", inline))"
case let .setFileToReceive(fileId, encrypt): return "/_set_file_to_receive \(fileId)\(onOffParam("encrypt", encrypt))"