From 50b71b30452d88f39a60204e7af8084460c660c1 Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Tue, 9 Jan 2024 13:18:01 +0400 Subject: [PATCH] ios: fix "mark read" chat preview button not always marking unread items as read --- apps/ios/Shared/Model/SimpleXAPI.swift | 21 +++++++++++++++++-- .../Views/ChatList/ChatListNavLink.swift | 2 +- apps/ios/SimpleXChat/APITypes.swift | 8 +++++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/apps/ios/Shared/Model/SimpleXAPI.swift b/apps/ios/Shared/Model/SimpleXAPI.swift index 2dbb43eecc..8e6644496e 100644 --- a/apps/ios/Shared/Model/SimpleXAPI.swift +++ b/apps/ios/Shared/Model/SimpleXAPI.swift @@ -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))") } } diff --git a/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift b/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift index 18464b3bb5..c0f1b7e35a 100644 --- a/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift +++ b/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift @@ -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") } diff --git a/apps/ios/SimpleXChat/APITypes.swift b/apps/ios/SimpleXChat/APITypes.swift index a199966bab..abeedb0aee 100644 --- a/apps/ios/SimpleXChat/APITypes.swift +++ b/apps/ios/SimpleXChat/APITypes.swift @@ -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))"