Newer
Older
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
import UIKit
import Theme
public protocol Presenting {
func present(_ target: UIViewController, from parent: UIViewController)
func dismiss(from parent: UIViewController)
}
public extension Presenting {
func dismiss(from parent: UIViewController) {
parent.dismiss(animated: true)
}
}
public struct PushPresenter: Presenting {
public init() {}
public func present(_ target: UIViewController, from parent: UIViewController) {
parent.navigationController?.pushViewController(target, animated: true)
}
}
public struct ModalPresenter: Presenting {
public init() {}
public func present(_ target: UIViewController, from parent: UIViewController) {
let statusBarVC = StatusBarViewController(target)
statusBarVC.modalPresentationStyle = .fullScreen
parent.present(statusBarVC, animated: true)
}
}
public struct ReplacePresenter: Presenting {
public enum Mode {
case replaceAll
case replaceLast
case replaceBackwards(AnyObject.Type)
}
var mode: Mode
public init(mode: Mode = .replaceAll) {
self.mode = mode
}
public func present(_ target: UIViewController, from parent: UIViewController) {
guard let navigationController = parent.navigationController else { return }
switch mode {
case .replaceAll:
navigationController.setViewControllers([target], animated: true)
case .replaceBackwards(let OlderInStack):
if let oldScreen = navigationController.viewControllers.filter({ $0.isKind(of: OlderInStack.self) }).first,
let index = navigationController.viewControllers.firstIndex(of: oldScreen) {
let viewControllersBefore =
navigationController.viewControllers.dropLast(
navigationController.viewControllers.count - index
)
if let coordinator = navigationController.transitionCoordinator {
coordinator.animate(alongsideTransition: nil) { _ in
navigationController.setViewControllers(viewControllersBefore + [target] , animated: true)
}
} else {
navigationController.setViewControllers(viewControllersBefore + [target] , animated: true)
}
} else {
navigationController.pushViewController(target, animated: true)
}
case .replaceLast:
let viewControllersBefore = navigationController.viewControllers.dropLast()
func replace() {
navigationController.setViewControllers(viewControllersBefore + [target] , animated: true)
}
if let coordinator = navigationController.transitionCoordinator {
coordinator.animate(alongsideTransition: nil) { _ in
replace()
}
} else {
replace()
}
}
}
}
public struct PopReplacePresenter: Presenting {
public init() {}
public func present(_ target: UIViewController, from parent: UIViewController) {
if let lastViewController = parent.navigationController?.viewControllers.last {
parent.navigationController?.setViewControllers([target, lastViewController], animated: false)
parent.navigationController?.setViewControllers([target], animated: true)
}
}
}