/
EmergencyContactViewController.swift
executable file
·158 lines (118 loc) · 5.4 KB
/
EmergencyContactViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
//
// EmergencyContactViewController.swift
// Relief
//
// Created by Joseph Duran on 8/8/16.
// Copyright © 2016 Chi-Golden-Bears-2016. All rights reserved.
//
import UIKit
import CoreData
import Contacts
import ContactsUI
class EmergencyContactViewController: UITableViewController, CNContactPickerDelegate, CNContactViewControllerDelegate {
var tableData: [Lifeline] = []
var contactObjects = [CNContact]()
var userLifeLines = [Lifeline]()
var lifelineAvailability = Availability(startHour: 0, endHour: 24)!
override func viewDidLoad() {
super.viewDidLoad()
if loadLifeLines() != nil{
tableData = loadLifeLines()!
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
// MARK: Table functions
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return tableData.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("contactProtoCell") as! EmergencyContactTableViewCell
let contact = tableData[indexPath.row]
cell.displayName.text = contact.fullName()
cell.displayPhone.text = contact.contactPhone
cell.listItem = contact
// Return our new cell for display
if !contact.isAvailableNow() {
cell.contentView.backgroundColor = UIColor(red: 0.0, green: 0.4, blue: 1.0, alpha: 1.0)
} else {
cell.contentView.backgroundColor = UIColor.purpleColor()
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell = tableView.cellForRowAtIndexPath(indexPath)
let url = NSURL(string: "tel://\(tableData[indexPath.row].contactPhone)")
cell!.contentView.backgroundColor = UIColor.greenColor()
UIApplication.sharedApplication().openURL(url!)
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
// MARK: Actions
func retrieveContactsWithStore(store:CNContactStore){
do{
let groups = try store.groupsMatchingPredicate(nil)
let predicate = CNContact.predicateForContactsInGroupWithIdentifier(groups[0].identifier)
let keysToFetch = [CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName), CNContactEmailAddressesKey]
let contacts = try store.unifiedContactsMatchingPredicate(predicate, keysToFetch: keysToFetch)
self.contactObjects = contacts
dispatch_async(dispatch_get_main_queue(), {()->Void in
self.tableView.reloadData();
})
} catch{
print(error)
}
}
func getContacts(){
let store = CNContactStore()
if CNContactStore.authorizationStatusForEntityType(.Contacts) == .NotDetermined{
store.requestAccessForEntityType(.Contacts, completionHandler: { (authorized: Bool, error:NSError?) -> Void in
if authorized{
self.retrieveContactsWithStore(store)
}
})
}else if CNContactStore.authorizationStatusForEntityType(.Contacts) == .Authorized{
self.retrieveContactsWithStore(store)
}
}
// MARK: Actions - contactsUI
@IBAction func openContactPicker(sender: UIBarButtonItem) {
addExistingContact()
}
func addExistingContact() {
let contactPicker = CNContactPickerViewController()
contactPicker.displayedPropertyKeys = [CNContactGivenNameKey, CNContactFamilyNameKey, CNContactPhoneNumbersKey]
contactPicker.delegate = self
self.presentViewController(contactPicker, animated: true, completion: nil)
}
func contactPicker(picker: CNContactPickerViewController, didSelectContactProperty contactProperty: CNContactProperty) {
let contact = contactProperty.contact
let phoneNumber = contactProperty.value!
let digits = phoneNumber.valueForKeyPath("digits")!
let contactGivenName = contact.givenName
let contactFamilyName = contact.familyName
let userChoice = Lifeline(contactFirstName: contactGivenName, contactLastName: contactFamilyName, contactPhone: digits as! String, startTime: startTime, endTime: endTime)
//should this savelifelines func be here?
tableData.append(userChoice!)
saveLifeLines()
tableView.reloadData()
}
// MARK: NSCoding
func saveLifeLines() {
let isSuccessfulSave = NSKeyedArchiver.archiveRootObject(tableData, toFile: Lifeline.ArchiveURL.path!)
if !isSuccessfulSave {
print("Failed to save lifeline")
}
}
func loadLifeLines() -> [Lifeline]? {
return NSKeyedUnarchiver.unarchiveObjectWithFile(Lifeline.ArchiveURL.path!) as? [Lifeline]
}
}