diff --git a/contact-list-app/Base.lproj/Main.storyboard b/contact-list-app/Base.lproj/Main.storyboard
index 9ef0d3a..e05204e 100644
--- a/contact-list-app/Base.lproj/Main.storyboard
+++ b/contact-list-app/Base.lproj/Main.storyboard
@@ -50,10 +50,11 @@
-
-
-
-
+
+
+
+
+
@@ -66,6 +67,7 @@
+
@@ -205,6 +207,5 @@
-
diff --git a/contact-list-app/ViewController.swift b/contact-list-app/ViewController.swift
index d1a85cd..4673e8e 100644
--- a/contact-list-app/ViewController.swift
+++ b/contact-list-app/ViewController.swift
@@ -20,11 +20,11 @@ class ViewController: UIViewController {
let contactStore = CNContactStore()
var contacts = [CNContact]()
var dataSource:[ContactList]? = []
- var backUpContacts:[CNContact] = []
+ var contactsArray: [ContactList]? = []
var isHidden = false
-
+ @IBOutlet weak var searchBar: UISearchBar!
@IBOutlet weak var contacsTable: UITableView!
-
+
override func viewDidLoad() {
super.viewDidLoad()
//call the main functions
@@ -41,6 +41,7 @@ class ViewController: UIViewController {
//set tableView handlers to this instance
contacsTable.dataSource = self
contacsTable.delegate = self
+ searchBar.delegate = self
}
func sortContacts(){
@@ -52,19 +53,20 @@ class ViewController: UIViewController {
temp.append(contacts[i])
}else{
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)
temp = []
}
}
//ask for the last contact
- if contacts[contacts.count-1].givenName.prefix(1) == dataSource![dataSource!.count-1].firstLetter{ //last group
- dataSource![dataSource!.count-1].contactList.append(contacts[contacts.count-1])
+ if contacts[contacts.count-1].givenName.prefix(1) == contactsArray![contactsArray!.count-1].firstLetter{ //last group
+ contactsArray![contactsArray!.count-1].contactList.append(contacts[contacts.count-1])
}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!
}
@@ -173,8 +175,45 @@ extension ViewController: UITableViewDelegate, UITableViewDataSource{
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
- let contacProfileVC = self.storyboard?.instantiateViewController(withIdentifier: "contactProfileID") as! ContactProfileVC
+ let contacProfileVC = self.storyboard?.instantiateViewController(withIdentifier: "contactProfileID") as! ContactProfileVC
contacProfileVC.contact = dataSource![indexPath.section].contactList[indexPath.row]
- 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
+ }
+}