-
Notifications
You must be signed in to change notification settings - Fork 872
/
Copy pathListsInterfaceController.swift
147 lines (105 loc) · 5.22 KB
/
ListsInterfaceController.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
/*
Copyright (C) 2016 Apple Inc. All Rights Reserved.
See LICENSE.txt for this sample’s licensing information
Abstract:
The `ListInterfaceController` that presents a single list managed by a `ListPresenterType` instance.
*/
import WatchKit
import ListerKit
class ListsInterfaceController: WKInterfaceController, ListsControllerDelegate {
// MARK: Types
struct Storyboard {
struct RowTypes {
static let list = "ListsInterfaceControllerListRowType"
static let noLists = "ListsInterfaceControllerNoListsRowType"
}
struct Segues {
static let listSelection = "ListsInterfaceControllerListSelectionSegue"
}
}
// MARK: Properties
@IBOutlet weak var interfaceTable: WKInterfaceTable!
var listsController: ListsController!
// MARK: Initializers
override init() {
super.init()
listsController = AppConfiguration.sharedConfiguration.listsControllerForCurrentConfigurationWithPathExtension(AppConfiguration.listerFileExtension)
let noListsIndexSet = NSIndexSet(index: 0)
interfaceTable.insertRowsAtIndexes(noListsIndexSet, withRowType: Storyboard.RowTypes.noLists)
if AppConfiguration.sharedConfiguration.isFirstLaunch {
print("Lister does not currently support configuring a storage option before the iOS app is launched. Please launch the iOS app first. See the Release Notes section in README.md for more information.")
}
}
// MARK: ListsControllerDelegate
func listsController(listsController: ListsController, didInsertListInfo listInfo: ListInfo, atIndex index: Int) {
let indexSet = NSIndexSet(index: index)
// The lists controller was previously empty. Remove the "no lists" row.
if index == 0 && listsController.count == 1 {
interfaceTable.removeRowsAtIndexes(indexSet)
}
interfaceTable.insertRowsAtIndexes(indexSet, withRowType: Storyboard.RowTypes.list)
configureRowControllerAtIndex(index)
}
func listsController(listsController: ListsController, didRemoveListInfo listInfo: ListInfo, atIndex index: Int) {
let indexSet = NSIndexSet(index: index)
// The lists controller is now empty. Add the "no lists" row.
if index == 0 && listsController.count == 0 {
interfaceTable.insertRowsAtIndexes(indexSet, withRowType: Storyboard.RowTypes.noLists)
}
interfaceTable.removeRowsAtIndexes(indexSet)
}
func listsController(listsController: ListsController, didUpdateListInfo listInfo: ListInfo, atIndex index: Int) {
configureRowControllerAtIndex(index)
}
// MARK: Segues
override func contextForSegueWithIdentifier(segueIdentifier: String, inTable table: WKInterfaceTable, rowIndex: Int) -> AnyObject? {
if segueIdentifier == Storyboard.Segues.listSelection {
let listInfo = listsController[rowIndex]
return listInfo
}
return nil
}
// MARK: Convenience
func configureRowControllerAtIndex(index: Int) {
let listRowController = interfaceTable.rowControllerAtIndex(index) as! ColoredTextRowController
let listInfo = listsController[index]
listRowController.setText(listInfo.name)
listInfo.fetchInfoWithCompletionHandler() {
/*
The fetchInfoWithCompletionHandler(_:) method calls its completion handler on a background
queue, dispatch back to the main queue to make UI updates.
*/
dispatch_async(dispatch_get_main_queue()) {
let listRowController = self.interfaceTable.rowControllerAtIndex(index) as! ColoredTextRowController
listRowController.setColor(listInfo.color!.colorValue)
}
}
}
// MARK: Interface Life Cycle
override func willActivate() {
// If the `ListsController` is activating, we should invalidate any pending user activities.
invalidateUserActivity()
listsController.delegate = self
listsController.startSearching()
}
override func didDeactivate() {
listsController.stopSearching()
listsController.delegate = nil
}
override func handleUserActivity(userInfo: [NSObject: AnyObject]?) {
/*
The Lister watch app only supports continuing activities where
`AppConfiguration.UserActivity.listURLPathUserInfoKey` is provided.
*/
let listInfoFilePath = userInfo?[AppConfiguration.UserActivity.listURLPathUserInfoKey] as? String
// If no `listInfoFilePath` is found, there is no activity of interest to handle.
if listInfoFilePath == nil {
return
}
let listInfoURL = NSURL(fileURLWithPath: listInfoFilePath!, isDirectory: false)
// Create a `ListInfo` that represents the list at `listInfoURL`.
let listInfo = ListInfo(URL: listInfoURL)
// Present a `ListInterfaceController`.
pushControllerWithName(ListInterfaceController.Storyboard.interfaceControllerName, context: listInfo)
}
}