diff --git a/ContactsApp/Base.lproj/Main.storyboard b/ContactsApp/Base.lproj/Main.storyboard index 5a1b67a..9bf56c1 100644 --- a/ContactsApp/Base.lproj/Main.storyboard +++ b/ContactsApp/Base.lproj/Main.storyboard @@ -16,7 +16,7 @@ - + @@ -33,11 +33,21 @@ + + + + + + + + - + + + @@ -46,6 +56,7 @@ + diff --git a/ContactsApp/ContactsViewController.swift b/ContactsApp/ContactsViewController.swift index e625e06..38c2458 100644 --- a/ContactsApp/ContactsViewController.swift +++ b/ContactsApp/ContactsViewController.swift @@ -11,8 +11,10 @@ import ContactsUI class ContactsViewController: UIViewController { + @IBOutlet weak var searchBar: UISearchBar! @IBOutlet weak var contactsTable: UITableView! var contactStructArray = [ContactStruct]() + var contactAux = [ContactStruct]() var contactStruct : ContactStruct? let contactStore = CNContactStore() @@ -20,11 +22,13 @@ class ContactsViewController: UIViewController { super.viewDidLoad() contactsTable.register(UINib(nibName: "ContactSection", bundle: nil), forHeaderFooterViewReuseIdentifier: "headerId") importContacts() + searchBar.delegate = self contactsTable.tableFooterView = UIView(frame: .zero) + searchBar.placeholder = NSLocalizedString("Nombre...", comment: "") } func importContacts() { - + self.contactStructArray = [] let keys = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey] as [Any] let request = CNContactFetchRequest( keysToFetch: keys as! [CNKeyDescriptor]) request.sortOrder = CNContactSortOrder.givenName @@ -48,10 +52,10 @@ class ContactsViewController: UIViewController { auxLetter = String(contact.givenName.prefix(1)) //auxLetter tiene la primera letra del nombre isNotTheFirst = true //la siguiente vez que las letras no sean iguales guardara en el array } - } } self.contactStructArray.append(ContactStruct(isExpanded: true, contacts: contacts, letter: auxLetter)) //agregamos el ultimo nombre + self.contactAux = self.contactStructArray } catch { print("unable to fetch contacts") @@ -142,3 +146,38 @@ extension ContactsViewController: UITableViewDelegate, UITableViewDataSource { } } + +extension ContactsViewController: UISearchBarDelegate, UISearchDisplayDelegate, UISearchResultsUpdating { + + func updateSearchResults(for searchController: UISearchController) { + + } + + func searchBarCancelButtonClicked(_ searchBar: UISearchBar) { + contactStructArray = contactAux + } + + func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) { + var foundContacts = [ContactStruct]() + + if searchText == "" { + contactStructArray = contactAux + contactsTable.reloadData() + } else { + for indexContact in contactAux.indices{ + let contactFound = contactAux[indexContact].contacts.filter( + { + let fullName = "\($0.givenName) \($0.familyName)" + return fullName.lowercased().contains(searchText.lowercased()) + } + ) + + if !contactFound.isEmpty { + foundContacts.append(ContactStruct(isExpanded: true, contacts: contactFound, letter: contactAux[indexContact].letter)) + } + } + contactStructArray = foundContacts + contactsTable.reloadData() + } + } +}