123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- //
- // ContactsViewController.swift
- // app
- //
- // Created by Luis Quiñones on 11/7/19.
- // Copyright © 2019 Luis Quiñones . All rights reserved.
- //
-
- import UIKit
- import ContactsUI
-
- class ContactsViewController: UIViewController, CNContactPickerDelegate, UITableViewDataSource, UITableViewDelegate {
-
- let transition = SlideInTransition()
- var topView: UIView?
- let defaults = UserDefaults.standard
- var emergency_contacts: [String:String]?
- var keys: Array<String>?
-
- @IBOutlet weak var tableView: UITableView!
-
- override func viewDidLoad() {
- super.viewDidLoad()
- // Do any additional setup after loading the view.
- emergency_contacts = defaults.object(forKey: "contacts") as? [String: String] ?? [String: String]()
- keys = Array(emergency_contacts!.keys)
- self.tableView.register(UINib(nibName: "ContactsTableViewCell", bundle: nil), forCellReuseIdentifier: "ContactsTableViewCell")
- }
-
- override func viewWillAppear(_ animated: Bool) {
- super.viewWillAppear(animated)
- self.navigationItem.hidesBackButton = true
- tableView.reloadData()
- }
-
- @IBAction func didTapMenu(_ sender: UIBarButtonItem) {
- guard let menuViewController = storyboard?.instantiateViewController(withIdentifier: "MenuViewController") as? MenuViewController else { return }
-
- menuViewController.modalPresentationStyle = .overCurrentContext
- menuViewController.transitioningDelegate = self
- present(menuViewController, animated: true)
- }
-
- // MARK: - Contacts
-
- @IBAction func addContactsTapped(_ sender: Any) {
-
- let picker = CNContactPickerViewController()
- picker.delegate = self
- present(picker,animated: true, completion: nil)
-
- }
-
- func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
-
- if !defaults.contains(key: "contacts") {
- emergency_contacts = [:]
- defaults.set(emergency_contacts, forKey: "contacts")
- print("Set empty contact list")
- }
- contacts.forEach { (contact) in
- for phone_number in contact.phoneNumbers {
- let contact_num = phone_number.value.stringValue
- // var contacts_dict = defaults.object(forKey: "contacts") as? [String: String] ?? [String: String]()
-
- if emergency_contacts!.count >= 10 {
- print("Contact list limited to 10.")
- }
-
- else if emergency_contacts?[contact_num] == nil {
- // Add contact
- emergency_contacts?[contact_num] = contact.givenName
- keys = Array(emergency_contacts!.keys)
- defaults.set(emergency_contacts, forKey: "contacts")
- print("contact added")
- // Refresh table
- tableView.reloadData()
- }
- else {
- print("Phone number already added.")
- }
- }
- }
- }
-
- func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
- self.dismiss(animated: true, completion: nil)
- }
-
- // MARK: - TableView
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
- return keys!.count
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
- let cell = tableView.dequeueReusableCell(withIdentifier: "ContactsTableViewCell", for: indexPath) as! ContactsTableViewCell
- let phone = keys?[indexPath.row]
- let name = emergency_contacts?[phone!]
- // print("name: \(name)")
- // print("phone: \(phone)")
- cell.contactName?.text = name
- cell.contactPhone?.text = phone
- return cell
- }
-
- func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
- return true
- }
-
- func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
- if editingStyle == .delete {
- let cell = tableView.cellForRow(at: indexPath) as! ContactsTableViewCell
-
- let phone = cell.contactPhone?.text
- keys?.remove(at: indexPath.row)
- // print(phone)
- emergency_contacts?[phone!] = nil
- defaults.set(emergency_contacts, forKey: "contacts")
- tableView.beginUpdates()
- tableView.deleteRows(at: [indexPath], with: .fade)
- tableView.endUpdates()
- // print(keys)
- // print(emergency_contacts)
- }
- }
-
- } // End VC
-
- // MARK: - Extensions
-
- extension ContactsViewController: UIViewControllerTransitioningDelegate {
- func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
- transition.isPresenting = true
- return transition
- }
- func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
- transition.isPresenting = false
- return transition
- }
- }
-
- extension UserDefaults {
- func object<T: Codable>(_ type: T.Type, with key: String, usingDecoder decoder: JSONDecoder = JSONDecoder()) -> T? {
- guard let data = self.value(forKey: key) as? Data else { return nil }
- return try? decoder.decode(type.self, from: data)
- }
-
- func set<T: Codable>(object: T, forKey key: String, usingEncoder encoder: JSONEncoder = JSONEncoder()) {
- let data = try? encoder.encode(object)
- self.set(data, forKey: key)
- }
-
- func contains(key: String) -> Bool {
- return UserDefaults.standard.object(forKey: key) != nil
- }
- }
|