-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathContactListViewController.swift
More file actions
126 lines (95 loc) · 3.98 KB
/
ContactListViewController.swift
File metadata and controls
126 lines (95 loc) · 3.98 KB
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
//
// MasterViewController.swift
// Lifetime
//
// Created by Nils Fischer on 29.04.16.
// Copyright © 2016 iOS Dev Kurs Universität Heidelberg. All rights reserved.
//
import UIKit
import Contacts
class ContactListViewController: UITableViewController {
/// A reference to the device's contacts
var contactStore: CNContactStore? {
didSet {
loadContacts()
}
}
// MARK: Contact Loading
private var contacts: [CNContact] = [] {
didSet {
tableView.reloadData()
}
}
private let requiredContactKeysToFetch = [ CNContactFormatter.descriptorForRequiredKeysForStyle(.FullName) ] + CNContact.requiredKeysForLifetime
private func loadContacts(filteredBy searchTerm: String? = nil) {
guard let contactStore = contactStore else {
contacts = []
return
}
if let searchTerm = searchTerm where !searchTerm.isEmpty {
contacts = (try? contactStore.unifiedContactsMatchingPredicate(CNContact.predicateForContactsMatchingName(searchTerm), keysToFetch: self.requiredContactKeysToFetch)) ?? []
} else {
let containers = (try? contactStore.containersMatchingPredicate(nil)) ?? []
contacts = containers.map({ CNContact.predicateForContactsInContainerWithIdentifier($0.identifier) }).flatMap({ (try? contactStore.unifiedContactsMatchingPredicate($0, keysToFetch: self.requiredContactKeysToFetch)) ?? [] })
}
}
// MARK: Search Controller
private lazy var searchController: UISearchController = {
let searchController = UISearchController(searchResultsController: nil)
searchController.dimsBackgroundDuringPresentation = false
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchResultsUpdater = self
self.definesPresentationContext = true
return searchController
}()
// MARK: Lifecycle
override func viewDidLoad() {
super.viewDidLoad()
tableView.tableHeaderView = searchController.searchBar
}
// MARK: User Interaction
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
switch segue.identifier! {
case "showContactDetail":
guard let indexPath = self.tableView.indexPathForSelectedRow else {break}
let contact = contacts[indexPath.row]
let contactDetailViewController = segue.destinationViewController as! ContactDetailViewController
contactDetailViewController.contact = contact
default:
break
}
}
}
// MARK: - Table View Datasource
// TODO: implement UITableViewDatasource protocol
extension ContactListViewController {
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contacts.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("LifetimeCell", forIndexPath: indexPath) as! LifetimeCell
let contact = contacts[indexPath.row]
cell.configureForContact(contact)
if contact.lifetime != nil {
cell.selectionStyle = .Default
cell.accessoryType = .DisclosureIndicator
} else {
cell.selectionStyle = .None
cell.accessoryType = .None
}
return cell
}
}
// MARK: - Search Results Updating
extension ContactListViewController: UISearchResultsUpdating {
func updateSearchResultsForSearchController(searchController: UISearchController) {
if let searchTerm = searchController.searchBar.text where !searchTerm.isEmpty {
loadContacts(filteredBy: searchTerm)
} else {
loadContacts(filteredBy: nil)
}
}
}