Prechádzať zdrojové kódy

added device contact import to contact list

Bastian van de Wetering 6 rokov pred
rodič
commit
ff1ba83d5a

+ 1 - 2
deltachat-ios/Controller/AccountSetupController.swift

@@ -15,7 +15,6 @@ class AccountSetupController: UITableViewController {
 	weak var coordinator: AccountSetupCoordinator?
 
 	private var skipOauth = false
-	private var useCustomSettings = false
 
 	private var backupProgressObserver: Any?
 	private var configureProgressObserver: Any?
@@ -358,7 +357,7 @@ class AccountSetupController: UITableViewController {
 		MRConfig.addr = emailAddress
 		MRConfig.mailPw = password
 
-		if useCustomSettings && !skipAdvanceSetup {
+		if !skipAdvanceSetup {
 			evaluluateAdvancedSetup() // this will set MRConfig related to advanced fields
 		}
 

+ 76 - 12
deltachat-ios/Controller/ContactListController.swift

@@ -7,6 +7,7 @@
 //
 
 import UIKit
+import Contacts
 
 class ContactListController: UITableViewController {
   weak var coordinator: ContactListCoordinator?
@@ -15,13 +16,26 @@ class ContactListController: UITableViewController {
   var contactIds: [Int] = Utils.getContactIds()
   var contactIdsForGroup: Set<Int> = []
 
+	lazy var deviceContactHandler: DeviceContactsHandler = {
+		let handler = DeviceContactsHandler()
+		handler.contactListDelegate = self
+		return handler
+	}()
+
+	var deviceContactAccessGranted: Bool = false {
+		didSet {
+			tableView.reloadData()
+		}
+	}
+
   override func viewDidLoad() {
     super.viewDidLoad()
     title = "Contacts"
     navigationController?.navigationBar.prefersLargeTitles = true
 
-    tableView.rowHeight = 80
+   // tableView.rowHeight = 80
     tableView.register(ContactCell.self, forCellReuseIdentifier: contactCellReuseIdentifier)
+		tableView.register(ActionCell.self, forCellReuseIdentifier: "actionCell")
   }
 
   private func getContactIds() {
@@ -31,11 +45,11 @@ class ContactListController: UITableViewController {
 
   override func viewWillAppear(_ animated: Bool) {
     super.viewWillAppear(animated)
-
     if #available(iOS 11.0, *) {
       navigationController?.navigationBar.prefersLargeTitles = true
     }
-
+		deviceContactHandler.importDeviceContacts()
+		deviceContactAccessGranted = CNContactStore.authorizationStatus(for: .contacts) == .authorized
     getContactIds()
   }
 
@@ -51,15 +65,31 @@ class ContactListController: UITableViewController {
   }
 
   override func numberOfSections(in _: UITableView) -> Int {
-    return 1
+		return deviceContactAccessGranted ? 1 : 2
   }
 
-  override func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
+  override func tableView(_: UITableView, numberOfRowsInSection section: Int) -> Int {
+		if !deviceContactAccessGranted && section == 0 {
+			return 1
+		}
     return contactIds.count
   }
 
   override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-    let cell: ContactCell
+		let section = indexPath.section
+
+		if !deviceContactAccessGranted && section == 0 {
+			let cell: ActionCell
+			if let c = tableView.dequeueReusableCell(withIdentifier: "actionCell") as? ActionCell {
+				cell = c
+			} else {
+				cell = ActionCell(style: .default, reuseIdentifier: "actionCell")
+			}
+			cell.actionTitle = "Import Device Contacts"
+			return cell
+		} else {
+
+		let cell: ContactCell
     if let c = tableView.dequeueReusableCell(withIdentifier: contactCellReuseIdentifier) as? ContactCell {
       cell = c
     } else {
@@ -83,15 +113,20 @@ class ContactListController: UITableViewController {
       cell.setVerified(isVerified: contact.isVerified)
     }
     return cell
+		}
   }
 
   override func tableView(_: UITableView, didSelectRowAt indexPath: IndexPath) {
-    let contactId = contactIds[indexPath.row]
-		let chatId = dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId))
-
-		coordinator?.showChat(chatId: Int(chatId))
-		// coordinator?.showContactDetail(contactId: contactId)
-  }
+		if !deviceContactAccessGranted && indexPath.section == 0 {
+			showSettingsAlert()
+		} else {
+			let contactId = contactIds[indexPath.row]
+			let chatId = dc_create_chat_by_contact_id(mailboxPointer, UInt32(contactId))
+
+			coordinator?.showChat(chatId: Int(chatId))
+			// coordinator?.showContactDetail(contactId: contactId)
+		}
+	}
 
 	override func tableView(_ tableView: UITableView, editActionsForRowAt indexPath: IndexPath) -> [UITableViewRowAction]? {
 		let row = indexPath.row
@@ -107,3 +142,32 @@ class ContactListController: UITableViewController {
 		return [edit]
 	}
 }
+
+extension ContactListController: ContactListDelegate {
+	func deviceContactsImported() {
+		contactIds = Utils.getContactIds()
+	}
+
+	func accessGranted() {
+		deviceContactAccessGranted = true
+	}
+
+	func accessDenied() {
+		deviceContactAccessGranted = false
+		getContactIds()
+	}
+
+	private func showSettingsAlert() {
+		let alert = UIAlertController(
+			title: "Import Contacts from to your device",
+			message: "To chat with contacts from your device open the settings menu and enable the Contacts option",
+			preferredStyle: .alert
+		)
+		alert.addAction(UIAlertAction(title: "Open Settings", style: .default) { _ in
+			UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
+		})
+		alert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in
+		})
+		present(alert, animated: true)
+	}
+}

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

@@ -36,7 +36,7 @@ final class DCNavigationController: UINavigationController {
   }
 
 	override func viewWillAppear(_ animated: Bool) {
-
+		super.viewWillAppear(animated)
 		if let connection = Reachability()?.connection {
 			switch connection {
 			case Reachability.Connection.cellular, Reachability.Connection.wifi:

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

@@ -34,7 +34,7 @@ class AppCoordinator: NSObject, Coordinator {
 	// MARK: viewControllers
 
 	private lazy var contactListController: UIViewController = {
-		let controller = ContactListController()
+		let controller = ContactListController(style: .grouped)
 		let nav = DCNavigationController(rootViewController: controller)
 		let settingsImage = UIImage(named: "contacts")
 		nav.tabBarItem = UITabBarItem(title: "Contacts", image: settingsImage, tag: 0)