-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathActions.swift
301 lines (242 loc) · 9.42 KB
/
Actions.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
Copyright (c) 2011-present, NimbusKit. All rights reserved.
This source code is licensed under the BSD-style license found at http://nimbuskit.info/license
*/
import Foundation
public typealias Action = (object: AnyObject, indexPath: NSIndexPath) -> Bool
public typealias BoolTargetSignature = (object: NSObject, indexPath: NSIndexPath) -> Bool
public typealias VoidTargetSignature = (object: NSObject, indexPath: NSIndexPath) -> ()
protocol TargetAction {
func performAction(object: NSObject, indexPath: NSIndexPath) -> Bool?
}
private struct BoolObjectAction <T: AnyObject> : TargetAction {
weak var target: T?
let action: (T) -> BoolTargetSignature
func performAction(object: NSObject, indexPath: NSIndexPath) -> Bool? {
if let t = target {
return action(t)(object: object, indexPath: indexPath)
}
return nil
}
}
private struct VoidObjectAction <T: AnyObject> : TargetAction {
weak var target: T?
let action: (T) -> VoidTargetSignature
func performAction(object: NSObject, indexPath: NSIndexPath) -> Bool? {
if let t = target {
action(t)(object: object, indexPath: indexPath)
}
return nil
}
}
struct ObjectActions {
var tap: Action?
var navigate: Action?
var detail: Action?
var tapSelector: TargetAction?
var navigateSelector: TargetAction?
var detailSelector: TargetAction?
var enabled: Bool = true
init() {}
func hasActions() -> Bool {
return (hasTapAction() || hasNavigateAction() || hasDetailAction())
}
func hasTapAction() -> Bool {
return (tap != nil || tapSelector != nil)
}
func hasNavigateAction() -> Bool {
return (navigate != nil || navigateSelector != nil)
}
func hasDetailAction() -> Bool {
return (detail != nil || detailSelector != nil)
}
func performTapAction(object: NSObject, indexPath: NSIndexPath) -> Bool? {
if !hasTapAction() {
return nil
}
var shouldDeselect = false
if let tap = tap {
shouldDeselect |= tap(object: object, indexPath: indexPath)
}
if let tapSelector = tapSelector {
shouldDeselect |= tapSelector.performAction(object, indexPath: indexPath)!
}
return shouldDeselect
}
func performNavigateAction(object: NSObject, indexPath: NSIndexPath) {
navigate?(object: object, indexPath: indexPath)
navigateSelector?.performAction(object, indexPath: indexPath)
}
func performDetailAction(object: NSObject, indexPath: NSIndexPath) {
detail?(object: object, indexPath: indexPath)
detailSelector?.performAction(object, indexPath: indexPath)
}
mutating func unionWith(otherActions: ObjectActions?) {
if nil == otherActions {
return
}
if (tap == nil && otherActions!.tap != nil) {
tap = otherActions!.tap
}
if (navigate == nil && otherActions!.navigate != nil) {
navigate = otherActions!.navigate
}
if (detail == nil && otherActions!.detail != nil) {
detail = otherActions!.detail
}
if (tapSelector == nil && otherActions!.tapSelector != nil) {
tapSelector = otherActions!.tapSelector
}
if (navigateSelector == nil && otherActions!.navigateSelector != nil) {
navigateSelector = otherActions!.navigateSelector
}
if (detailSelector == nil && otherActions!.detailSelector != nil) {
detailSelector = otherActions!.detailSelector
}
}
mutating func reset() {
self.tap = nil
self.navigate = nil
self.detail = nil
self.tapSelector = nil
self.navigateSelector = nil
self.detailSelector = nil
}
}
public class Actions : NSObject {
var objectToAction = Dictionary<Int, ObjectActions>()
var classToAction = Dictionary<String, ObjectActions>()
}
extension Actions {
// Querying Actionable State
public func isActionableObject<O where O: AnyObject, O: Hashable>(object: O) -> Bool {
return self.actionsForObject(object).hasActions()
}
// Enabling/Disabling Actions
public func setObject<O where O: AnyObject, O: Hashable>(object: O, enabled: Bool) {
self.ensureActionsExistForObject(object)
self.objectToAction[object.hashValue]!.enabled = enabled
}
public func setClass(theClass: AnyClass, enabled: Bool) {
let theClassName = NSStringFromClass(theClass)
self.ensureActionsExistForClass(theClassName)
self.classToAction[theClassName]!.enabled = enabled
}
// Object Mapping
public func attachToObject<O where O: AnyObject, O: Hashable>(object: O, tap: Action) -> O {
self.ensureActionsExistForObject(object)
self.objectToAction[object.hashValue]!.tap = tap
return object
}
public func attachToObject<O where O: AnyObject, O: Hashable>(object: O, navigate: Action) -> O {
self.ensureActionsExistForObject(object)
self.objectToAction[object.hashValue]!.navigate = navigate
return object
}
public func attachToObject<O where O: AnyObject, O: Hashable>(object: O, detail: Action) -> O {
self.ensureActionsExistForObject(object)
self.objectToAction[object.hashValue]!.detail = detail
return object
}
public func attachToObject<O, T where O: AnyObject, O: Hashable, T: AnyObject>(object: O, target: T, tap: (T) -> BoolTargetSignature) -> O {
self.ensureActionsExistForObject(object)
self.objectToAction[object.hashValue]!.tapSelector = BoolObjectAction(target: target, action: tap)
return object
}
public func attachToObject<O, T where O: AnyObject, O: Hashable, T: AnyObject>(object: O, target: T, navigate: (T) -> VoidTargetSignature) -> O {
self.ensureActionsExistForObject(object)
self.objectToAction[object.hashValue]!.navigateSelector = VoidObjectAction(target: target, action: navigate)
return object
}
public func attachToObject<O, T where O: AnyObject, O: Hashable, T: AnyObject>(object: O, target: T, detail: (T) -> VoidTargetSignature) -> O {
self.ensureActionsExistForObject(object)
self.objectToAction[object.hashValue]!.detailSelector = VoidObjectAction(target: target, action: detail)
return object
}
// Class Mapping
public func attachToClass(theClass: AnyClass, tap: Action) -> AnyClass {
let className = NSStringFromClass(theClass)
self.ensureActionsExistForClass(className)
self.classToAction[className]!.tap = tap
return theClass
}
public func attachToClass(theClass: AnyClass, navigate: Action) -> AnyClass {
let className = NSStringFromClass(theClass)
self.ensureActionsExistForClass(className)
self.classToAction[className]!.navigate = navigate
return theClass
}
public func attachToClass(theClass: AnyClass, detail: Action) -> AnyClass {
let className = NSStringFromClass(theClass)
self.ensureActionsExistForClass(className)
self.classToAction[className]!.detail = detail
return theClass
}
public func attachToClass<T: AnyObject>(theClass: AnyClass, target: T, tap: (T) -> BoolTargetSignature) -> AnyClass {
let className = NSStringFromClass(theClass)
self.ensureActionsExistForClass(className)
self.classToAction[className]!.tapSelector = BoolObjectAction(target: target, action: tap)
return theClass
}
public func attachToClass<T: AnyObject>(theClass: AnyClass, target: T, navigate: (T) -> VoidTargetSignature) -> AnyClass {
let className = NSStringFromClass(theClass)
self.ensureActionsExistForClass(className)
self.classToAction[className]!.navigateSelector = VoidObjectAction(target: target, action: navigate)
return theClass
}
public func attachToClass<T: AnyObject>(theClass: AnyClass, target: T, detail: (T) -> VoidTargetSignature) -> AnyClass {
let className = NSStringFromClass(theClass)
self.ensureActionsExistForClass(className)
self.classToAction[className]!.detailSelector = VoidObjectAction(target: target, action: detail)
return theClass
}
// Removing Actions
public func removeAllActionsForObject<O where O: AnyObject, O: Hashable>(object: O) {
self.objectToAction[object.hashValue]?.reset()
}
public func removeAllActionsForClass(theClass: AnyClass) {
self.classToAction[NSStringFromClass(theClass)]?.reset()
}
}
// Internal
extension Actions {
/**
Returns all attached actions for a given object.
"Attached actions" are:
1) actions attached to the provided object, and
2) actions attached to classes in the object's class ancestry.
Priority is as follows:
1) Object actions
2) Object.class actions
3) Object.superclass.class actions
4) etc... up the class ancestry
## Example
Consider the following class hierarchy:
NSObject -> Widget (tap) -> DetailWidget (detail)
The actions for an instance of DetailWidget are Widget's tap and DetailWidget's detail actions.
Attaching a tap action to the DetailWidget instance would override the Widget tap action.
*/
func actionsForObject<O where O: AnyObject, O: Hashable>(object: O) -> ObjectActions {
var actions = ObjectActions()
actions.unionWith(self.objectToAction[object.hashValue])
var objectClass: AnyClass! = object.dynamicType
while objectClass != nil {
actions.unionWith(self.classToAction[NSStringFromClass(objectClass)])
objectClass = objectClass.superclass()
}
return actions
}
}
// Private
extension Actions {
private func ensureActionsExistForObject<O where O: AnyObject, O: Hashable>(object: O) {
if self.objectToAction[object.hashValue] == nil {
self.objectToAction[object.hashValue] = ObjectActions()
}
}
private func ensureActionsExistForClass(theClass: String) {
if self.classToAction[theClass] == nil {
self.classToAction[theClass] = ObjectActions()
}
}
}