Commit 7abcdb34 by Javier Heisekce

Merge branch 'feature/searchBar'

parents e2c874c1 5c06c519
......@@ -16,7 +16,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="dBj-cU-Ugw">
<rect key="frame" x="0.0" y="44" width="375" height="623"/>
<rect key="frame" x="0.0" y="100" width="375" height="567"/>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<prototypes>
<tableViewCell clipsSubviews="YES" contentMode="scaleToFill" preservesSuperviewLayoutMargins="YES" selectionStyle="default" indentationWidth="10" reuseIdentifier="contactName" id="QkB-8k-kbX">
......@@ -33,11 +33,21 @@
<outlet property="delegate" destination="l7R-c3-U8p" id="pgD-yj-lEC"/>
</connections>
</tableView>
<searchBar contentMode="redraw" translatesAutoresizingMaskIntoConstraints="NO" id="2yO-jM-dTD">
<rect key="frame" x="0.0" y="44" width="375" height="56"/>
<textInputTraits key="textInputTraits"/>
<connections>
<outlet property="delegate" destination="U0h-dc-vbE" id="LUn-MP-HEY"/>
</connections>
</searchBar>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<constraints>
<constraint firstItem="dBj-cU-Ugw" firstAttribute="top" secondItem="2yO-jM-dTD" secondAttribute="bottom" id="8nd-C7-lbC"/>
<constraint firstItem="ig1-pz-8Nn" firstAttribute="trailing" secondItem="dBj-cU-Ugw" secondAttribute="trailing" id="HCT-3z-lWu"/>
<constraint firstItem="dBj-cU-Ugw" firstAttribute="top" secondItem="ig1-pz-8Nn" secondAttribute="top" id="NqI-Ku-x9S"/>
<constraint firstItem="2yO-jM-dTD" firstAttribute="top" secondItem="ig1-pz-8Nn" secondAttribute="top" id="Id5-HP-nkN"/>
<constraint firstItem="2yO-jM-dTD" firstAttribute="trailing" secondItem="ig1-pz-8Nn" secondAttribute="trailing" id="b9K-lU-zTt"/>
<constraint firstItem="2yO-jM-dTD" firstAttribute="leading" secondItem="ig1-pz-8Nn" secondAttribute="leading" id="dOI-p7-1BY"/>
<constraint firstItem="dBj-cU-Ugw" firstAttribute="leading" secondItem="ig1-pz-8Nn" secondAttribute="leading" id="ecL-bI-50d"/>
<constraint firstItem="ig1-pz-8Nn" firstAttribute="bottom" secondItem="dBj-cU-Ugw" secondAttribute="bottom" id="ydk-Ee-Ybk"/>
</constraints>
......@@ -46,6 +56,7 @@
<navigationItem key="navigationItem" id="U0h-dc-vbE"/>
<connections>
<outlet property="contactsTable" destination="dBj-cU-Ugw" id="8YS-LO-fy0"/>
<outlet property="searchBar" destination="2yO-jM-dTD" id="4EW-JC-tfj"/>
<segue destination="i2T-MY-6qf" kind="show" identifier="gotocontact" id="1Ts-QM-UT7"/>
</connections>
</viewController>
......
......@@ -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()
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment