ContactsViewController.swift 3.28 KB
Newer Older
Javier Heisekce committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//
//  ContactsViewController.swift
//  ContactsApp
//
//  Created by User on 3/13/20.
//  Copyright © 2020 jheisecke. All rights reserved.
//

import UIKit
import ContactsUI

class ContactsViewController: UIViewController {
    
    @IBOutlet weak var contactsTable: UITableView!
    
    var contacts = [CNContact]()
17
    var contactsBackup = [CNContact]()
Javier Heisekce committed
18 19 20 21 22 23 24 25 26 27
    let contactStore = CNContactStore()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        importContacts()
        contactsTable.tableFooterView = UIView(frame: .zero)
    }
    
    func importContacts() {
        
28 29
        let keys = [CNContactFormatter.descriptorForRequiredKeys(for: .fullName), CNContactEmailAddressesKey, CNContactPhoneNumbersKey, CNContactImageDataKey, CNContactImageDataAvailableKey] as [Any]
        let request = CNContactFetchRequest( keysToFetch: keys as! [CNKeyDescriptor])
Javier Heisekce committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
        request.sortOrder = CNContactSortOrder.givenName
        do {
            try self.contactStore.enumerateContacts(with: request) {
                (contact, stop) in
                // Array containing all unified contacts from everywhere
                self.contacts.append(contact)
            }
        }
        catch {
            print("unable to fetch contacts")
        }

        print(contacts)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let singleContactVC = segue.destination as? SingleContactViewController {
            if let selectedCnt = sender as? CNContact {
                singleContactVC.selectedContact = selectedCnt
            }
        }
    }
}
extension ContactsViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return contacts.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "contactName")!
        cell.textLabel?.text = "\(contacts[indexPath.row].givenName) \(contacts[indexPath.row].familyName)"
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let selectedContact = contacts[indexPath.row]
        performSegue(withIdentifier: "gotocontact", sender: selectedContact)
    }
    
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    func tableView(_ tableView: UITableView, estimatedHeightForHeaderInSection section: Int) -> CGFloat {
        return 40
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let button = UIButton(type: .system)
        button.setTitle("Λ", for: .normal)
        button.setTitleColor(.black, for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 15)
        button.backgroundColor = .systemGray4
        button.addTarget(self, action: #selector(closeSection), for: .touchUpInside)
        
        return button//ʌΛ
    }
    
    @objc func closeSection() {
        contactsBackup = contacts
        let section = 0
        
        var indexPaths = [IndexPath]()
        for row in contacts.indices {
            let indexPath = IndexPath(row: row, section: section)
            indexPaths.append(indexPath)
        }
        contacts.removeAll()
        contactsTable.reloadData()
    }
Javier Heisekce committed
96
}