Commit 75f30cac by Luis Zarza

search-bar feature added

parent 2ec83e34
......@@ -50,10 +50,11 @@
</tableViewCell>
</prototypes>
</tableView>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" fixedFrame="YES" image="roshka" translatesAutoresizingMaskIntoConstraints="NO" id="uXr-7H-u4l">
<rect key="frame" x="157" y="96" width="100" height="90"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
</imageView>
<searchBar contentMode="redraw" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="6X6-0e-OoT">
<rect key="frame" x="0.0" y="113" width="414" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<textInputTraits key="textInputTraits"/>
</searchBar>
</subviews>
<color key="backgroundColor" red="0.92132973669999996" green="0.93007987739999998" blue="0.952917397" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
......@@ -66,6 +67,7 @@
<navigationItem key="navigationItem" title="Contacts" id="bfK-iM-1Sh"/>
<connections>
<outlet property="contacsTable" destination="yEq-E4-J6w" id="zMq-Ro-NJS"/>
<outlet property="searchBar" destination="6X6-0e-OoT" id="v0q-Sp-8An"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
......@@ -205,6 +207,5 @@
<image name="envelope" catalog="system" width="64" height="46"/>
<image name="person.circle.fill" catalog="system" width="64" height="60"/>
<image name="phone.fill" catalog="system" width="64" height="56"/>
<image name="roshka" width="400" height="400"/>
</resources>
</document>
......@@ -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
}
}
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