Commit 75f30cac by Luis Zarza

search-bar feature added

parent 2ec83e34
...@@ -50,10 +50,11 @@ ...@@ -50,10 +50,11 @@
</tableViewCell> </tableViewCell>
</prototypes> </prototypes>
</tableView> </tableView>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="roshka" translatesAutoresizingMaskIntoConstraints="NO" id="uXr-7H-u4l"> <searchBar contentMode="redraw" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="6X6-0e-OoT">
<rect key="frame" x="157" y="96" width="100" height="90"/> <rect key="frame" x="0.0" y="113" width="414" height="44"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> <autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
</imageView> <textInputTraits key="textInputTraits"/>
</searchBar>
</subviews> </subviews>
<color key="backgroundColor" red="0.92132973669999996" green="0.93007987739999998" blue="0.952917397" alpha="1" colorSpace="custom" customColorSpace="sRGB"/> <color key="backgroundColor" red="0.92132973669999996" green="0.93007987739999998" blue="0.952917397" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints> <constraints>
...@@ -66,6 +67,7 @@ ...@@ -66,6 +67,7 @@
<navigationItem key="navigationItem" title="Contacts" id="bfK-iM-1Sh"/> <navigationItem key="navigationItem" title="Contacts" id="bfK-iM-1Sh"/>
<connections> <connections>
<outlet property="contacsTable" destination="yEq-E4-J6w" id="zMq-Ro-NJS"/> <outlet property="contacsTable" destination="yEq-E4-J6w" id="zMq-Ro-NJS"/>
<outlet property="searchBar" destination="6X6-0e-OoT" id="v0q-Sp-8An"/>
</connections> </connections>
</viewController> </viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/> <placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
...@@ -205,6 +207,5 @@ ...@@ -205,6 +207,5 @@
<image name="envelope" catalog="system" width="64" height="46"/> <image name="envelope" catalog="system" width="64" height="46"/>
<image name="person.circle.fill" catalog="system" width="64" height="60"/> <image name="person.circle.fill" catalog="system" width="64" height="60"/>
<image name="phone.fill" catalog="system" width="64" height="56"/> <image name="phone.fill" catalog="system" width="64" height="56"/>
<image name="roshka" width="400" height="400"/>
</resources> </resources>
</document> </document>
...@@ -20,9 +20,9 @@ class ViewController: UIViewController { ...@@ -20,9 +20,9 @@ class ViewController: UIViewController {
let contactStore = CNContactStore() let contactStore = CNContactStore()
var contacts = [CNContact]() var contacts = [CNContact]()
var dataSource:[ContactList]? = [] var dataSource:[ContactList]? = []
var backUpContacts:[CNContact] = [] var contactsArray: [ContactList]? = []
var isHidden = false var isHidden = false
@IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var contacsTable: UITableView! @IBOutlet weak var contacsTable: UITableView!
override func viewDidLoad() { override func viewDidLoad() {
...@@ -41,6 +41,7 @@ class ViewController: UIViewController { ...@@ -41,6 +41,7 @@ class ViewController: UIViewController {
//set tableView handlers to this instance //set tableView handlers to this instance
contacsTable.dataSource = self contacsTable.dataSource = self
contacsTable.delegate = self contacsTable.delegate = self
searchBar.delegate = self
} }
func sortContacts(){ func sortContacts(){
...@@ -52,19 +53,20 @@ class ViewController: UIViewController { ...@@ -52,19 +53,20 @@ class ViewController: UIViewController {
temp.append(contacts[i]) temp.append(contacts[i])
}else{ }else{
temp.append(contacts[i]) temp.append(contacts[i])
dataSource!.append(ContactList(firstLetter: String(pivot), contactList: temp)) contactsArray!.append(ContactList(firstLetter: String(pivot), contactList: temp))
pivot = contacts[i+1].givenName.prefix(1) pivot = contacts[i+1].givenName.prefix(1)
temp = [] temp = []
} }
} }
//ask for the last contact //ask for the last contact
if contacts[contacts.count-1].givenName.prefix(1) == dataSource![dataSource!.count-1].firstLetter{ //last group if contacts[contacts.count-1].givenName.prefix(1) == contactsArray![contactsArray!.count-1].firstLetter{ //last group
dataSource![dataSource!.count-1].contactList.append(contacts[contacts.count-1]) contactsArray![contactsArray!.count-1].contactList.append(contacts[contacts.count-1])
}else{ //new group }else{ //new group
dataSource!.append(ContactList(firstLetter: String(pivot), contactList: [contacts[contacts.count-1]])) contactsArray!.append(ContactList(firstLetter: String(pivot), contactList: [contacts[contacts.count-1]]))
} }
dataSource! = contactsArray!
} }
...@@ -178,3 +180,40 @@ extension ViewController: UITableViewDelegate, UITableViewDataSource{ ...@@ -178,3 +180,40 @@ extension ViewController: UITableViewDelegate, UITableViewDataSource{
show(contacProfileVC, sender: nil) show(contacProfileVC, sender: nil)
} }
} }
extension ViewController: UISearchBarDelegate{
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
dataSource! = contactsArray!
if searchText.isEmpty == false {
dataSource! = filterContacts(searchText:searchText)
contacsTable.reloadData()
}else{
contacsTable.reloadData()
}
}
func filterContacts(searchText:String)->[ContactList]{
var filteredContacts:[ContactList] = []
var contactsTemp:[CNContact] = []
contactsArray!.forEach{contact in
contact.contactList.forEach{contacto in
var fullName = contacto.givenName + contacto.familyName
if fullName.uppercased().contains(searchText.uppercased()) {
contactsTemp.append(contacto)
}
}
//en caso de que la seccion tenga coincidencias
if contactsTemp.count > 0 {
var contactListAux = ContactList(firstLetter: String(contactsTemp[0].givenName.prefix(1)), contactList: contactsTemp)
filteredContacts.append(contactListAux) //se agrega a la lista filtrada
contactsTemp = []
}
}
return filteredContacts
}
}
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