Commit 6bf83153 by Javier Heisekce

Agrega ordenamiento de contactos por nombre y divide en secciones

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