From 28748b8f2dd2603f590150e95a50e4258de8883b Mon Sep 17 00:00:00 2001 From: spaced4ndy <8711996+spaced4ndy@users.noreply.github.com> Date: Tue, 7 May 2024 17:27:19 +0400 Subject: [PATCH] delete via chat list (has bugs) --- apps/ios/Shared/Model/SimpleXAPI.swift | 20 +++++++ .../Views/ChatList/ChatListNavLink.swift | 56 +++++++++++++------ 2 files changed, 60 insertions(+), 16 deletions(-) diff --git a/apps/ios/Shared/Model/SimpleXAPI.swift b/apps/ios/Shared/Model/SimpleXAPI.swift index 861784e513..f0896703d4 100644 --- a/apps/ios/Shared/Model/SimpleXAPI.swift +++ b/apps/ios/Shared/Model/SimpleXAPI.swift @@ -773,6 +773,26 @@ func deleteChat(_ chat: Chat, chatDeleteMode: ChatDeleteMode = .full(notify: tru } } +func deleteChatContact(_ chat: Chat, chatDeleteMode: ChatDeleteMode = .full(notify: true)) async { + do { + let cInfo = chat.chatInfo + let ct = try await apiDeleteContact(id: cInfo.apiId, chatDeleteMode: chatDeleteMode) + DispatchQueue.main.async { + if case .full = chatDeleteMode { + ChatModel.shared.removeChat(cInfo.id) + } else { + ChatModel.shared.updateContact(ct) + } + } + } catch let error { + logger.error("deleteChatContact apiDeleteContact error: \(responseError(error))") + AlertManager.shared.showAlertMsg( + title: "Error deleting chat!", + message: "Error: \(responseError(error))" + ) + } +} + func apiClearChat(type: ChatType, id: Int64) async throws -> ChatInfo { let r = await chatSendCmd(.apiClearChat(type: type, id: id), bgTask: false) if case let .chatCleared(_, updatedChatInfo) = r { return updatedChatInfo } diff --git a/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift b/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift index d3c63916b7..79ee498c69 100644 --- a/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift +++ b/apps/ios/Shared/Views/ChatList/ChatListNavLink.swift @@ -32,11 +32,23 @@ struct ChatListNavLink: View { @State private var showJoinGroupDialog = false @State private var showContactConnectionInfo = false @State private var showInvalidJSON = false - @State private var showDeleteContactActionSheet = false + @State private var contactNavLinkSheet: ContactNavLinkActionSheet? = nil @State private var showConnectContactViaAddressDialog = false @State private var inProgress = false @State private var progressByTimeout = false + enum ContactNavLinkActionSheet: Identifiable { + case deleteConversationActionSheet + case notifyDeleteContactActionSheet + + var id: String { + switch self { + case .deleteConversationActionSheet: return "deleteConversationActionSheet" + case .notifyDeleteContactActionSheet: return "notifyDeleteContactActionSheet" + } + } + } + var body: some View { Group { switch chat.chatInfo { @@ -72,7 +84,7 @@ struct ChatListNavLink: View { .frame(height: rowHeights[dynamicTypeSize]) .swipeActions(edge: .trailing, allowsFullSwipe: true) { Button { - showDeleteContactActionSheet = true + contactNavLinkSheet = .deleteConversationActionSheet } label: { Label("Delete", systemImage: "trash") } @@ -100,7 +112,7 @@ struct ChatListNavLink: View { } Button { if contact.ready || !contact.active { - showDeleteContactActionSheet = true + contactNavLinkSheet = .deleteConversationActionSheet } else { AlertManager.shared.showAlert(deletePendingContactAlert(chat, contact)) } @@ -112,24 +124,36 @@ struct ChatListNavLink: View { .frame(height: rowHeights[dynamicTypeSize]) } } - .actionSheet(isPresented: $showDeleteContactActionSheet) { - if contact.ready && contact.active { + .actionSheet(item: $contactNavLinkSheet) { sheet in + switch(sheet) { + case .deleteConversationActionSheet: return ActionSheet( - title: Text("Delete contact?\nThis cannot be undone!"), + title: Text("Delete conversation?"), buttons: [ - .destructive(Text("Delete and notify contact")) { Task { await deleteChat(chat, chatDeleteMode: .full(notify: true)) } }, - .destructive(Text("Delete")) { Task { await deleteChat(chat, chatDeleteMode: .full(notify: false)) } }, - .cancel() - ] - ) - } else { - return ActionSheet( - title: Text("Delete contact?\nThis cannot be undone!"), - buttons: [ - .destructive(Text("Delete")) { Task { await deleteChat(chat) } }, + .destructive(Text("Delete conversation")) { Task { await deleteChatContact(chat, chatDeleteMode: .messages) } }, + .destructive(Text("Delete conversation and contact")) { contactNavLinkSheet = .notifyDeleteContactActionSheet }, .cancel() ] ) + case .notifyDeleteContactActionSheet: + if contact.ready && contact.active { + return ActionSheet( + title: Text("Notify contact?\nThis cannot be undone!"), + buttons: [ + .destructive(Text("Delete and notify contact")) { Task { await deleteChatContact(chat, chatDeleteMode: .full(notify: true)) } }, + .destructive(Text("Delete without notification")) { Task { await deleteChatContact(chat, chatDeleteMode: .full(notify: false)) } }, + .cancel() + ] + ) + } else { + return ActionSheet( + title: Text("Confirm contact deletion.\nThis cannot be undone!"), + buttons: [ + .destructive(Text("Delete")) { Task { await deleteChatContact(chat, chatDeleteMode: .full(notify: false)) } }, + .cancel() + ] + ) + } } } }