Commit 6bf83153 by Javier Heisekce

Agrega ordenamiento de contactos por nombre y divide en secciones

parent aae30271
...@@ -15,6 +15,8 @@ ...@@ -15,6 +15,8 @@
CEC259D8241FAC79005237F5 /* ContactStruct.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC259D7241FAC79005237F5 /* ContactStruct.swift */; }; CEC259D8241FAC79005237F5 /* ContactStruct.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC259D7241FAC79005237F5 /* ContactStruct.swift */; };
CEC9A37F241BFBD000F024EE /* ContactsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC9A37E241BFBD000F024EE /* ContactsViewController.swift */; }; CEC9A37F241BFBD000F024EE /* ContactsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC9A37E241BFBD000F024EE /* ContactsViewController.swift */; };
CEC9A381241C096D00F024EE /* SingleContactViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC9A380241C096D00F024EE /* SingleContactViewController.swift */; }; CEC9A381241C096D00F024EE /* SingleContactViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = CEC9A380241C096D00F024EE /* SingleContactViewController.swift */; };
CED5EABE242A4CF800E22547 /* ContactSection.xib in Resources */ = {isa = PBXBuildFile; fileRef = CED5EABD242A4CF800E22547 /* ContactSection.xib */; };
CED5EAC2242A509800E22547 /* ContactSectionTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = CED5EAC1242A509800E22547 /* ContactSectionTableViewCell.swift */; };
/* End PBXBuildFile section */ /* End PBXBuildFile section */
/* Begin PBXFileReference section */ /* Begin PBXFileReference section */
...@@ -28,6 +30,8 @@ ...@@ -28,6 +30,8 @@
CEC259D7241FAC79005237F5 /* ContactStruct.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactStruct.swift; sourceTree = "<group>"; }; CEC259D7241FAC79005237F5 /* ContactStruct.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactStruct.swift; sourceTree = "<group>"; };
CEC9A37E241BFBD000F024EE /* ContactsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsViewController.swift; sourceTree = "<group>"; }; CEC9A37E241BFBD000F024EE /* ContactsViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactsViewController.swift; sourceTree = "<group>"; };
CEC9A380241C096D00F024EE /* SingleContactViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingleContactViewController.swift; sourceTree = "<group>"; }; CEC9A380241C096D00F024EE /* SingleContactViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SingleContactViewController.swift; sourceTree = "<group>"; };
CED5EABD242A4CF800E22547 /* ContactSection.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = ContactSection.xib; sourceTree = "<group>"; };
CED5EAC1242A509800E22547 /* ContactSectionTableViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactSectionTableViewCell.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */ /* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */ /* Begin PBXFrameworksBuildPhase section */
...@@ -69,6 +73,8 @@ ...@@ -69,6 +73,8 @@
CEC9A37E241BFBD000F024EE /* ContactsViewController.swift */, CEC9A37E241BFBD000F024EE /* ContactsViewController.swift */,
CEC9A380241C096D00F024EE /* SingleContactViewController.swift */, CEC9A380241C096D00F024EE /* SingleContactViewController.swift */,
CEC259D7241FAC79005237F5 /* ContactStruct.swift */, CEC259D7241FAC79005237F5 /* ContactStruct.swift */,
CED5EABD242A4CF800E22547 /* ContactSection.xib */,
CED5EAC1242A509800E22547 /* ContactSectionTableViewCell.swift */,
); );
path = ContactsApp; path = ContactsApp;
sourceTree = "<group>"; sourceTree = "<group>";
...@@ -131,6 +137,7 @@ ...@@ -131,6 +137,7 @@
isa = PBXResourcesBuildPhase; isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
CED5EABE242A4CF800E22547 /* ContactSection.xib in Resources */,
CE3351F8241A65D500BA57CA /* LaunchScreen.storyboard in Resources */, CE3351F8241A65D500BA57CA /* LaunchScreen.storyboard in Resources */,
CE3351F5241A65D500BA57CA /* Assets.xcassets in Resources */, CE3351F5241A65D500BA57CA /* Assets.xcassets in Resources */,
CE3351F3241A65D400BA57CA /* Main.storyboard in Resources */, CE3351F3241A65D400BA57CA /* Main.storyboard in Resources */,
...@@ -144,6 +151,7 @@ ...@@ -144,6 +151,7 @@
isa = PBXSourcesBuildPhase; isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647; buildActionMask = 2147483647;
files = ( files = (
CED5EAC2242A509800E22547 /* ContactSectionTableViewCell.swift in Sources */,
CEC259D8241FAC79005237F5 /* ContactStruct.swift in Sources */, CEC259D8241FAC79005237F5 /* ContactStruct.swift in Sources */,
CE3351EC241A65D400BA57CA /* AppDelegate.swift in Sources */, CE3351EC241A65D400BA57CA /* AppDelegate.swift in Sources */,
CEC9A37F241BFBD000F024EE /* ContactsViewController.swift in Sources */, CEC9A37F241BFBD000F024EE /* ContactsViewController.swift in Sources */,
......
...@@ -12,4 +12,5 @@ import ContactsUI ...@@ -12,4 +12,5 @@ import ContactsUI
struct ContactStruct { struct ContactStruct {
var isExpanded: Bool var isExpanded: Bool
let contacts : [CNContact] let contacts : [CNContact]
let letter : String
} }
...@@ -12,9 +12,8 @@ import ContactsUI ...@@ -12,9 +12,8 @@ import ContactsUI
class ContactsViewController: UIViewController { class ContactsViewController: UIViewController {
@IBOutlet weak var contactsTable: UITableView! @IBOutlet weak var contactsTable: UITableView!
var contactStruct = [ContactStruct]() var contactStructArray = [ContactStruct]()
var contacts = [CNContact]() var contactStruct : ContactStruct?
//var contactsBackup = [CNContact]()
let contactStore = CNContactStore() let contactStore = CNContactStore()
override func viewDidLoad() { override func viewDidLoad() {
...@@ -29,19 +28,36 @@ class ContactsViewController: UIViewController { ...@@ -29,19 +28,36 @@ class ContactsViewController: UIViewController {
let request = CNContactFetchRequest( keysToFetch: keys as! [CNKeyDescriptor]) let request = CNContactFetchRequest( keysToFetch: keys as! [CNKeyDescriptor])
request.sortOrder = CNContactSortOrder.givenName request.sortOrder = CNContactSortOrder.givenName
do { do {
var auxLetter = ""
var contacts = [CNContact]()
var isNotTheFirst = false
try self.contactStore.enumerateContacts(with: request) { try self.contactStore.enumerateContacts(with: request) {
(contact, stop) in (contact, stop) in
// Array containing all unified contacts from everywhere if contact.givenName.prefix(1) == auxLetter {
self.contacts.append(contact) contacts.append(contact)
} else {
if isNotTheFirst {
let contactsExpandables = ContactStruct(isExpanded: true, contacts: contacts, letter: auxLetter)
self.contactStructArray.append(contactsExpandables) //guardamos el contacto expandible anterior
auxLetter = String(contact.givenName.prefix(1)) //guardamos la nueva letra
contacts = [CNContact]() //inicializamos nuevamente el array de contactos
contacts.append(contact) //guardamos el nuevo contacto
} else {
contacts.append(contact)
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
}
} }
let contactsExpandables = ContactStruct(isExpanded: true, contacts: contacts) }
self.contactStruct = [contactsExpandables] self.contactStructArray.append(ContactStruct(isExpanded: true, contacts: contacts, letter: auxLetter)) //agregamos el ultimo nombre
print("CONTACTOS ORDENADOS")
print(self.contactStructArray)
print("======================")
} }
catch { catch {
print("unable to fetch contacts") print("unable to fetch contacts")
} }
print(contacts)
} }
override func prepare(for segue: UIStoryboardSegue, sender: Any?) { override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
...@@ -53,17 +69,22 @@ class ContactsViewController: UIViewController { ...@@ -53,17 +69,22 @@ class ContactsViewController: UIViewController {
} }
} }
extension ContactsViewController: UITableViewDelegate, UITableViewDataSource { extension ContactsViewController: UITableViewDelegate, UITableViewDataSource {
func numberOfSections(in tableView: UITableView) -> Int {
return contactStructArray.count
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if !contactStruct[section].isExpanded { if !contactStructArray[section].isExpanded {
return 0 return 0
} }
return contactStruct[section].contacts.count return contactStructArray[section].contacts.count
} }
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "contactName")! let cell = tableView.dequeueReusableCell(withIdentifier: "contactName")!
let contactCell = contactStruct[indexPath.section].contacts[indexPath.row] let contactCell = contactStructArray[indexPath.section].contacts[indexPath.row]
cell.textLabel?.text = "\(contactCell.givenName) \(contactCell.familyName)" cell.textLabel?.text = "\(contactCell.givenName) \(contactCell.familyName)"
...@@ -71,7 +92,7 @@ extension ContactsViewController: UITableViewDelegate, UITableViewDataSource { ...@@ -71,7 +92,7 @@ extension ContactsViewController: UITableViewDelegate, UITableViewDataSource {
} }
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let selectedContact = contacts[indexPath.row] let selectedContact = contactStructArray[indexPath.section].contacts[indexPath.row]
performSegue(withIdentifier: "gotocontact", sender: selectedContact) performSegue(withIdentifier: "gotocontact", sender: selectedContact)
} }
...@@ -85,23 +106,23 @@ extension ContactsViewController: UITableViewDelegate, UITableViewDataSource { ...@@ -85,23 +106,23 @@ extension ContactsViewController: UITableViewDelegate, UITableViewDataSource {
button.setTitleColor(.black, for: .normal) button.setTitleColor(.black, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15) button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
button.backgroundColor = .systemGray4 button.backgroundColor = .systemGray4
button.addTarget(self, action: #selector(closeSection), for: .touchUpInside) button.addTarget(self, action: #selector(interactSection), for: .touchUpInside)
button.tag = section button.tag = section
return button//ʌΛ return button//ʌΛ
} }
@objc func closeSection(button : UIButton) { @objc func interactSection(button : UIButton) {
//contactsBackup = contacts //contactsBackup = contacts
let section = button.tag let section = button.tag
var indexPaths = [IndexPath]() var indexPaths = [IndexPath]()
for row in contactStruct[section].contacts.indices { for row in contactStructArray[section].contacts.indices {
let indexPath = IndexPath(row: row, section: section) let indexPath = IndexPath(row: row, section: section)
indexPaths.append(indexPath) indexPaths.append(indexPath)
} }
let isExpanded = contactStruct[section].isExpanded let isExpanded = contactStructArray[section].isExpanded
contactStruct[section].isExpanded = !isExpanded contactStructArray[section].isExpanded = !isExpanded
button.setTitle(isExpanded ? "V" : "Λ", for: .normal) button.setTitle(isExpanded ? "V" : "Λ", for: .normal)
......
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