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
|
//
// RootContainerViewController.swift
// MullvadVPN
//
// Created by pronebird on 25/05/2019.
// Copyright © 2019 Amagicom AB. All rights reserved.
//
import UIKit
enum HeaderBarStyle {
case transparent, `default`, unsecured, secured
fileprivate func backgroundColor() -> UIColor {
switch self {
case .transparent:
return UIColor.clear
case .default:
return UIColor.HeaderBar.defaultBackgroundColor
case .secured:
return UIColor.HeaderBar.securedBackgroundColor
case .unsecured:
return UIColor.HeaderBar.unsecuredBackgroundColor
}
}
}
/// A protocol that defines the relationship between the root container and its child controllers
protocol RootContainment {
/// Return the preferred header bar style
var preferredHeaderBarStyle: HeaderBarStyle { get }
}
/// A root container class that primarily handles the unwind storyboard segues on log out
class RootContainerViewController: UIViewController {
typealias CompletionHandler = () -> Void
private var viewControllers = [UIViewController]()
private var topViewController: UIViewController? {
return viewControllers.last
}
@IBOutlet var headerBarView: UIView!
@IBOutlet var headerBarSettingsButton: UIButton!
@IBOutlet var transitionContainer: UIView!
private(set) var headerBarStyle = HeaderBarStyle.default
override var childForStatusBarStyle: UIViewController? {
return topViewController
}
override var childForStatusBarHidden: UIViewController? {
return topViewController
}
override var shouldAutomaticallyForwardAppearanceMethods: Bool {
return false
}
// MARK: - View lifecycle
override func viewDidLoad() {
super.viewDidLoad()
var margins = view.layoutMargins
margins.left = 24
margins.right = 24
view.layoutMargins = margins
updateHeaderBarBackground()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateAdditionalSafeAreaInsetsIfNeeded()
}
override func viewSafeAreaInsetsDidChange() {
super.viewSafeAreaInsetsDidChange()
updateHeaderBarLayoutMarginsIfNeeded()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
topViewController?.beginAppearanceTransition(true, animated: animated)
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
topViewController?.endAppearanceTransition()
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
topViewController?.beginAppearanceTransition(false, animated: animated)
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
topViewController?.endAppearanceTransition()
}
// MARK: - Storyboard segue handling
override func unwind(for unwindSegue: UIStoryboardSegue, towards subsequentVC: UIViewController) {
let index = viewControllers.firstIndex(of: subsequentVC)!
let newViewControllers = Array(viewControllers.prefix(through: index))
let animated = UIView.areAnimationsEnabled
setViewControllers(newViewControllers, animated: animated)
}
// MARK: - Public
func setViewControllers(_ newViewControllers: [UIViewController], animated: Bool, completion: CompletionHandler? = nil) {
// Dot not handle appearance events when the container itself is not visible
let shouldHandleAppearanceEvents = view.window != nil
// Animations won't run when the container is not visible, so prevent them
let shouldAnimate = animated && shouldHandleAppearanceEvents
let sourceViewController = topViewController
let targetViewController = newViewControllers.last
let viewControllersToAdd = newViewControllers.filter { !viewControllers.contains($0) }
let viewControllersToRemove = viewControllers.filter { !newViewControllers.contains($0) }
let finishTransition = {
// Notify the added controllers that they finished a transition into the container
for child in viewControllersToAdd {
child.didMove(toParent: self)
}
// Remove the controllers that transitioned out of the container
// The call to removeFromParent() automatically calls child.didMove()
for child in viewControllersToRemove {
child.view.removeFromSuperview()
child.removeFromParent()
}
// Remove the source controller from view hierarchy
if sourceViewController != targetViewController {
sourceViewController?.view.removeFromSuperview()
}
// Finish appearance transition
if shouldHandleAppearanceEvents {
sourceViewController?.endAppearanceTransition()
if sourceViewController != targetViewController {
targetViewController?.endAppearanceTransition()
}
}
completion?()
}
let alongSideAnimations = {
self.updateHeaderBarStyleFromChildPreferences(animated: shouldAnimate)
}
// Make sure that all new view controllers have loaded their views
// This is important because the unwind segue calls the unwind action which may rely on
// IB outlets to be set at that time.
for newViewController in newViewControllers {
newViewController.loadViewIfNeeded()
}
// Add new child controllers. The call to addChild() automatically calls child.willMove()
// Children have to be registered in the container for Storyboard unwind segues to function
// properly, however the child controller views don't have to be added immediately, and
// appearance methods have to be handled manually.
for child in viewControllersToAdd {
addChild(child)
}
// Add the destination view into the view hierarchy
if let targetView = targetViewController?.view {
addChildView(targetView)
}
// Notify the controllers that they will transition out of the container
for child in viewControllersToRemove {
child.willMove(toParent: nil)
}
viewControllers = newViewControllers
// Begin appearance transition
if shouldHandleAppearanceEvents {
sourceViewController?.beginAppearanceTransition(false, animated: shouldAnimate)
if sourceViewController != targetViewController {
targetViewController?.beginAppearanceTransition(true, animated: shouldAnimate)
}
}
if shouldAnimate {
CATransaction.begin()
CATransaction.setCompletionBlock {
finishTransition()
}
let transition = CATransition()
transition.duration = 0.35
transition.type = .push
// Pick the animation movement direction
let sourceIndex = sourceViewController.flatMap({ newViewControllers.firstIndex(of: $0) })
let targetIndex = targetViewController.flatMap({ newViewControllers.firstIndex(of: $0) })
switch (sourceIndex, targetIndex) {
case (.some(let lhs), .some(let rhs)):
transition.subtype = lhs > rhs ? .fromLeft : .fromRight
case (.none, .some):
transition.subtype = .fromLeft
default:
transition.subtype = .fromRight
}
transitionContainer.layer.add(transition, forKey: "transition")
alongSideAnimations()
CATransaction.commit()
} else {
alongSideAnimations()
finishTransition()
}
}
func pushViewController(_ viewController: UIViewController, animated: Bool) {
var newViewControllers = viewControllers.filter({ $0 != viewController })
newViewControllers.append(viewController)
setViewControllers(newViewControllers, animated: animated)
}
/// Request the root container to query the top controller for the new header bar style
func setNeedsHeaderBarStyleAppearance() {
updateHeaderBarStyleFromChildPreferences(animated: UIView.areAnimationsEnabled)
}
// MARK: - Actions
@IBAction func doShowSettings() {
performSegue(withIdentifier: SegueIdentifier.Root.showSettings.rawValue, sender: self)
}
// MARK: - Private
private func addChildView(_ childView: UIView) {
childView.translatesAutoresizingMaskIntoConstraints = true
childView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
childView.frame = transitionContainer.bounds
transitionContainer.addSubview(childView)
}
/// Updates the header bar's layout margins to make sure it doesn't go below the system status
/// bar.
private func updateHeaderBarLayoutMarginsIfNeeded() {
let offsetTop = view.safeAreaInsets.top - additionalSafeAreaInsets.top
var layoutMargins = headerBarView.layoutMargins
layoutMargins.top = offsetTop
layoutMargins.left = view.layoutMargins.left
layoutMargins.right = view.layoutMargins.right
layoutMargins.bottom = 0
if layoutMargins != headerBarView.layoutMargins {
headerBarView.layoutMargins = layoutMargins
}
}
/// Updates additional safe area insets to push the child views below the header bar
private func updateAdditionalSafeAreaInsetsIfNeeded() {
var safeAreaInstes = additionalSafeAreaInsets
safeAreaInstes.top = headerBarView.frame.height
if additionalSafeAreaInsets != safeAreaInstes {
additionalSafeAreaInsets = safeAreaInstes
}
}
private func setHeaderBarStyle(_ style: HeaderBarStyle, animated: Bool) {
headerBarStyle = style
let action = {
self.updateHeaderBarBackground()
}
if animated {
UIView.animate(withDuration: 0.25, animations: action)
} else {
action()
}
}
private func updateHeaderBarBackground() {
headerBarView.backgroundColor = headerBarStyle.backgroundColor()
}
private func updateHeaderBarStyleFromChildPreferences(animated: Bool) {
if let conforming = topViewController as? RootContainment {
setHeaderBarStyle(conforming.preferredHeaderBarStyle, animated: animated)
}
}
}
class RootContainerPushSegue: UIStoryboardSegue {
override func perform() {
let container = source.rootContainerController!
let animated = UIView.areAnimationsEnabled
container.pushViewController(destination, animated: animated)
}
}
extension UIViewController {
var rootContainerController: RootContainerViewController? {
var viewController: UIViewController? = parent
while viewController != nil {
if let container = viewController as? RootContainerViewController {
return container
}
viewController = viewController?.parent
}
return nil
}
}
|