-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathListInterfaceController.swift
226 lines (170 loc) · 8.19 KB
/
ListInterfaceController.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
The `ListInterfaceController` interface controller that presents a single list managed by a `ListPresenterType` object.
*/
import WatchKit
import ListerKit
/**
The interface controller that presents a list. The interface controller listens for changes to how the list
should be presented by the list presenter.
*/
class ListInterfaceController: WKInterfaceController, ListPresenterDelegate {
// MARK: Types
struct Storyboard {
static let interfaceControllerName = "ListInterfaceController"
struct RowTypes {
static let item = "ListControllerItemRowType"
static let noItems = "ListControllerNoItemsRowType"
}
}
// MARK: Properties
@IBOutlet weak var interfaceTable: WKInterfaceTable!
var listDocument: ListDocument!
var listPresenter: IncompleteListItemsPresenter? {
return listDocument?.listPresenter as? IncompleteListItemsPresenter
}
// MARK: Interface Table Selection
override func table(table: WKInterfaceTable, didSelectRowAtIndex rowIndex: Int) {
guard let listPresenter = listPresenter else { return }
let listItem = listPresenter.presentedListItems[rowIndex]
listPresenter.toggleListItem(listItem)
}
// MARK: Actions
@IBAction func markAllListItemsAsComplete() {
guard let listPresenter = listPresenter else { return }
listPresenter.updatePresentedListItemsToCompletionState(true)
}
@IBAction func markAllListItemsAsIncomplete() {
guard let listPresenter = listPresenter else { return }
listPresenter.updatePresentedListItemsToCompletionState(false)
}
func refreshAllData() {
guard let listPresenter = listPresenter else { return }
let listItemCount = listPresenter.count
if listItemCount > 0 {
interfaceTable.setNumberOfRows(listItemCount, withRowType: Storyboard.RowTypes.item)
for idx in 0..<listItemCount {
configureRowControllerAtIndex(idx)
}
}
else {
let indexSet = NSIndexSet(index: 0)
interfaceTable.insertRowsAtIndexes(indexSet, withRowType: Storyboard.RowTypes.noItems)
}
}
// MARK: ListPresenterDelegate
func listPresenterDidRefreshCompleteLayout(_: ListPresenterType) {
refreshAllData()
}
func listPresenterWillChangeListLayout(_: ListPresenterType, isInitialLayout: Bool) {
// `WKInterfaceTable` objects do not need to be notified of changes to the table, so this is a no op.
}
func listPresenter(_: ListPresenterType, didInsertListItem listItem: ListItem, atIndex index: Int) {
let indexSet = NSIndexSet(index: index)
// The list presenter was previously empty. Remove the "no items" row.
if index == 0 && listPresenter!.count == 1 {
interfaceTable.removeRowsAtIndexes(indexSet)
}
interfaceTable.insertRowsAtIndexes(indexSet, withRowType: Storyboard.RowTypes.item)
}
func listPresenter(_: ListPresenterType, didRemoveListItem listItem: ListItem, atIndex index: Int) {
let indexSet = NSIndexSet(index: index)
interfaceTable.removeRowsAtIndexes(indexSet)
// The list presenter is now empty. Add the "no items" row.
if index == 0 && listPresenter!.isEmpty {
interfaceTable.insertRowsAtIndexes(indexSet, withRowType: Storyboard.RowTypes.noItems)
}
}
func listPresenter(_: ListPresenterType, didUpdateListItem listItem: ListItem, atIndex index: Int) {
configureRowControllerAtIndex(index)
}
func listPresenter(_: ListPresenterType, didMoveListItem listItem: ListItem, fromIndex: Int, toIndex: Int) {
// Remove the item from the fromIndex straight away.
let fromIndexSet = NSIndexSet(index: fromIndex)
interfaceTable.removeRowsAtIndexes(fromIndexSet)
/*
Determine where to insert the moved item. If the `toIndex` was beyond the `fromIndex`, normalize
its value.
*/
var toIndexSet: NSIndexSet
if toIndex > fromIndex {
toIndexSet = NSIndexSet(index: toIndex - 1)
}
else {
toIndexSet = NSIndexSet(index: toIndex)
}
interfaceTable.insertRowsAtIndexes(toIndexSet, withRowType: Storyboard.RowTypes.item)
}
func listPresenter(_: ListPresenterType, didUpdateListColorWithColor color: List.Color) {
guard let listPresenter = listPresenter else { return }
for idx in 0..<listPresenter.count {
configureRowControllerAtIndex(idx)
}
}
func listPresenterDidChangeListLayout(_: ListPresenterType, isInitialLayout: Bool) {
if isInitialLayout {
// Display all of the list items on the first layout.
refreshAllData()
}
else {
/*
The underlying document changed because of user interaction (this event only occurs if the
list presenter's underlying list presentation changes based on user interaction).
*/
listDocument.updateChangeCount(.Done)
}
}
// MARK: Convenience
func setupInterfaceTable() {
listDocument.listPresenter = IncompleteListItemsPresenter()
listPresenter!.delegate = self
listDocument.openWithCompletionHandler { success in
if !success {
print("Couldn't open document: \(self.listDocument?.fileURL).")
return
}
/*
Once the document for the list has been found and opened, update the user activity with its URL path
to enable the container iOS app to start directly in this list document. A URL path
is passed instead of a URL because the `userInfo` dictionary of a WatchKit app's user activity
does not allow NSURL values.
*/
let userInfo: [NSObject: AnyObject] = [
AppConfiguration.UserActivity.listURLPathUserInfoKey: self.listDocument.fileURL.path!,
AppConfiguration.UserActivity.listColorUserInfoKey: self.listDocument.listPresenter!.color.rawValue
]
/*
Lister uses a specific user activity name registered in the Info.plist and defined as a constant to
separate this action from the built-in UIDocument handoff support.
*/
self.updateUserActivity(AppConfiguration.UserActivity.watch, userInfo: userInfo, webpageURL: nil)
}
}
func configureRowControllerAtIndex(index: Int) {
guard let listPresenter = listPresenter else { return }
let listItemRowController = interfaceTable.rowControllerAtIndex(index) as! ListItemRowController
let listItem = listPresenter.presentedListItems[index]
listItemRowController.setText(listItem.text)
let textColor = listItem.isComplete ? UIColor.grayColor() : UIColor.whiteColor()
listItemRowController.setTextColor(textColor)
// Update the checkbox image.
let state = listItem.isComplete ? "checked" : "unchecked"
let imageName = "checkbox-\(listPresenter.color.name.lowercaseString)-\(state)"
listItemRowController.setCheckBoxImageNamed(imageName)
}
// MARK: Interface Life Cycle
override func awakeWithContext(context: AnyObject?) {
precondition(context is ListInfo, "Expected class of `context` to be ListInfo.")
let listInfo = context as! ListInfo
listDocument = ListDocument(fileURL: listInfo.URL)
// Set the title of the interface controller based on the list's name.
setTitle(listInfo.name)
// Fill the interface table with the current list items.
setupInterfaceTable()
}
override func didDeactivate() {
listDocument.closeWithCompletionHandler(nil)
}
}