-
Notifications
You must be signed in to change notification settings - Fork 384
/
Copy pathRootContainerViewController.swift
911 lines (739 loc) · 30.8 KB
/
RootContainerViewController.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
//
// RootContainerViewController.swift
// MullvadVPN
//
// Created by pronebird on 25/05/2019.
// Copyright © 2019 Mullvad VPN AB. All rights reserved.
//
import Routing
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
}
}
}
struct HeaderBarPresentation {
let style: HeaderBarStyle
let showsDivider: Bool
static var `default`: HeaderBarPresentation {
HeaderBarPresentation(style: .default, showsDivider: false)
}
}
/// A protocol that defines the relationship between the root container and its child controllers
protocol RootContainment {
/// Return the preferred header bar style
var preferredHeaderBarPresentation: HeaderBarPresentation { get }
/// Return true if the view controller prefers header bar hidden
var prefersHeaderBarHidden: Bool { get }
/// Return true if the view controller prefers notification bar hidden
var prefersNotificationBarHidden: Bool { get }
/// Return true if the view controller prefers device info bar hidden
var prefersDeviceInfoBarHidden: Bool { get }
}
extension RootContainment {
var prefersNotificationBarHidden: Bool {
false
}
var prefersDeviceInfoBarHidden: Bool {
false
}
}
protocol RootContainerViewControllerDelegate: AnyObject {
func rootContainerViewControllerShouldShowAccount(
_ controller: RootContainerViewController,
animated: Bool
)
func rootContainerViewControllerShouldShowSettings(
_ controller: RootContainerViewController,
navigateTo route: SettingsNavigationRoute?,
animated: Bool
)
func rootContainerViewSupportedInterfaceOrientations(_ controller: RootContainerViewController)
-> UIInterfaceOrientationMask
func rootContainerViewAccessibilityPerformMagicTap(_ controller: RootContainerViewController)
-> Bool
}
/// A root container view controller
class RootContainerViewController: UIViewController {
typealias CompletionHandler = () -> Void
private let headerBarView = HeaderBarView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
let transitionContainer = UIView(frame: UIScreen.main.bounds)
private var presentationContainerAccountButton: UIButton?
private var presentationContainerSettingsButton: UIButton?
private var configuration = RootConfiguration(showsAccountButton: false)
private(set) var headerBarPresentation = HeaderBarPresentation.default
private(set) var headerBarHidden = false
private(set) var overrideHeaderBarHidden: Bool?
private(set) var viewControllers = [UIViewController]()
private var appearingController: UIViewController?
private var disappearingController: UIViewController?
private var interfaceOrientationMask: UIInterfaceOrientationMask?
private var isNavigationBarHidden = false {
didSet {
guard let notificationController else {
return
}
if isNavigationBarHidden {
removeNotificationController(notificationController)
} else {
addNotificationController(notificationController)
}
}
}
var topViewController: UIViewController? {
viewControllers.last
}
weak var delegate: (any RootContainerViewControllerDelegate)?
override var childForStatusBarStyle: UIViewController? {
topViewController
}
override var childForStatusBarHidden: UIViewController? {
topViewController
}
override var shouldAutomaticallyForwardAppearanceMethods: Bool {
false
}
override var disablesAutomaticKeyboardDismissal: Bool {
topViewController?.disablesAutomaticKeyboardDismissal ?? super
.disablesAutomaticKeyboardDismissal
}
// MARK: - View lifecycle
override func viewDidLoad() {
super.viewDidLoad()
var margins = view.directionalLayoutMargins
margins.leading = UIMetrics.contentLayoutMargins.leading
margins.trailing = UIMetrics.contentLayoutMargins.trailing
view.directionalLayoutMargins = margins
definesPresentationContext = true
addTransitionView()
addHeaderBarView()
updateHeaderBarBackground()
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
updateAdditionalSafeAreaInsetsIfNeeded()
}
override func viewSafeAreaInsetsDidChange() {
super.viewSafeAreaInsetsDidChange()
updateHeaderBarLayoutMarginsIfNeeded()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let childController = topViewController {
beginChildControllerTransition(childController, isAppearing: true, animated: animated)
}
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
if let childController = topViewController {
endChildControllerTransition(childController)
}
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
if let childController = topViewController {
beginChildControllerTransition(childController, isAppearing: false, animated: animated)
}
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if let childController = topViewController {
endChildControllerTransition(childController)
}
}
// MARK: - Autorotation
override var shouldAutorotate: Bool {
true
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
interfaceOrientationMask ?? super.supportedInterfaceOrientations
}
// MARK: - Public
func setViewControllers(
_ newViewControllers: [UIViewController],
animated: Bool,
completion: CompletionHandler? = nil
) {
// Fetch the initial orientation mask
if interfaceOrientationMask == nil {
updateInterfaceOrientation(attemptRotateToDeviceOrientation: false)
}
setViewControllersInternal(
newViewControllers,
isUnwinding: false,
animated: animated,
completion: completion
)
}
func pushViewController(
_ viewController: UIViewController,
animated: Bool,
completion: CompletionHandler? = nil
) {
var newViewControllers = viewControllers.filter { $0 != viewController }
newViewControllers.append(viewController)
setViewControllersInternal(
newViewControllers,
isUnwinding: false,
animated: animated,
completion: completion
)
}
func popToViewController(
_ controller: UIViewController,
animated: Bool,
completion: CompletionHandler? = nil
) {
guard let index = viewControllers.firstIndex(of: controller) else { return }
let newViewControllers = Array(viewControllers[...index])
setViewControllersInternal(
newViewControllers,
isUnwinding: true,
animated: animated,
completion: completion
)
}
func popViewController(animated: Bool, completion: CompletionHandler? = nil) {
guard viewControllers.count > 1 else { return }
var newViewControllers = viewControllers
newViewControllers.removeLast()
setViewControllersInternal(
newViewControllers,
isUnwinding: true,
animated: animated,
completion: completion
)
}
func popToRootViewController(animated: Bool, completion: CompletionHandler? = nil) {
if let rootController = viewControllers.first, viewControllers.count > 1 {
setViewControllersInternal(
[rootController],
isUnwinding: true,
animated: animated,
completion: completion
)
}
}
/// Request the root container to query the top controller for the new header bar style
func updateHeaderBarAppearance() {
updateHeaderBarStyleFromChildPreferences(animated: UIView.areAnimationsEnabled)
}
func updateHeaderBarHiddenAppearance() {
updateHeaderBarHiddenFromChildPreferences(animated: UIView.areAnimationsEnabled)
}
/// Request to display settings controller
func showAccount(animated: Bool) {
delegate?.rootContainerViewControllerShouldShowAccount(
self,
animated: animated
)
}
/// Request to display settings controller
func showSettings(navigateTo route: SettingsNavigationRoute? = nil, animated: Bool) {
delegate?.rootContainerViewControllerShouldShowSettings(
self,
navigateTo: route,
animated: animated
)
}
/// Add account and settings bar buttons into the presentation container to make them accessible even
/// when the root container is covered with a modal.
func addTrailingButtonsToPresentationContainer(_ presentationContainer: UIView) {
let accountButton = getPresentationContainerAccountButton()
let settingsButton = getPresentationContainerSettingsButton()
presentationContainerAccountButton = accountButton
presentationContainerSettingsButton = settingsButton
// Hide the account button inside the header bar to avoid color blending issues
headerBarView.accountButton.alpha = 0
headerBarView.settingsButton.alpha = 0
presentationContainer.addConstrainedSubviews([accountButton, settingsButton]) {
accountButton.centerXAnchor
.constraint(equalTo: headerBarView.accountButton.centerXAnchor)
accountButton.centerYAnchor
.constraint(equalTo: headerBarView.accountButton.centerYAnchor)
settingsButton.centerXAnchor
.constraint(equalTo: headerBarView.settingsButton.centerXAnchor)
settingsButton.centerYAnchor
.constraint(equalTo: headerBarView.settingsButton.centerYAnchor)
}
}
func removeTrailingButtonsFromPresentationContainer() {
presentationContainerAccountButton?.removeFromSuperview()
presentationContainerSettingsButton?.removeFromSuperview()
headerBarView.accountButton.alpha = 1
headerBarView.settingsButton.alpha = 1
}
func setOverrideHeaderBarHidden(_ isHidden: Bool?, animated: Bool) {
overrideHeaderBarHidden = isHidden
if let isHidden {
setHeaderBarHidden(isHidden, animated: animated)
} else {
updateHeaderBarHiddenFromChildPreferences(animated: animated)
}
}
func enableHeaderBarButtons(_ enabled: Bool) {
headerBarView.accountButton.isEnabled = enabled
headerBarView.settingsButton.isEnabled = enabled
}
// MARK: - Accessibility
override func accessibilityPerformMagicTap() -> Bool {
delegate?.rootContainerViewAccessibilityPerformMagicTap(self) ?? super
.accessibilityPerformMagicTap()
}
// MARK: - Private
private func addTransitionView() {
let constraints = [
transitionContainer.topAnchor.constraint(equalTo: view.topAnchor),
transitionContainer.leadingAnchor.constraint(equalTo: view.leadingAnchor),
transitionContainer.trailingAnchor.constraint(equalTo: view.trailingAnchor),
transitionContainer.bottomAnchor.constraint(equalTo: view.bottomAnchor),
]
transitionContainer.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(transitionContainer)
NSLayoutConstraint.activate(constraints)
}
private func addHeaderBarView() {
let constraints = [
headerBarView.topAnchor.constraint(equalTo: view.topAnchor),
headerBarView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
headerBarView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
]
headerBarView.translatesAutoresizingMaskIntoConstraints = false
// Prevent automatic layout margins adjustment as we manually control them.
headerBarView.insetsLayoutMarginsFromSafeArea = false
headerBarView.accountButton.addTarget(
self,
action: #selector(handleAccountButtonTap),
for: .touchUpInside
)
headerBarView.settingsButton.addTarget(
self,
action: #selector(handleSettingsButtonTap),
for: .touchUpInside
)
view.addSubview(headerBarView)
NSLayoutConstraint.activate(constraints)
}
private func getPresentationContainerAccountButton() -> UIButton {
let button: UIButton
if let transitionViewButton = presentationContainerAccountButton {
transitionViewButton.removeFromSuperview()
button = transitionViewButton
} else {
button = HeaderBarView.makeHeaderBarButton(with: UIImage(named: "IconAccount"))
button.addTarget(
self,
action: #selector(handleAccountButtonTap),
for: .touchUpInside
)
}
button.isEnabled = headerBarView.accountButton.isEnabled
button.isHidden = !configuration.showsAccountButton
return button
}
private func getPresentationContainerSettingsButton() -> UIButton {
let button: UIButton
if let transitionViewButton = presentationContainerSettingsButton {
transitionViewButton.removeFromSuperview()
button = transitionViewButton
} else {
button = HeaderBarView.makeHeaderBarButton(with: UIImage(named: "IconSettings"))
button.isEnabled = headerBarView.settingsButton.isEnabled
button.addTarget(
self,
action: #selector(handleSettingsButtonTap),
for: .touchUpInside
)
}
return button
}
@objc private func handleAccountButtonTap() {
showAccount(animated: true)
}
@objc private func handleSettingsButtonTap() {
showSettings(animated: true)
}
// swiftlint:disable:next function_body_length
private func setViewControllersInternal(
_ newViewControllers: [UIViewController],
isUnwinding: Bool,
animated: Bool,
completion: CompletionHandler? = nil
) {
assert(
Set(newViewControllers).count == newViewControllers.count,
"All view controllers in root container controller must be distinct"
)
guard viewControllers != newViewControllers else {
completion?()
return
}
// 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) }
// hide in-App notificationBanner when the container decides to keep it invisible
isNavigationBarHidden = (targetViewController as? any RootContainment)?.prefersNotificationBarHidden ?? false
configureViewControllers(
viewControllersToAdd: viewControllersToAdd,
newViewControllers: newViewControllers,
targetViewController: targetViewController,
viewControllersToRemove: viewControllersToRemove
)
beginTransition(
shouldHandleAppearanceEvents: shouldHandleAppearanceEvents,
targetViewController: targetViewController,
shouldAnimate: shouldAnimate,
sourceViewController: sourceViewController
)
let finishTransition = { [weak self] in
self?.onTransitionEnd(
shouldHandleAppearanceEvents: shouldHandleAppearanceEvents,
sourceViewController: sourceViewController,
targetViewController: targetViewController,
viewControllersToAdd: viewControllersToAdd,
viewControllersToRemove: viewControllersToRemove
)
completion?()
}
let alongSideAnimations = { [weak self] in
guard let self else { return }
updateHeaderBarStyleFromChildPreferences(animated: shouldAnimate)
updateHeaderBarHiddenFromChildPreferences(animated: shouldAnimate)
updateNotificationBarHiddenFromChildPreferences()
updateDeviceInfoBarHiddenFromChildPreferences()
}
if shouldAnimate {
CATransaction.begin()
CATransaction.setCompletionBlock {
finishTransition()
}
animateTransition(
sourceViewController: sourceViewController,
newViewControllers: newViewControllers,
targetViewController: targetViewController,
isUnwinding: isUnwinding,
alongSideAnimations: alongSideAnimations
)
CATransaction.commit()
} else {
alongSideAnimations()
finishTransition()
}
}
private func animateTransition(
sourceViewController: UIViewController?,
newViewControllers: [UIViewController],
targetViewController: UIViewController?,
isUnwinding: Bool,
alongSideAnimations: () -> Void
) {
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 let (.some(lhs), .some(rhs)):
transition.subtype = lhs > rhs ? .fromLeft : .fromRight
case (.none, .some):
transition.subtype = isUnwinding ? .fromLeft : .fromRight
default:
transition.subtype = .fromRight
}
transitionContainer.layer.add(transition, forKey: "transition")
alongSideAnimations()
}
private func onTransitionEnd(
shouldHandleAppearanceEvents: Bool,
sourceViewController: UIViewController?,
targetViewController: UIViewController?,
viewControllersToAdd: [UIViewController],
viewControllersToRemove: [UIViewController]
) {
/*
Finish transition appearance.
Note this has to be done before the call to `didMove(to:)` or `removeFromParent()`
otherwise `endAppearanceTransition()` will fire `didMove(to:)` twice.
*/
if shouldHandleAppearanceEvents {
if let targetViewController,
sourceViewController != targetViewController {
self.endChildControllerTransition(targetViewController)
}
if let sourceViewController,
sourceViewController != targetViewController {
self.endChildControllerTransition(sourceViewController)
}
}
// 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()
}
self.updateInterfaceOrientation(attemptRotateToDeviceOrientation: true)
self.updateAccessibilityElementsAndNotifyScreenChange()
}
private func configureViewControllers(
viewControllersToAdd: [UIViewController],
newViewControllers: [UIViewController],
targetViewController: UIViewController?,
viewControllersToRemove: [UIViewController]
) {
// 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)
}
// 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 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
}
private func beginTransition(
shouldHandleAppearanceEvents: Bool,
targetViewController: UIViewController?,
shouldAnimate: Bool,
sourceViewController: UIViewController?
) {
if shouldHandleAppearanceEvents {
if let sourceViewController,
sourceViewController != targetViewController {
beginChildControllerTransition(
sourceViewController,
isAppearing: false,
animated: shouldAnimate
)
}
if let targetViewController,
sourceViewController != targetViewController {
beginChildControllerTransition(
targetViewController,
isAppearing: true,
animated: shouldAnimate
)
}
setNeedsStatusBarAppearanceUpdate()
}
}
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
if headerBarView.layoutMargins.top != offsetTop {
headerBarView.layoutMargins.top = offsetTop
}
}
/// Updates additional safe area insets to push the child views below the header bar
private func updateAdditionalSafeAreaInsetsIfNeeded() {
let offsetTop = view.safeAreaInsets.top - additionalSafeAreaInsets.top
let insetTop = headerBarHidden ? 0 : headerBarView.frame.height - offsetTop
if additionalSafeAreaInsets.top != insetTop {
additionalSafeAreaInsets.top = insetTop
}
}
private func setHeaderBarPresentation(_ presentation: HeaderBarPresentation, animated: Bool) {
headerBarPresentation = presentation
let action = {
self.updateHeaderBarBackground()
}
if animated {
UIView.animate(withDuration: 0.25, animations: action)
} else {
action()
}
}
private func setHeaderBarHidden(_ hidden: Bool, animated: Bool) {
headerBarHidden = hidden
let action = {
self.headerBarView.alpha = hidden ? 0 : 1
}
if animated {
UIView.animate(withDuration: 0.25, animations: action)
} else {
action()
}
}
private func updateHeaderBarBackground() {
headerBarView.backgroundColor = headerBarPresentation.style.backgroundColor()
headerBarView.showsDivider = headerBarPresentation.showsDivider
}
private func updateHeaderBarStyleFromChildPreferences(animated: Bool) {
if let conforming = topViewController as? any RootContainment {
setHeaderBarPresentation(conforming.preferredHeaderBarPresentation, animated: animated)
}
}
private func updateDeviceInfoBarHiddenFromChildPreferences() {
if let conforming = topViewController as? any RootContainment {
headerBarView.isDeviceInfoHidden = conforming.prefersDeviceInfoBarHidden
}
}
private func updateNotificationBarHiddenFromChildPreferences() {
if let notificationController,
let conforming = topViewController as? any RootContainment {
if conforming.prefersNotificationBarHidden {
removeNotificationController(notificationController)
} else {
addNotificationController(notificationController)
}
}
}
private func updateHeaderBarHiddenFromChildPreferences(animated: Bool) {
guard overrideHeaderBarHidden == nil else { return }
if let conforming = topViewController as? any RootContainment {
setHeaderBarHidden(conforming.prefersHeaderBarHidden, animated: animated)
}
}
private func updateInterfaceOrientation(attemptRotateToDeviceOrientation: Bool) {
let newSupportedOrientations = delegate?
.rootContainerViewSupportedInterfaceOrientations(self)
if interfaceOrientationMask != newSupportedOrientations {
interfaceOrientationMask = newSupportedOrientations
// Tell UIKit to update the interface orientation
if attemptRotateToDeviceOrientation {
Self.attemptRotationToDeviceOrientation()
}
}
}
private func beginChildControllerTransition(
_ controller: UIViewController,
isAppearing: Bool,
animated: Bool
) {
if appearingController != controller, isAppearing {
appearingController = controller
controller.beginAppearanceTransition(isAppearing, animated: animated)
}
if disappearingController != controller, !isAppearing {
disappearingController = controller
controller.beginAppearanceTransition(isAppearing, animated: animated)
}
}
private func endChildControllerTransition(_ controller: UIViewController) {
if controller == appearingController {
appearingController = nil
controller.endAppearanceTransition()
}
if controller == disappearingController {
disappearingController = nil
controller.endAppearanceTransition()
}
}
private func updateAccessibilityElementsAndNotifyScreenChange() {
// Update accessibility elements to define the correct navigation order: header bar, content
// view.
view.accessibilityElements = [headerBarView, topViewController?.view].compactMap { $0 }
// Tell accessibility that the significant part of screen was changed.
UIAccessibility.post(notification: .screenChanged, argument: nil)
}
// MARK: - Notification controller support
/**
An instance of notification controller presented within container.
*/
var notificationController: NotificationController? {
didSet {
guard oldValue != notificationController else { return }
oldValue.flatMap { removeNotificationController($0) }
notificationController.flatMap { addNotificationController($0) }
}
}
/**
Layout guide for notification view.
When set, notification view follows the layout guide that defines its dimensions and position, otherwise it's
laid out within container's safe area.
*/
var notificationViewLayoutGuide: UILayoutGuide? {
didSet {
notificationController.flatMap { updateNotificationViewConstraints($0) }
}
}
private var notificationViewConstraints: [NSLayoutConstraint] = []
private func updateNotificationViewConstraints(_ notificationController: NotificationController) {
let newConstraints = notificationController.view
.pinEdgesTo(notificationViewLayoutGuide ?? view.safeAreaLayoutGuide)
NSLayoutConstraint.deactivate(notificationViewConstraints)
NSLayoutConstraint.activate(newConstraints)
notificationViewConstraints = newConstraints
}
private func addNotificationController(_ notificationController: NotificationController) {
guard let notificationView = notificationController.view else { return }
notificationView.configureForAutoLayout()
addChild(notificationController)
view.addSubview(notificationView)
notificationController.didMove(toParent: self)
updateNotificationViewConstraints(notificationController)
}
private func removeNotificationController(_ notificationController: NotificationController) {
notificationController.willMove(toParent: nil)
notificationController.view.removeFromSuperview()
notificationController.removeFromParent()
}
}
/// A UIViewController extension that gives view controllers an access to root container
extension UIViewController {
var rootContainerController: RootContainerViewController? {
var current: UIViewController? = self
let iterator = AnyIterator { () -> UIViewController? in
current = current?.parent
return current
}
return iterator.first { $0 is RootContainerViewController } as? RootContainerViewController
}
func setNeedsHeaderBarStyleAppearanceUpdate() {
rootContainerController?.updateHeaderBarAppearance()
}
func setNeedsHeaderBarHiddenAppearanceUpdate() {
rootContainerController?.updateHeaderBarHiddenAppearance()
}
}
extension RootContainerViewController {
func update(configuration: RootConfiguration) {
self.configuration = configuration
presentationContainerAccountButton?.isHidden = !configuration.showsAccountButton
headerBarView.update(configuration: configuration)
}
// swiftlint:disable:next file_length
}