瀏覽代碼

ensure that a new contact is always added to the member group during group creation, even if the contact actually already existed before

cyberta 4 年之前
父節點
當前提交
e8dc0475e1

+ 8 - 40
deltachat-ios/Controller/AddGroupMembersViewController.swift

@@ -22,8 +22,6 @@ class AddGroupMembersViewController: GroupMembersViewController {
         case memberList
     }
 
-    private var contactAddedObserver: NSObjectProtocol?
-
     private lazy var chatMemberIds: [Int] = {
         if let chat = chat {
             return chat.contactIds
@@ -86,44 +84,6 @@ class AddGroupMembersViewController: GroupMembersViewController {
         navigationItem.rightBarButtonItem = doneButton
         navigationItem.leftBarButtonItem = cancelButton
         contactIds = loadMemberCandidates()
-
-        let nc = NotificationCenter.default
-        contactAddedObserver = nc.addObserver(
-            forName: dcNotificationContactChanged,
-            object: nil,
-            queue: nil
-        ) { [weak self] notification in
-            guard let self = self else { return }
-            if let ui = notification.userInfo {
-                if let contactId = ui["contact_id"] as? Int {
-                    if contactId == 0 {
-                        return
-                    }
-                    self.contactIds = self.loadMemberCandidates()
-                    if self.contactIds.contains(contactId) {
-                        self.selectedContactIds.insert(contactId)
-                        self.tableView.reloadData()
-                    }
-
-                }
-            }
-        }
-    }
-
-    override func viewWillAppear(_ animated: Bool) {
-        super.viewWillAppear(animated)
-    }
-
-    override func viewWillDisappear(_: Bool) {
-        if !isMovingFromParent {
-            // a subview was added to the navigation stack, no action needed
-            return
-        }
-
-        let nc = NotificationCenter.default
-        if let observer = self.contactAddedObserver {
-            nc.removeObserver(observer)
-        }
     }
 
     @objc func cancelButtonPressed() {
@@ -187,6 +147,14 @@ class AddGroupMembersViewController: GroupMembersViewController {
     private func showNewContactController() {
         let newContactController = NewContactController(dcContext: dcContext)
         newContactController.createChatOnSave = false
+        newContactController.onContactSaved = { [weak self] (contactId: Int) -> Void in
+            guard let self = self else { return }
+            self.contactIds = self.loadMemberCandidates()
+            if self.contactIds.contains(contactId) {
+                self.selectedContactIds.insert(contactId)
+                self.tableView.reloadData()
+            }
+        }
         navigationController?.pushViewController(newContactController, animated: true)
     }
 }

+ 5 - 0
deltachat-ios/Controller/NewContactController.swift

@@ -11,6 +11,8 @@ class NewContactController: UITableViewController {
     var doneButton: UIBarButtonItem?
     var cancelButton: UIBarButtonItem?
 
+    var onContactSaved: ((Int) -> Void)?
+
     func contactIsValid() -> Bool {
         return Utils.isValid(email: model.email)
     }
@@ -73,6 +75,9 @@ class NewContactController: UITableViewController {
 
     @objc func saveContactButtonPressed() {
         let contactId = dcContext.createContact(name: model.name, email: model.email)
+        if let onContactSaved = self.onContactSaved {
+            onContactSaved(contactId)
+        }
         if createChatOnSave {
             let chatId = dcContext.createChatByContactId(contactId: contactId)
             showChat(chatId: chatId)