/
NavigationDSL.swift
479 lines (421 loc) · 16 KB
/
NavigationDSL.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
//
// NavigationDSL.swift
// Tempura
//
// Copyright © 2021 Bending Spoons.
// Distributed under the MIT License.
// See the LICENSE file for more information.
import UIKit
/// Used by a `RoutableWithConfiguration` inside its `RoutableWithConfiguration.navigationConfiguration`
/// to describe the kind of navigation action (`Show`, `Hide`) to handle.
///
/// ```swift
/// extension ListViewController: RoutableWithConfiguration {
///
/// // needed by the `Routable` protocol
/// // to identify this ViewController in the hierarchy
/// var routeIdentifier: RouteElementIdentifier {
/// return "listScreen"
/// }
///
/// // the `NavigationRequest`s that this ViewController is handling
/// // with the `NavigationInstruction` to execute
/// var navigationConfiguration: [NavigationRequest: NavigationInstruction] {
/// return [
/// .show("addItemScreen"): .presentModally({ [unowned self] _ in
/// let vc = AddItemViewController(store: self.store)
/// return vc
/// })
/// ]
/// }
/// ```
///
public struct NavigationRequest: Hashable {
fileprivate enum NavigationKind: Int {
case show
case hide
}
/// Represents a NavigationRequest to match a `Show` action dispatched.
public static func show<T: RawRepresentable>(_ source: T) -> NavigationRequest where T.RawValue == RouteElementIdentifier {
return NavigationRequest(source: source.rawValue, kind: .show)
}
/// Represents a NavigationRequest to match a `Show` action dispatched.
public static func show(_ source: RouteElementIdentifier) -> NavigationRequest {
return NavigationRequest(source: source, kind: .show)
}
/// Represents a NavigationRequest to match a `Hide` action dispatched.
public static func hide<T: RawRepresentable>(_ source: T) -> NavigationRequest where T.RawValue == RouteElementIdentifier {
return NavigationRequest(source: source.rawValue, kind: .hide)
}
/// Represents a NavigationRequest to match a `Hide` action dispatched.
public static func hide(_ source: RouteElementIdentifier) -> NavigationRequest {
return NavigationRequest(source: source, kind: .hide)
}
private let source: String
private let kind: NavigationKind
private init(source: String, kind: NavigationKind) {
self.source = source
self.kind = kind
}
// Conformance to Hashable
public func hash(into hasher: inout Hasher) {
hasher.combine(self.source)
hasher.combine(self.kind)
}
fileprivate func canHandle(_ identifier: String, kind: NavigationKind) -> Bool {
return self.source == identifier && kind == self.kind
}
/// Implementation of the equality between two NavigationRequest.
public static func == (l: NavigationRequest, r: NavigationRequest) -> Bool {
if l.kind != r.kind {
return false
}
if l.source != r.source {
return false
}
return true
}
}
extension NavigationRequest: CustomDebugStringConvertible {
public var debugDescription: String {
switch self.kind {
case .show:
return "Show(\(self.source))"
case .hide:
return "Hide(\(self.source))"
}
}
}
/// Closure used by a `NavigationInstruction` of type `.custom`.
public typealias CustomNavigationOptionClosure = (
_ identifier: RouteElementIdentifier,
_ from: RouteElementIdentifier,
_ animated: Bool,
_ context: Any?,
_ completion: @escaping RoutingCompletion
) -> Void
/// Closure used by a `NavigationInstruction` of type `.optionalCustom`.
/// The return value determines whether the navigation has been handled or not. If not, the routing continues as if the navigation
/// instruction is not defined
public typealias OptionalCustomNavigationOptionClosure = (
_ identifier: RouteElementIdentifier,
_ from: RouteElementIdentifier,
_ animated: Bool,
_ context: Any?,
_ completion: @escaping RoutingCompletion
) -> Bool
/// Used by a `RoutableWithConfiguration` inside its `RoutableWithConfiguration.navigationConfiguration`
/// to describe the kind of navigation to perform when handling a `NavigationRequest`.
///
/// ```swift
/// extension ListViewController: RoutableWithConfiguration {
///
/// // needed by the `Routable` protocol
/// // to identify this ViewController in the hierarchy
/// var routeIdentifier: RouteElementIdentifier {
/// return "listScreen"
/// }
///
/// // the `NavigationRequest`s that this ViewController is handling
/// // with the `NavigationInstruction` to execute
/// var navigationConfiguration: [NavigationRequest: NavigationInstruction] {
/// return [
/// .show("addItemScreen"): .presentModally({ [unowned self] _ in
/// let vc = AddItemViewController(store: self.store)
/// return vc
/// })
/// ]
/// }
/// ```
///
public enum NavigationInstruction {
/// Define one of the two possible behaviours when dismissing a modal ViewController:
///
/// `.soft`: dismiss the ViewController but keep all the presented ViewControllers
///
/// `.hard`: the usual UIKit behaviour, dismiss the ViewController and all the ViewControllers that is presenting
public enum ModalDismissBehaviour {
/// If the targeted modal is presenting other modals, keep them alive.
case soft
/// While removing the targeted modal, remove also all the modals that it is presenting.
case hard
}
/// Push the ViewController using `UINavigationController.pushViewController(:animated:)`.
case push((_ context: Any?) -> UIViewController)
/// Pop the ViewController using `UINavigationController.popViewController(animated:)`.
case pop
/// Pops up to the root ViewController using `UINavigationcontroller.popToRootViewController(animated:)
case popToRootViewController
/// Pops up to a ViewController using `UINavigationcontroller.popToViewController(:animated:)
case popToViewController(identifier: RouteElementIdentifier)
/// Present the ViewController modally using `UIViewController.present(:animated:completion:)`.
case presentModally((_ context: Any?) -> UIViewController)
/// Dismiss the ViewController presented modally using `UIViewController.dismiss(animated:completion:)`.
case dismissModally(behaviour: ModalDismissBehaviour)
/// Define your custom implementation of the navigation.
case custom(CustomNavigationOptionClosure)
/// Define your custom implementation of the navigation.
/// If the closure returns false, the routing continues as if the navigation instruction is not defined
case optionalCustom(OptionalCustomNavigationOptionClosure)
func handle(
sourceViewController: UIViewController,
identifier: RouteElementIdentifier,
from: RouteElementIdentifier,
animated: Bool,
context: Any?,
completion: @escaping RoutingCompletion
) -> Bool {
let handled: Bool
switch self {
case .push(let vcClosure):
let vc = vcClosure(context)
self.handlePush(sourceViewController: sourceViewController, childVC: vc, animated: animated, completion: completion)
handled = true
case .pop:
self.handlePop(sourceViewController: sourceViewController, animated: animated, completion: completion)
handled = true
case .popToRootViewController:
self.handlePopToRootViewController(sourceViewController: sourceViewController, animated: animated, completion: completion)
handled = true
case .popToViewController(let destinationIdentifier):
guard let destinationViewController = UIApplication.shared.currentViewControllers
.last(where: { ($0 as? Routable)?.routeIdentifier == destinationIdentifier }) else {
fatalError("PopToViewController requested to an unknown destination view controller")
}
self.handlePopToViewController(
sourceViewController: sourceViewController,
destinationViewController: destinationViewController,
animated: animated,
completion: completion
)
handled = true
case .presentModally(let vcClosure):
let vc = vcClosure(context)
self.handlePresentModally(
sourceViewController: sourceViewController,
childVC: vc,
animated: animated,
completion: completion
)
handled = true
case .dismissModally(let behaviour):
self.handleDismissModally(
sourceViewController: sourceViewController,
animated: animated,
behaviour: behaviour,
completion: completion
)
handled = true
case .custom(let closure):
closure(identifier, from, animated, context, completion)
handled = true
case .optionalCustom(let closure):
handled = closure(identifier, from, animated, context, completion)
}
return handled
}
private func handlePush(
sourceViewController: UIViewController,
childVC: UIViewController,
animated: Bool,
completion: @escaping RoutingCompletion
) {
if let navVC = sourceViewController as? UINavigationController {
navVC.pushViewController(childVC, animated: animated)
completion()
return
}
if let navVC = sourceViewController.navigationController {
navVC.pushViewController(childVC, animated: animated)
completion()
return
}
fatalError(
// swiftlint:disable:next line_length
"Push requested on a source view controller that is neither a UINavigationController instance nor part of a UINavigationController's stack"
)
}
private func handlePopToRootViewController(
sourceViewController: UIViewController,
animated: Bool,
completion: @escaping RoutingCompletion
) {
if let navVC = sourceViewController as? UINavigationController {
navVC.popToRootViewController(animated: animated)
completion()
return
}
if let navVC = sourceViewController.navigationController {
navVC.popToRootViewController(animated: animated)
completion()
return
}
fatalError(
// swiftlint:disable:next line_length
"PopToRootViewController requested on a source view controller that is neither a UINavigationController instance nor part of a UINavigationController's stack"
)
}
private func handlePopToViewController(
sourceViewController: UIViewController,
destinationViewController: UIViewController,
animated: Bool,
completion: @escaping RoutingCompletion
) {
if let navVC = sourceViewController as? UINavigationController {
navVC.popToViewController(destinationViewController, animated: animated)
completion()
return
}
if let navVC = sourceViewController.navigationController {
navVC.popToViewController(destinationViewController, animated: animated)
completion()
return
}
fatalError(
// swiftlint:disable:next line_length
"PopToViewController requested on a source view controller that is neither a UINavigationController instance nor part of a UINavigationController's stack"
)
}
private func handlePop(
sourceViewController: UIViewController,
animated: Bool,
completion: @escaping RoutingCompletion
) {
if let navVC = sourceViewController as? UINavigationController {
navVC.popViewController(animated: animated)
completion()
return
}
if let navVC = sourceViewController.navigationController {
navVC.popViewController(animated: animated)
completion()
return
}
fatalError(
// swiftlint:disable:next line_length
"Pop requested on a source view controller that is neither a UINavigationController instance nor part of a UINavigationController's stack"
)
}
private func handlePresentModally(
sourceViewController: UIViewController,
childVC: UIViewController,
animated: Bool,
completion: @escaping RoutingCompletion
) {
sourceViewController.recursivePresent(childVC, animated: animated, completion: completion)
}
private func handleDismissModally(
sourceViewController: UIViewController,
animated: Bool,
behaviour: NavigationInstruction.ModalDismissBehaviour,
completion: @escaping RoutingCompletion
) {
switch behaviour {
case .soft:
sourceViewController.softDismiss(animated: animated, completion: completion)
case .hard:
sourceViewController.dismiss(animated: animated, completion: completion)
}
}
}
/// A RoutableWithConfiguration is a `ViewController` that takes active part to the execution of a navigation action.
///
/// If a screen `listScreen` needs to present `addItemScreen`, the ViewController that is handling `listScreen` must
/// conform to the `RoutableWithConfiguration` protocol.
///
/// When a `Show("addItemScreen")` action is dispatched, the `Navigator` will capture the action and will start
/// finding a RoutableWithConfiguration in the active hierarchy that can handle the action.
/// If the `navigationConfiguration` of `listScreen` will match the `NavigationRequest` of `.show(addItemScreen)`
/// than the Navigator will execute the relative `NavigationInstruction` where you can
/// configure the ViewController to present.
///
/// There are others `NavigationRequest`s and `NavigationInstruction`s that can be used to define the navigation
/// structure of the app.
///
/// In case you need more control, you can always implement the `Routable` protocol yourself and have
/// fine grained control of the implementation of the navigation.
/// In fact, a `RoutableWithConfiguration` and its `navigationConfiguration` are used behind the scenes
/// to implement the `Routable` protocol for you.
///
/// ```swift
/// extension ListViewController: RoutableWithConfiguration {
///
/// // needed by the `Routable` protocol
/// // to identify this ViewController in the hierarchy
/// var routeIdentifier: RouteElementIdentifier {
/// return "listScreen"
/// }
///
/// // the `NavigationRequest`s that this ViewController is handling
/// // with the `NavigationInstruction` to execute
/// var navigationConfiguration: [NavigationRequest: NavigationInstruction] {
/// return [
/// .show("addItemScreen"): .presentModally({ [unowned self] _ in
/// let vc = AddItemViewController(store: self.store)
/// return vc
/// })
/// ]
/// }
/// ```
///
public protocol RoutableWithConfiguration: Routable {
/// The `NavigationRequest`s this RoutableWithConfiguration will handle
/// and the `NavigationInstruction`s that will be executed by the `Navigator`.
var navigationConfiguration: [NavigationRequest: NavigationInstruction] { get }
}
extension RoutableWithConfiguration where Self: UIViewController {
/// Method of the `Routable` protocol that the `RoutableWithConfiguration` is
/// implementing automatically looking at the `navigationConfiguration`.
public func show(
identifier: RouteElementIdentifier,
from: RouteElementIdentifier,
animated: Bool,
context: Any?,
completion: @escaping RoutingCompletion
) -> Bool {
for (source, instruction) in self.navigationConfiguration {
guard source.canHandle(identifier, kind: .show) else {
continue
}
let handled = instruction.handle(
sourceViewController: self,
identifier: identifier,
from: from,
animated: animated,
context: context,
completion: completion
)
if handled {
return true
}
}
return false
}
/// Method of the `Routable` protocol that the `RoutableWithConfiguration` is
/// implementing automatically looking at the `navigationConfiguration`.
public func hide(
identifier: RouteElementIdentifier,
from: RouteElementIdentifier,
animated: Bool,
context: Any?,
completion: @escaping RoutingCompletion
) -> Bool {
for (source, option) in self.navigationConfiguration {
guard source.canHandle(identifier, kind: .hide) else {
continue
}
let handled = option.handle(
sourceViewController: self,
identifier: identifier,
from: from,
animated: animated,
context: context,
completion: completion
)
if handled {
return true
}
}
return false
}
}