-
Notifications
You must be signed in to change notification settings - Fork 0
/
Navigator.swift
executable file
·223 lines (191 loc) · 6.64 KB
/
Navigator.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
//
// Navigator.swift
//
// Copyright (c) 2017 Marco Muratori
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
import UIKit
// MARK: -
// MARK: UIViewController extension
public protocol Navigable {}
extension UIViewController: Navigable {} // implement the protocol by default in all UIViewControllers
public extension Navigable where Self: UIViewController {
@discardableResult static func find(asyncMain: Navigator.asyncMainHandler<Self>? = nil) -> Self? {
return Navigator.find(self, asyncMain: asyncMain)
}
@discardableResult static func select(asyncMain: Navigator.asyncMainHandler<Self>? = nil) -> Self? {
return Navigator.select(self, asyncMain: asyncMain)
}
}
// MARK: -
// MARK: Navigator class
public class Navigator {
public static var debug = false {
didSet {
if debug {
NLog("Disable debug var inside Navigator class to shut down those comments")
}
}
}
/// This block is always called in the main thread.
public typealias asyncMainHandler<T: UIViewController> = ((_ container: UIViewController?, _ vc: T?) -> ())
@discardableResult public static func find<T: UIViewController>(_ type: T.Type, asyncMain: asyncMainHandler<T>? = nil) -> T? {
return lookFor(type, select: false, asyncMain: asyncMain)
}
@discardableResult public static func select<T: UIViewController>(_ type: T.Type, asyncMain: asyncMainHandler<T>? = nil) -> T? {
return lookFor(type, select: true, asyncMain: asyncMain)
}
@discardableResult private static func lookFor<T: UIViewController>(_ vcType: T.Type, select: Bool, asyncMain: asyncMainHandler<T>?) -> T? {
// recursive search
func checkIn(_ viewController: UIViewController?, stack: [StackObject] = [StackObject](), indent: String = "") -> [StackObject] {
var stack = stack
switch viewController {
case is T:
NLog(indent + "-> Found!")
if stack.last != nil {
stack[stack.count - 1].vc = viewController
}
return stack
case let parent? where parent.children.count > 0:
if stack.last != nil {
stack[stack.count - 1].vc = viewController
}
for vc in parent.children {
NLog(indent + "-> \(String(describing: type(of: vc))):")
let subStack = checkIn(vc, stack: [StackObject(parent: parent, vc: nil)], indent: indent + " ")
if subStack.last?.vc is T {
stack.append(contentsOf: subStack)
return stack
}
}
fallthrough // not found, check for presentedViewController in default case
default:
// The presentedViewController is != nil also when it has bee presented
// by an ancestor of the examined container.
// So we also need to check 'pvc.parent == container'.
if let parent = viewController,
let pvc = parent.presentedViewController,
pvc.parent == parent {
if stack.last != nil {
stack[stack.count - 1].vc = viewController
}
NLog(indent + "-> \(String(describing: type(of: pvc))).presentedViewController:")
let subStack = checkIn(pvc, stack: stack, indent: indent + " ")
if subStack.last?.vc is T {
stack.append(contentsOf: subStack)
}
}
return stack
}
}
guard let root = APP_ROOT else {
asyncMain?(nil, nil)
return nil
}
NLog("From \(String(describing: type(of: root)))...")
let stack = checkIn(root)
DispatchQueue.main.async(execute: { () -> Void in
if select {
for obj in stack {
if !obj.select() {
NLog("unable to select one of the elements in the stack:\n\(stack as AnyObject)")
break
}
}
}
asyncMain?(stack.last?.parent, stack.last?.vc as? T)
})
return stack.last?.vc as? T
}
static func NLog(_ msg: String) {
if debug {
print(msg.count > 0 ? "[Navigator]: \(msg)" : msg)
}
}
}
struct StackObject {
var parent: UIViewController?
var vc: UIViewController?
func select() -> Bool {
guard let vc = vc else {
return false
}
switch parent {
case let parent? where parent is UITabBarController:
(parent as! UITabBarController).selectedViewController = vc
return true
case let parent? where parent is UISplitViewController:
(parent as! UISplitViewController).showDetailViewController(vc, sender: nil)
return true
case let parent? where parent is UINavigationController:
let parent = parent as! UINavigationController
parent.popToRootViewController(animated: false)
// avoid pushing the same vc twice:
if parent.topViewController != vc {
parent.show(vc, sender: nil)
}
return true
default:
return false
}
}
}
// MARK: -
// MARK: Globals
// Returns the AppDelegate
public let APP_DELEGATE = UIApplication.shared.delegate
// Returns the App Window
public var APP_WINDOW: UIWindow? {
get {
return UIApplication.shared.keyWindow
}
}
// Returns the root viewController also if it is not in the view hierarchy
public var APP_ROOT: UIViewController? {
get {
var root = APP_WINDOW?.rootViewController
while root?.presentingViewController != nil {
root = root?.presentingViewController
}
return root
}
}
// Returns the root viewController which is in the view hierarchy
public var APP_ROOT_VH: UIViewController? {
get {
func isInViewHierarchy(_ vc: UIViewController?) -> Bool {
guard let vc = vc else { return false }
return vc.isViewLoaded && vc.view.window != nil
}
var vc = APP_ROOT
while !isInViewHierarchy(vc) {
if vc == nil { return nil }
vc = vc?.presentedViewController
}
return vc
}
}
// Returns the top most viewController
public var APP_TOP: UIViewController? {
var topController = APP_ROOT_VH
while topController?.presentedViewController != nil {
topController = topController!.presentedViewController
}
return topController
}