Просмотр исходного кода

delete chat now navigates to chat list and deletes it there

nayooti 5 лет назад
Родитель
Сommit
37f4e6edc8

+ 13 - 2
deltachat-ios/Controller/ChatListController.swift

@@ -18,6 +18,7 @@ class ChatListController: UIViewController {
     private var msgChangedObserver: Any?
     private var incomingMsgObserver: Any?
     private var viewChatObserver: Any?
+    private var deleteChatObserver: Any?
 
     private var newButton: UIBarButtonItem!
 
@@ -60,6 +61,12 @@ class ChatListController: UIViewController {
                 self.coordinator?.showChat(chatId: chatId)
             }
         }
+
+        deleteChatObserver = nc.addObserver(forName: dcNotificationChatDeletedInChatDetail, object: nil, queue: nil) { notification in
+            if let chatId = notification.userInfo?["chat_id"] as? Int {
+                self.deleteChat(chatId: chatId)
+            }
+        }
     }
 
     override func viewDidDisappear(_ animated: Bool) {
@@ -300,10 +307,14 @@ extension ChatListController: UITableViewDataSource, UITableViewDelegate {
             preferredStyle: .safeActionSheet
         )
         alert.addAction(UIAlertAction(title: String.localized("menu_delete_chat"), style: .destructive, handler: { _ in
-            self.dcContext.deleteChat(chatId: chatId)
-            self.getChatList()
+            self.deleteChat(chatId: chatId)
         }))
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: nil))
         self.present(alert, animated: true, completion: nil)
     }
+
+    private func deleteChat(chatId: Int) {
+        self.dcContext.deleteChat(chatId: chatId)
+        self.getChatList()
+    }
 }

+ 15 - 1
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -570,10 +570,12 @@ class ContactDetailCoordinator: Coordinator, ContactDetailCoordinatorProtocol {
 
     var dcContext: DcContext
     let navigationController: UINavigationController
+    let chatId: Int?
 
     private var childCoordinators: [Coordinator] = []
 
     init(dcContext: DcContext, chatId: Int?, navigationController: UINavigationController) {
+        self.chatId = chatId
         self.dcContext = dcContext
         self.navigationController = navigationController
     }
@@ -596,7 +598,19 @@ class ContactDetailCoordinator: Coordinator, ContactDetailCoordinatorProtocol {
     }
 
     func deleteChat() {
-        print("delete chat")
+        guard let chatId = chatId else {
+            return
+        }
+
+        func notifyToDeleteChat() {
+            NotificationCenter.default.post(name: dcNotificationChatDeletedInChatDetail, object: nil, userInfo: ["chat_id": chatId])
+        }
+
+        // we want to notify chatList to delete chat AFTER is is visible
+        CATransaction.begin()
+        CATransaction.setCompletionBlock(notifyToDeleteChat)
+        self.navigationController.popToRootViewController(animated: true)
+        CATransaction.commit()
     }
 
     func archiveChat() {

+ 1 - 0
deltachat-ios/DC/events.swift

@@ -10,6 +10,7 @@ let dcNotificationSecureInviterProgress = Notification.Name(rawValue: "MrEventSe
 let dcNotificationViewChat = Notification.Name(rawValue: "MrEventViewChat")
 let dcNotificationContactChanged = Notification.Name(rawValue: "MrEventContactsChanged")
 let dcNotificationChatModified = Notification.Name(rawValue: "dcNotificationChatModified")
+let dcNotificationChatDeletedInChatDetail = Notification.Name(rawValue: "ChatDeletedInChatDetail")
 
 @_silgen_name("callbackSwift")