Browse Source

add removal of contacts in new group creation (#470), fix interference between contacts added by list selection and QR code scanning

cyberta 5 years ago
parent
commit
a1b1eea61b

+ 47 - 8
deltachat-ios/Controller/NewGroupController.swift

@@ -27,6 +27,7 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     lazy var groupNameCell: TextFieldCell = {
         let cell = TextFieldCell(description: String.localized("group_name"), placeholder: String.localized("group_name"))
         cell.onTextFieldChange = self.updateGroupName
+        cell.textField.autocorrectionType = UITextAutocorrectionType.no
         return cell
     }()
 
@@ -63,13 +64,6 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
         self.hideKeyboardOnTap() 
     }
 
-    override func viewWillAppear(_ animated: Bool) {
-        if groupChatId != 0 {
-            let chat = DcChat.init(id: groupChatId)
-            updateGroupContactIds(Set(chat.contactIds))
-        }
-    }
-
     @objc func doneButtonPressed() {
         if groupChatId == 0 {
             groupChatId = dcContext.createGroupChat(verified: isVerifiedGroup, name: groupName)
@@ -209,6 +203,30 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
         }
     }
 
+    override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
+        let section = indexPath.section
+        let row = indexPath.row
+
+        //swipe by delete
+        if section == sectionGroupMembers, groupContactIds[row] != DC_CONTACT_ID_SELF {
+            let delete = UITableViewRowAction(style: .destructive, title: String.localized("remove_desktop")) { [unowned self] _, indexPath in
+                if self.groupChatId != 0, DcChat(id: self.groupChatId).contactIds.contains(self.groupContactIds[row]) {
+                    let success = self.dcContext.removeContactFromChat(chatId: self.groupChatId, contactId: self.groupContactIds[row])
+                    if success {
+                        self.removeGroupContactFromList(at: indexPath)
+                    }
+                } else {
+                    self.removeGroupContactFromList(at: indexPath)
+                }
+            }
+            delete.backgroundColor = UIColor.red
+            return [delete]
+        } else {
+            return nil
+        }
+    }
+
+
     private func updateGroupName(textView: UITextField) {
         let name = textView.text ?? ""
         groupName = name
@@ -248,9 +266,30 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
         self.tableView.endUpdates()
     }
 
-    func updateGroupContactIds(_ members: Set<Int>) {
+    func updateGroupContactIdsOnQRCodeInvite() {
+        for contactId in DcChat(id: groupChatId).contactIds {
+            contactIdsForGroup.insert(contactId)
+        }
+        groupContactIds = Array(contactIdsForGroup)
+        self.tableView.reloadData()
+    }
+
+    func updateGroupContactIdsOnListSelection(_ members: Set<Int>) {
+        if groupChatId != 0 {
+            var members = members
+            for contactId in DcChat(id: groupChatId).contactIds {
+                members.insert(contactId)
+            }
+        }
         contactIdsForGroup = members
         groupContactIds = Array(members)
         self.tableView.reloadData()
     }
+
+    func removeGroupContactFromList(at indexPath: IndexPath) {
+        let row = indexPath.row
+        self.contactIdsForGroup.remove(self.groupContactIds[row])
+        self.groupContactIds.remove(at: row)
+        tableView.deleteRows(at: [indexPath], with: .fade)
+    }
 }

+ 6 - 0
deltachat-ios/Controller/QrInviteViewController.swift

@@ -8,6 +8,8 @@ class QrInviteViewController: UITableViewController {
     let dcContext: DcContext
     let chatId: Int
 
+    var onDismissed: (() -> Void)?
+
     init(dcContext: DcContext, chatId: Int) {
         self.dcContext = dcContext
         self.chatId = chatId
@@ -24,6 +26,10 @@ class QrInviteViewController: UITableViewController {
         tableView.separatorStyle = .none
     }
 
+    override func viewDidDisappear(_ animated: Bool) {
+        onDismissed?()
+    }
+
     override func tableView(_: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
         let row = indexPath.row
         switch row {

+ 9 - 2
deltachat-ios/Coordinator/AppCoordinator.swift

@@ -519,6 +519,7 @@ class NewGroupCoordinator: Coordinator {
 
     func showQrCodeInvite(chatId: Int) {
         let qrInviteCodeController = QrInviteViewController(dcContext: dcContext, chatId: chatId)
+        qrInviteCodeController.onDismissed = onQRInviteCodeControllerDismissed
         navigationController.pushViewController(qrInviteCodeController, animated: true)
     }
 
@@ -529,14 +530,20 @@ class NewGroupCoordinator: Coordinator {
         let coordinator = NewGroupAddMembersCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
         newGroupController.coordinator = coordinator
-        newGroupController.onMembersSelected = onGroupMembersSelected
+        newGroupController.onMembersSelected = onGroupMembersSelected(_:)
         navigationController.pushViewController(newGroupController, animated: true)
     }
 
+    func onQRInviteCodeControllerDismissed() {
+        if let groupNameController = navigationController.topViewController as? NewGroupController {
+            groupNameController.updateGroupContactIdsOnQRCodeInvite()
+        }
+    }
+
     func onGroupMembersSelected(_ memberIds: Set<Int>) {
         navigationController.popViewController(animated: true)
         if let groupNameController = navigationController.topViewController as? NewGroupController {
-            groupNameController.updateGroupContactIds(memberIds)
+            groupNameController.updateGroupContactIdsOnListSelection(memberIds)
         }
     }
 }