From da0ea636921e1dd5589f7c90b26fe346c9f5afeb Mon Sep 17 00:00:00 2001 From: Ran Hassid Date: Tue, 8 Aug 2017 12:43:09 +0300 Subject: [PATCH 1/4] add should add contact and replace CNContact with EPContact array --- Contacts Picker/ViewController.swift | 7 +++++- Pods/EPContact.swift | 8 +++++++ Pods/EPContactsPicker.swift | 33 ++++++++++++++++++++++------ 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/Contacts Picker/ViewController.swift b/Contacts Picker/ViewController.swift index 54a3cc3..cf841b2 100644 --- a/Contacts Picker/ViewController.swift +++ b/Contacts Picker/ViewController.swift @@ -7,6 +7,7 @@ // import UIKit +import Contacts class ViewController: UIViewController, EPPickerDelegate { @@ -22,7 +23,7 @@ class ViewController: UIViewController, EPPickerDelegate { @IBAction func onTouchShowMeContactsButton(_ sender: AnyObject) { - let contactPickerScene = EPContactsPicker(delegate: self, multiSelection:true, subtitleCellType: SubtitleCellValue.email) + let contactPickerScene = EPContactsPicker(delegate: self, multiSelection:true, subtitleCellType: SubtitleCellValue.phoneNumber) let navigationController = UINavigationController(rootViewController: contactPickerScene) self.present(navigationController, animated: true, completion: nil) @@ -50,5 +51,9 @@ class ViewController: UIViewController, EPPickerDelegate { print("\(contact.displayName())") } } + + func epContactPicker(_: EPContactsPicker, shouldAddContact contact: EPContact) -> Bool { + return contact.hasPhoneNumbers() + } } diff --git a/Pods/EPContact.swift b/Pods/EPContact.swift index 199f5bb..8ee6ba9 100644 --- a/Pods/EPContact.swift +++ b/Pods/EPContact.swift @@ -67,6 +67,14 @@ open class EPContact { return firstName + " " + lastName } + open func hasPhoneNumbers() -> Bool { + return phoneNumbers.count > 0 + } + + open func hasEmails() -> Bool { + return emails.count > 0 + } + open func contactInitials() -> String { var initials = String() diff --git a/Pods/EPContactsPicker.swift b/Pods/EPContactsPicker.swift index d4d333b..89e9da1 100644 --- a/Pods/EPContactsPicker.swift +++ b/Pods/EPContactsPicker.swift @@ -15,6 +15,7 @@ public protocol EPPickerDelegate: class { func epContactPicker(_: EPContactsPicker, didCancel error: NSError) func epContactPicker(_: EPContactsPicker, didSelectContact contact: EPContact) func epContactPicker(_: EPContactsPicker, didSelectMultipleContacts contacts: [EPContact]) + func epContactPicker(_: EPContactsPicker, shouldAddContact contact: EPContact) -> Bool } public extension EPPickerDelegate { @@ -22,6 +23,7 @@ public extension EPPickerDelegate { func epContactPicker(_: EPContactsPicker, didCancel error: NSError) { } func epContactPicker(_: EPContactsPicker, didSelectContact contact: EPContact) { } func epContactPicker(_: EPContactsPicker, didSelectMultipleContacts contacts: [EPContact]) { } + func epContactPicker(_: EPContactsPicker, shouldAddContact contact: EPContact) -> Bool { return true } } typealias ContactsHandler = (_ contacts : [CNContact] , _ error : NSError?) -> Void @@ -40,11 +42,11 @@ open class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UIS open weak var contactDelegate: EPPickerDelegate? var contactsStore: CNContactStore? var resultSearchController = UISearchController() - var orderedContacts = [String: [CNContact]]() //Contacts ordered in dicitonary alphabetically + var orderedContacts = [String: [EPContact]]() //Contacts ordered in dicitonary alphabetically var sortedContactKeys = [String]() var selectedContacts = [EPContact]() - var filteredContacts = [CNContact]() + var filteredContacts = [EPContact]() var subtitleCellValue = SubtitleCellValue.phoneNumber var multiSelectEnabled: Bool = false //Default is single selection contact @@ -195,15 +197,24 @@ open class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UIS if let firstLetter = contact.givenName[0..<1] , firstLetter.containsAlphabets() { key = firstLetter.uppercased() } - var contacts = [CNContact]() + var contacts = [EPContact]() if let segregatedContact = self.orderedContacts[key] { contacts = segregatedContact } - contacts.append(contact) + + let epContact = EPContact.init(contact: contact) + + if (self.contactDelegate?.epContactPicker(self, shouldAddContact: epContact) == true){ + contacts.append(epContact) + } + self.orderedContacts[key] = contacts }) + + + self.sortedContactKeys = Array(self.orderedContacts.keys).sorted(by: <) if self.sortedContactKeys.first == "#" { self.sortedContactKeys.removeFirst() @@ -258,14 +269,14 @@ open class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UIS let contact: EPContact if resultSearchController.isActive { - contact = EPContact(contact: filteredContacts[(indexPath as NSIndexPath).row]) + contact = filteredContacts[(indexPath as NSIndexPath).row] } else { guard let contactsForSection = orderedContacts[sortedContactKeys[(indexPath as NSIndexPath).section]] else { assertionFailure() return UITableViewCell() } - contact = EPContact(contact: contactsForSection[(indexPath as NSIndexPath).row]) + contact = contactsForSection[(indexPath as NSIndexPath).row] } if multiSelectEnabled && selectedContacts.contains(where: { $0.contactId == contact.contactId }) { @@ -352,11 +363,19 @@ open class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UIS } let store = CNContactStore() + do { - filteredContacts = try store.unifiedContacts(matching: predicate, + + let unifiedContacts = try store.unifiedContacts(matching: predicate, keysToFetch: allowedContactKeys()) //print("\(filteredContacts.count) count") + filteredContacts.removeAll() + + for contact in unifiedContacts { + filteredContacts.append(EPContact.init(contact: contact)) + } + self.tableView.reloadData() } From 190650e80bfda1967811f5f2c44dce08273c354f Mon Sep 17 00:00:00 2001 From: Ran Hassid Date: Tue, 8 Aug 2017 12:57:10 +0300 Subject: [PATCH 2/4] add multi contact selection limit --- Contacts Picker/ViewController.swift | 3 ++- Pods/EPContactsPicker.swift | 30 +++++++++++++++++++++++----- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/Contacts Picker/ViewController.swift b/Contacts Picker/ViewController.swift index cf841b2..7d1244d 100644 --- a/Contacts Picker/ViewController.swift +++ b/Contacts Picker/ViewController.swift @@ -23,7 +23,8 @@ class ViewController: UIViewController, EPPickerDelegate { @IBAction func onTouchShowMeContactsButton(_ sender: AnyObject) { - let contactPickerScene = EPContactsPicker(delegate: self, multiSelection:true, subtitleCellType: SubtitleCellValue.phoneNumber) +// let contactPickerScene = EPContactsPicker(delegate: self, multiSelection:true, subtitleCellType: SubtitleCellValue.phoneNumber) + let contactPickerScene = EPContactsPicker(delegate: self, multiSelection: true, multiSelectionLimit: 2) let navigationController = UINavigationController(rootViewController: contactPickerScene) self.present(navigationController, animated: true, completion: nil) diff --git a/Pods/EPContactsPicker.swift b/Pods/EPContactsPicker.swift index 89e9da1..ebcde31 100644 --- a/Pods/EPContactsPicker.swift +++ b/Pods/EPContactsPicker.swift @@ -50,6 +50,7 @@ open class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UIS var subtitleCellValue = SubtitleCellValue.phoneNumber var multiSelectEnabled: Bool = false //Default is single selection contact + var multiSelectContactLimit : UInt = 0 // MARK: - Lifecycle Methods @@ -122,14 +123,30 @@ open class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UIS convenience public init(delegate: EPPickerDelegate?, multiSelection : Bool) { self.init(style: .plain) self.multiSelectEnabled = multiSelection - contactDelegate = delegate + self.contactDelegate = delegate } convenience public init(delegate: EPPickerDelegate?, multiSelection : Bool, subtitleCellType: SubtitleCellValue) { self.init(style: .plain) self.multiSelectEnabled = multiSelection - contactDelegate = delegate - subtitleCellValue = subtitleCellType + self.contactDelegate = delegate + self.subtitleCellValue = subtitleCellType + } + + convenience public init(delegate: EPPickerDelegate?, multiSelection : Bool, multiSelectionLimit: UInt) { + + self.init(style: .plain) + self.multiSelectEnabled = multiSelection + self.multiSelectContactLimit = multiSelectionLimit + self.contactDelegate = delegate + } + + convenience public init(delegate: EPPickerDelegate?, multiSelection : Bool, multiSelectionLimit: UInt, subtitleCellType: SubtitleCellValue) { + self.init(style: .plain) + self.multiSelectEnabled = multiSelection + self.multiSelectContactLimit = multiSelectionLimit + self.subtitleCellValue = subtitleCellType + self.contactDelegate = delegate } @@ -299,7 +316,7 @@ open class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UIS return selectedContact.contactId != $0.contactId } } - else { + else if (self.multiSelectContactLimit == 0 || self.selectedContacts.count < Int(self.multiSelectContactLimit)) { cell.accessoryType = UITableViewCellAccessoryType.checkmark selectedContacts.append(selectedContact) } @@ -373,7 +390,10 @@ open class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UIS filteredContacts.removeAll() for contact in unifiedContacts { - filteredContacts.append(EPContact.init(contact: contact)) + let epContact = EPContact.init(contact: contact) + if (self.contactDelegate?.epContactPicker(self, shouldAddContact: epContact) == true){ + filteredContacts.append(EPContact.init(contact: contact)) + } } self.tableView.reloadData() From 97fe91a142b2d9004c5df90fd3035e81cc315af9 Mon Sep 17 00:00:00 2001 From: Ran Hassid Date: Tue, 8 Aug 2017 13:02:32 +0300 Subject: [PATCH 3/4] consider selection keys in results --- Pods/EPContactsPicker.swift | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/Pods/EPContactsPicker.swift b/Pods/EPContactsPicker.swift index ebcde31..93b7f16 100644 --- a/Pods/EPContactsPicker.swift +++ b/Pods/EPContactsPicker.swift @@ -207,27 +207,27 @@ open class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UIS do { try contactsStore?.enumerateContacts(with: contactFetchRequest, usingBlock: { (contact, stop) -> Void in - //Ordering contacts based on alphabets in firstname - contactsArray.append(contact) - var key: String = "#" - //If ordering has to be happening via family name change it here. - if let firstLetter = contact.givenName[0..<1] , firstLetter.containsAlphabets() { - key = firstLetter.uppercased() - } - var contacts = [EPContact]() - - if let segregatedContact = self.orderedContacts[key] { - contacts = segregatedContact - } let epContact = EPContact.init(contact: contact) - if (self.contactDelegate?.epContactPicker(self, shouldAddContact: epContact) == true){ + if (self.contactDelegate?.epContactPicker(self, shouldAddContact: epContact)) == true { + //Ordering contacts based on alphabets in firstname + contactsArray.append(contact) + var key: String = "#" + //If ordering has to be happening via family name change it here. + if let firstLetter = contact.givenName[0..<1] , firstLetter.containsAlphabets() { + key = firstLetter.uppercased() + } + var contacts = [EPContact]() + + if let segregatedContact = self.orderedContacts[key] { + contacts = segregatedContact + } + contacts.append(epContact) - } - - self.orderedContacts[key] = contacts - + + self.orderedContacts[key] = contacts + } }) From 91015eb10d94aeaef5db43c4906e44a6b1372041 Mon Sep 17 00:00:00 2001 From: Ran Hassid Date: Tue, 8 Aug 2017 13:33:02 +0300 Subject: [PATCH 4/4] use default view title only if its nil --- Pods/EPContactsPicker.swift | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Pods/EPContactsPicker.swift b/Pods/EPContactsPicker.swift index 93b7f16..f7481ca 100644 --- a/Pods/EPContactsPicker.swift +++ b/Pods/EPContactsPicker.swift @@ -56,7 +56,11 @@ open class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UIS override open func viewDidLoad() { super.viewDidLoad() - self.title = EPGlobalConstants.Strings.contactsTitle + + + if self.title == nil { + self.title = EPGlobalConstants.Strings.contactsTitle + } registerContactCell() inititlizeBarButtons() @@ -227,7 +231,7 @@ open class EPContactsPicker: UITableViewController, UISearchResultsUpdating, UIS contacts.append(epContact) self.orderedContacts[key] = contacts - } + } })