浏览代码

join newly created verified groups via QR code

cyberta 5 年之前
父节点
当前提交
8936922711

+ 1 - 1
deltachat-ios/Controller/GroupMembersViewController.swift

@@ -2,7 +2,7 @@ import UIKit
 
 
 class NewGroupAddMembersViewController: GroupMembersViewController {
 class NewGroupAddMembersViewController: GroupMembersViewController {
     weak var coordinator: NewGroupAddMembersCoordinator?
     weak var coordinator: NewGroupAddMembersCoordinator?
-    let dcContext: DcContext;
+    let dcContext: DcContext
 
 
     var onMembersSelected: ((Set<Int>) -> Void)?
     var onMembersSelected: ((Set<Int>) -> Void)?
     let isVerifiedGroup: Bool
     let isVerifiedGroup: Bool

+ 22 - 10
deltachat-ios/Controller/NewGroupController.swift

@@ -5,12 +5,14 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     weak var coordinator: NewGroupCoordinator?
     weak var coordinator: NewGroupCoordinator?
 
 
     var groupName: String = ""
     var groupName: String = ""
+    var groupChatId: Int = 0
 
 
     var doneButton: UIBarButtonItem!
     var doneButton: UIBarButtonItem!
     var contactIdsForGroup: Set<Int> // TODO: check if array is sufficient
     var contactIdsForGroup: Set<Int> // TODO: check if array is sufficient
     var groupContactIds: [Int]
     var groupContactIds: [Int]
     var groupImage: UIImage?
     var groupImage: UIImage?
     let isVerifiedGroup: Bool
     let isVerifiedGroup: Bool
+    let dcContext: DcContext
 
 
     private let sectionGroupDetails = 0
     private let sectionGroupDetails = 0
     private let sectionGroupDetailsRowAvatar = 0
     private let sectionGroupDetailsRowAvatar = 0
@@ -37,14 +39,13 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
         return cell
         return cell
     }()
     }()
 
 
-    convenience init(isVerified: Bool) {
-        self.init(contactIdsForGroup: [Int(DC_CONTACT_ID_SELF)], isVerified: isVerified)
-    }
+    var qrInviteCodeCell: ActionCell?
 
 
-    init(contactIdsForGroup: Set<Int>, isVerified: Bool) {
-        self.contactIdsForGroup = contactIdsForGroup
+    init(dcContext: DcContext, isVerified: Bool) {
+        self.contactIdsForGroup = [Int(DC_CONTACT_ID_SELF)]
         self.groupContactIds = Array(contactIdsForGroup)
         self.groupContactIds = Array(contactIdsForGroup)
         self.isVerifiedGroup = isVerified
         self.isVerifiedGroup = isVerified
+        self.dcContext = dcContext
         super.init(style: .grouped)
         super.init(style: .grouped)
     }
     }
 
 
@@ -64,15 +65,20 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
     }
     }
 
 
     @objc func doneButtonPressed() {
     @objc func doneButtonPressed() {
-        let groupChatId = dc_create_group_chat(mailboxPointer, 0, groupName)
+        if groupChatId == 0 {
+            groupChatId = dcContext.createGroupChat(name: groupName)
+        } else {
+            _ = dcContext.setChatName(chatId: groupChatId, name: groupName)
+        }
+
         for contactId in contactIdsForGroup {
         for contactId in contactIdsForGroup {
-            let success = dc_add_contact_to_chat(mailboxPointer, groupChatId, UInt32(contactId))
+            let success = dcContext.addContactToChat(chatId: groupChatId, contactId: contactId)
 
 
             if let groupImage = groupImage, let dcContext = coordinator?.dcContext {
             if let groupImage = groupImage, let dcContext = coordinator?.dcContext {
                     AvatarHelper.saveChatAvatar(dcContext: dcContext, image: groupImage, for: Int(groupChatId))
                     AvatarHelper.saveChatAvatar(dcContext: dcContext, image: groupImage, for: Int(groupChatId))
             }
             }
 
 
-            if success == 1 {
+            if success {
                 logger.info("successfully added \(contactId) to group \(groupName)")
                 logger.info("successfully added \(contactId) to group \(groupName)")
             } else {
             } else {
                 logger.error("failed to add \(contactId) to group \(groupName)")
                 logger.error("failed to add \(contactId) to group \(groupName)")
@@ -108,13 +114,16 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
                 if let actionCell = cell as? ActionCell {
                 if let actionCell = cell as? ActionCell {
                     actionCell.actionTitle = String.localized("group_add_members")
                     actionCell.actionTitle = String.localized("group_add_members")
                     actionCell.actionColor = UIColor.systemBlue
                     actionCell.actionColor = UIColor.systemBlue
+                    actionCell.isUserInteractionEnabled = true
                 }
                 }
                 return cell
                 return cell
             } else {
             } else {
                 let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath)
                 let cell = tableView.dequeueReusableCell(withIdentifier: "actionCell", for: indexPath)
                 if let actionCell = cell as? ActionCell {
                 if let actionCell = cell as? ActionCell {
                     actionCell.actionTitle = String.localized("qrshow_join_group_title")
                     actionCell.actionTitle = String.localized("qrshow_join_group_title")
-                    actionCell.actionColor = UIColor.systemBlue
+                    actionCell.actionColor = groupName.isEmpty ? DcColors.colorDisabled : UIColor.systemBlue
+                    actionCell.isUserInteractionEnabled = !groupName.isEmpty
+                    qrInviteCodeCell = actionCell
                 }
                 }
                 return cell
                 return cell
             }
             }
@@ -181,7 +190,8 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
                 contactsWithoutSelf.remove(Int(DC_CONTACT_ID_SELF))
                 contactsWithoutSelf.remove(Int(DC_CONTACT_ID_SELF))
                 coordinator?.showAddMembers(preselectedMembers: contactsWithoutSelf, isVerified: self.isVerifiedGroup)
                 coordinator?.showAddMembers(preselectedMembers: contactsWithoutSelf, isVerified: self.isVerifiedGroup)
             } else {
             } else {
-                logger.debug("todo: implement me")
+                self.groupChatId = dcContext.createGroupChat(name: groupName)
+                coordinator?.showQrCodeInvite(chatId: Int(self.groupChatId))
             }
             }
         }
         }
     }
     }
@@ -190,6 +200,8 @@ class NewGroupController: UITableViewController, MediaPickerDelegate {
         let name = textView.text ?? ""
         let name = textView.text ?? ""
         groupName = name
         groupName = name
         doneButton.isEnabled = name.containsCharacters()
         doneButton.isEnabled = name.containsCharacters()
+        qrInviteCodeCell?.isUserInteractionEnabled = name.containsCharacters()
+        qrInviteCodeCell?.actionColor = groupName.isEmpty ? DcColors.colorDisabled : UIColor.systemBlue
     }
     }
 
 
     private func onAvatarTapped() {
     private func onAvatarTapped() {

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

@@ -292,7 +292,7 @@ class NewChatCoordinator: Coordinator {
     }
     }
 
 
     func showNewGroupController(isVerified: Bool) {
     func showNewGroupController(isVerified: Bool) {
-        let newGroupController = NewGroupController(isVerified: isVerified)
+        let newGroupController = NewGroupController(dcContext: dcContext, isVerified: isVerified)
         let coordinator = NewGroupCoordinator(dcContext: dcContext, navigationController: navigationController)
         let coordinator = NewGroupCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
         childCoordinators.append(coordinator)
         newGroupController.coordinator = coordinator
         newGroupController.coordinator = coordinator
@@ -517,6 +517,11 @@ class NewGroupCoordinator: Coordinator {
         mediaPicker.showCamera(delegate: delegate)
         mediaPicker.showCamera(delegate: delegate)
     }
     }
 
 
+    func showQrCodeInvite(chatId: Int) {
+        let qrInviteCodeController = QrInviteViewController(dcContext: dcContext, chatId: chatId)
+        navigationController.pushViewController(qrInviteCodeController, animated: true)
+    }
+
     func showAddMembers(preselectedMembers: Set<Int>, isVerified: Bool) {
     func showAddMembers(preselectedMembers: Set<Int>, isVerified: Bool) {
         let newGroupController = NewGroupAddMembersViewController(dcContext: dcContext,
         let newGroupController = NewGroupAddMembersViewController(dcContext: dcContext,
                                                                   preselected: preselectedMembers,
                                                                   preselected: preselectedMembers,

+ 12 - 0
deltachat-ios/DC/Wrapper.swift

@@ -42,6 +42,18 @@ class DcContext {
         return Int(dc_create_chat_by_contact_id(contextPointer, UInt32(contactId)))
         return Int(dc_create_chat_by_contact_id(contextPointer, UInt32(contactId)))
     }
     }
 
 
+    func createGroupChat(name: String) -> Int {
+         return Int(dc_create_group_chat(contextPointer, 0, name))
+    }
+
+    func addContactToChat(chatId: Int, contactId: Int) -> Bool {
+        return dc_add_contact_to_chat(contextPointer, UInt32(chatId), UInt32(contactId)) == 1
+    }
+
+    func setChatName(chatId: Int, name: String) -> Bool {
+        return dc_set_chat_name(contextPointer, UInt32(chatId), name) == 1
+    }
+
     func deleteChat(chatId: Int) {
     func deleteChat(chatId: Int) {
         dc_delete_chat(contextPointer, UInt32(chatId))
         dc_delete_chat(contextPointer, UInt32(chatId))
     }
     }