Browse Source

Merge pull request #392 from deltachat/tweak-contact-ids

tweak contact lists
cyBerta 5 năm trước cách đây
mục cha
commit
4158b4c2c5

+ 2 - 5
deltachat-ios/Controller/ChatViewController.swift

@@ -496,11 +496,8 @@ class ChatViewController: MessagesViewController {
                                       preferredStyle: .actionSheet)
         alert.addAction(UIAlertAction(title: String.localized("start_chat"), style: .default, handler: { _ in
             self.dismiss(animated: true, completion: nil)
-            var contactId = Utils.getContactIdByEmail(email)
-            if contactId == nil {
-                contactId = self.dcContext.createContact(name: "", email: email)
-            }
-            let chatId = self.dcContext.createChat(contactId: contactId!)
+            let contactId = self.dcContext.createContact(name: "", email: email)
+            let chatId = self.dcContext.createChat(contactId: contactId)
             self.coordinator?.showChat(chatId: chatId)
         }))
         alert.addAction(UIAlertAction(title: String.localized("cancel"), style: .cancel, handler: { _ in

+ 9 - 10
deltachat-ios/Controller/NewChatViewController.swift

@@ -5,6 +5,8 @@ import UIKit
 class NewChatViewController: UITableViewController {
     weak var coordinator: NewChatCoordinator?
 
+    private let dcContext: DcContext
+
     private let sectionNew = 0
     private let sectionNewRowNewGroup = 0
     private let sectionNewRowNewContact = 1
@@ -24,11 +26,7 @@ class NewChatViewController: UITableViewController {
         return searchController
     }()
 
-    var contactIds: [Int] = Utils.getContactIds() {
-        didSet {
-            tableView.reloadData()
-        }
-    }
+    private var contactIds: [Int]
 
     // contactWithSearchResults.indexesToHightLight empty by default
     var contacts: [ContactWithSearchResults] {
@@ -60,7 +58,9 @@ class NewChatViewController: UITableViewController {
         }
     }
 
-    init() {
+    init(dcContext: DcContext) {
+        self.dcContext = dcContext
+        self.contactIds = dcContext.getContacts(flags: DC_GCL_ADD_SELF)
         super.init(style: .grouped)
         hidesBottomBarWhenPushed = true
     }
@@ -85,7 +85,6 @@ class NewChatViewController: UITableViewController {
     override func viewWillAppear(_ animated: Bool) {
         super.viewWillAppear(animated)
         deviceContactAccessGranted = CNContactStore.authorizationStatus(for: .contacts) == .authorized
-        contactIds = Utils.getContactIds()
     }
 
     override func viewDidAppear(_ animated: Bool) {
@@ -289,7 +288,7 @@ class NewChatViewController: UITableViewController {
         alert.addAction(UIAlertAction(title: String.localized("delete"), style: .destructive, handler: { _ in
             self.dismiss(animated: true, completion: nil)
             if context.deleteContact(contactId: contactId) {
-                self.contactIds = Utils.getContactIds()
+                self.contactIds = self.dcContext.getContacts(flags: DC_GCL_ADD_SELF)
                 self.tableView.reloadData()
             }
         }))
@@ -369,8 +368,8 @@ class NewChatViewController: UITableViewController {
 
 extension NewChatViewController: ContactListDelegate {
     func deviceContactsImported() {
-        contactIds = Utils.getContactIds()
-        //		tableView.reloadData()
+        contactIds = dcContext.getContacts(flags: DC_GCL_ADD_SELF)
+        tableView.reloadData()
     }
 
     func accessGranted() {

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

@@ -138,7 +138,7 @@ class ChatListCoordinator: Coordinator {
     }
 
     func showNewChatController() {
-        let newChatVC = NewChatViewController()
+        let newChatVC = NewChatViewController(dcContext: dcContext)
         let coordinator = NewChatCoordinator(dcContext: dcContext, navigationController: navigationController)
         childCoordinators.append(coordinator)
         newChatVC.coordinator = coordinator

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

@@ -26,6 +26,11 @@ class DcContext {
         return dc_delete_contact(self.contextPointer, UInt32(contactId)) == 1
     }
 
+    func getContacts(flags: Int32) -> [Int] {
+        let cContacts = dc_get_contacts(self.contextPointer, UInt32(flags), nil)
+        return Utils.copyAndFreeArray(inputArray: cContacts)
+    }
+
     func getChatlist(flags: Int32, queryString: String?, queryId: Int) -> DcChatlist {
         let chatlistPointer = dc_get_chatlist(contextPointer, flags, queryString, UInt32(queryId))
         let chatlist = DcChatlist(chatListPointer: chatlistPointer)

+ 2 - 5
deltachat-ios/Helper/Utils.swift

@@ -3,16 +3,13 @@ import UIKit
 import AVFoundation
 
 struct Utils {
+
+    // do not use, use DcContext::getContacts() instead
     static func getContactIds() -> [Int] {
         let cContacts = dc_get_contacts(mailboxPointer, 0, nil)
         return Utils.copyAndFreeArray(inputArray: cContacts)
     }
 
-    static func getContactIdByEmail(_ address: String) -> Int? {
-        let ids = getContactIds()
-        return ids.first(where: { DcContact(id: $0).email == address })
-    }
-
     static func getBlockedContactIds() -> [Int] {
         let cBlockedContacts = dc_get_blocked_contacts(mailboxPointer)
         return Utils.copyAndFreeArray(inputArray: cBlockedContacts)