Parcourir la source

import addressbook into core

Bastian van de Wetering il y a 6 ans
Parent
commit
f2c53cd3cb

+ 19 - 8
deltachat-ios/DeviceContactsHandler.swift

@@ -21,10 +21,21 @@ class DeviceContactsHandler {
 	private let store = CNContactStore()
 
 
-	public func getContacts() -> [DeviceContact] {
+	private func makeContactString(contacts: [CNContact]) -> String {
+		var contactString:String = ""
+		for contact in contacts {
+			let displayName: String = "\(contact.givenName) \(contact.familyName)"
+			// cnContact can have multiple email addresses -> create contact for each email address
+			for emailAddress in contact.emailAddresses {
+				contactString += "\(displayName)\n\(emailAddress.value)\n"
+			}
+		}
+		return contactString
+	}
+
+	private func getContacts() -> String {
 			let storedContacts = self.fetchContactsWithEmailFromDevice()
-			let contacts = storedContacts.map({self.makeContact(from: $0)})
-		return contacts
+			return makeContactString(contacts: storedContacts)
 	}
 
 
@@ -60,19 +71,19 @@ class DeviceContactsHandler {
 		return DeviceContact(displayName: displayName, emailAddresses: emailAdresses)
 	}
 
-	func requestDeviceContacts(delegate: DeviceContactsDelegate) {
+	func importDeviceContacts(delegate: DeviceContactsDelegate) {
 		switch CNContactStore.authorizationStatus(for: .contacts) {
 		case .authorized:
-			let contacts = getContacts()
-			delegate.setContacts(contacts: contacts)
+			let contactString = getContacts()
+			delegate.setContacts(contactString: contactString)
 		case .denied:
 			delegate.accessDenied()
 		case .restricted, .notDetermined:
 			store.requestAccess(for: .contacts) {[unowned self] granted, error in
 				if granted {
 					DispatchQueue.main.async {
-						let contacts = self.getContacts()
-						delegate.setContacts(contacts: contacts)
+						let contactString = self.getContacts()
+						delegate.setContacts(contactString: contactString)
 					}
 				} else {
 					DispatchQueue.main.async {

+ 14 - 6
deltachat-ios/NewChatViewController.swift

@@ -23,6 +23,11 @@ class NewChatViewController: UITableViewController {
   var hud: ProgressHud?
 
 	let deviceContactHandler = DeviceContactsHandler()
+	var deviceContactAccessGranted: Bool = false {
+		didSet {
+			tableView.reloadData()
+		}
+	}
 
 
   override func viewDidLoad() {
@@ -32,14 +37,14 @@ class NewChatViewController: UITableViewController {
     navigationController?.navigationBar.prefersLargeTitles = true
 
     let cancelButton = UIBarButtonItem(barButtonSystemItem: .cancel, target: self, action: #selector(NewChatViewController.cancelButtonPressed))
-
     navigationItem.rightBarButtonItem = cancelButton
+
+		deviceContactHandler.importDeviceContacts(delegate: self)
   }
 
   override func viewDidAppear(_ animated: Bool) {
     super.viewDidAppear(animated)
 
-		let contacts = deviceContactHandler.requestDeviceContacts(delegate: self)
 
     contactIds = Utils.getContactIds()
     tableView.reloadData()
@@ -230,12 +235,15 @@ extension NewChatViewController: QrCodeReaderDelegate {
 }
 
 extension NewChatViewController: DeviceContactsDelegate {
-	func setContacts(contacts: [DeviceContact]) {
-		print(contacts)
+	func setContacts(contactString: String) {
+		let number = dc_add_address_book(mailboxPointer, contactString)
+		self.deviceContactAccessGranted = true
+		print(number)
 	}
 
+
 	func accessDenied() {
-		
+		self.deviceContactAccessGranted = false
 	}
 
 	func requestAccess() {
@@ -258,7 +266,7 @@ extension NewChatViewController: DeviceContactsDelegate {
 }
 
 protocol DeviceContactsDelegate {
-	func setContacts(contacts: [DeviceContact])
+	func setContacts(contactString: String)
 	func accessDenied()
 	func requestAccess()
 }