-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDefaultRouter.swift
More file actions
61 lines (48 loc) · 1.75 KB
/
DefaultRouter.swift
File metadata and controls
61 lines (48 loc) · 1.75 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
//
// DefaultRouter.swift
// RoutingExample
//
// Created by Cassius Pacheco on 8/3/20.
// Copyright © 2020 Cassius Pacheco. All rights reserved.
//
import UIKit
import DependencyContainer
class DefaultRouter: NSObject, Router, Closable, Dismissable {
private let rootTransition: Transition
weak var root: UIViewController?
var container: DependencyContainer
init(rootTransition: Transition, container: DependencyContainer) {
self.rootTransition = rootTransition
self.container = container
}
deinit {
print("🗑 Deallocating \(self) with \(String(describing: rootTransition))")
}
// MARK: - Routable
func route(to viewController: UIViewController, as transition: Transition, completion: (() -> Void)?) {
guard let root = root else { return }
transition.open(viewController, from: root, completion: completion)
}
func route(to viewController: UIViewController, as transition: Transition) {
route(to: viewController, as: transition, completion: nil)
}
// MARK: - Closable
func close(completion: (() -> Void)?) {
guard let root = root else { return }
// Removes the `root` with the same transition that it was opened.
rootTransition.close(root, completion: completion)
}
func close() {
close(completion: nil)
}
// MARK: - Dismissable
func dismiss(completion: (() -> Void)?) {
// Dismiss the root with iOS' default dismiss animation.
// It will only work if the root or its ancestor were presented
// using iOS' native present view controller method.
root?.dismiss(animated: rootTransition.isAnimated, completion: completion)
}
func dismiss() {
dismiss(completion: nil)
}
}