Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

ContactsViewController.swift 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //
  2. // ContactsViewController.swift
  3. // app
  4. //
  5. // Created by Luis Quiñones on 11/7/19.
  6. // Copyright © 2019 Luis Quiñones . All rights reserved.
  7. //
  8. import UIKit
  9. import ContactsUI
  10. class ContactsViewController: UIViewController, CNContactPickerDelegate, UITableViewDataSource, UITableViewDelegate {
  11. let transition = SlideInTransition()
  12. var topView: UIView?
  13. let defaults = UserDefaults.standard
  14. var emergency_contacts: [String:String]?
  15. var keys: Array<String>?
  16. @IBOutlet weak var tableView: UITableView!
  17. override func viewDidLoad() {
  18. super.viewDidLoad()
  19. // Do any additional setup after loading the view.
  20. emergency_contacts = defaults.object(forKey: "contacts") as? [String: String] ?? [String: String]()
  21. keys = Array(emergency_contacts!.keys)
  22. self.tableView.register(UINib(nibName: "ContactsTableViewCell", bundle: nil), forCellReuseIdentifier: "ContactsTableViewCell")
  23. }
  24. override func viewWillAppear(_ animated: Bool) {
  25. super.viewWillAppear(animated)
  26. self.navigationItem.hidesBackButton = true
  27. tableView.reloadData()
  28. }
  29. @IBAction func didTapMenu(_ sender: UIBarButtonItem) {
  30. guard let menuViewController = storyboard?.instantiateViewController(withIdentifier: "MenuViewController") as? MenuViewController else { return }
  31. menuViewController.modalPresentationStyle = .overCurrentContext
  32. menuViewController.transitioningDelegate = self
  33. present(menuViewController, animated: true)
  34. }
  35. // MARK: - Contacts
  36. @IBAction func addContactsTapped(_ sender: Any) {
  37. let picker = CNContactPickerViewController()
  38. picker.delegate = self
  39. present(picker,animated: true, completion: nil)
  40. }
  41. func contactPicker(_ picker: CNContactPickerViewController, didSelect contacts: [CNContact]) {
  42. if !defaults.contains(key: "contacts") {
  43. emergency_contacts = [:]
  44. defaults.set(emergency_contacts, forKey: "contacts")
  45. print("Set empty contact list")
  46. }
  47. contacts.forEach { (contact) in
  48. for phone_number in contact.phoneNumbers {
  49. let contact_num = phone_number.value.stringValue
  50. // var contacts_dict = defaults.object(forKey: "contacts") as? [String: String] ?? [String: String]()
  51. if emergency_contacts!.count >= 10 {
  52. print("Contact list limited to 10.")
  53. }
  54. else if emergency_contacts?[contact_num] == nil {
  55. // Add contact
  56. emergency_contacts?[contact_num] = contact.givenName
  57. keys = Array(emergency_contacts!.keys)
  58. defaults.set(emergency_contacts, forKey: "contacts")
  59. print("contact added")
  60. // Refresh table
  61. tableView.reloadData()
  62. }
  63. else {
  64. print("Phone number already added.")
  65. }
  66. }
  67. }
  68. }
  69. func contactPickerDidCancel(_ picker: CNContactPickerViewController) {
  70. self.dismiss(animated: true, completion: nil)
  71. }
  72. // MARK: - TableView
  73. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  74. return keys!.count
  75. }
  76. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  77. let cell = tableView.dequeueReusableCell(withIdentifier: "ContactsTableViewCell", for: indexPath) as! ContactsTableViewCell
  78. let phone = keys?[indexPath.row]
  79. let name = emergency_contacts?[phone!]
  80. // print("name: \(name)")
  81. // print("phone: \(phone)")
  82. cell.contactName?.text = name
  83. cell.contactPhone?.text = phone
  84. return cell
  85. }
  86. func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
  87. return true
  88. }
  89. func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
  90. if editingStyle == .delete {
  91. let cell = tableView.cellForRow(at: indexPath) as! ContactsTableViewCell
  92. let phone = cell.contactPhone?.text
  93. keys?.remove(at: indexPath.row)
  94. // print(phone)
  95. emergency_contacts?[phone!] = nil
  96. defaults.set(emergency_contacts, forKey: "contacts")
  97. tableView.beginUpdates()
  98. tableView.deleteRows(at: [indexPath], with: .fade)
  99. tableView.endUpdates()
  100. // print(keys)
  101. // print(emergency_contacts)
  102. }
  103. }
  104. } // End VC
  105. // MARK: - Extensions
  106. extension ContactsViewController: UIViewControllerTransitioningDelegate {
  107. func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  108. transition.isPresenting = true
  109. return transition
  110. }
  111. func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
  112. transition.isPresenting = false
  113. return transition
  114. }
  115. }
  116. extension UserDefaults {
  117. func object<T: Codable>(_ type: T.Type, with key: String, usingDecoder decoder: JSONDecoder = JSONDecoder()) -> T? {
  118. guard let data = self.value(forKey: key) as? Data else { return nil }
  119. return try? decoder.decode(type.self, from: data)
  120. }
  121. func set<T: Codable>(object: T, forKey key: String, usingEncoder encoder: JSONEncoder = JSONEncoder()) {
  122. let data = try? encoder.encode(object)
  123. self.set(data, forKey: key)
  124. }
  125. func contains(key: String) -> Bool {
  126. return UserDefaults.standard.object(forKey: key) != nil
  127. }
  128. }