-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
ActionAreaController.swift
1141 lines (984 loc) · 39.8 KB
/
ActionAreaController.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
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2019 Google LLC. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// swiftlint:disable file_length
import SnapKit
import UIKit
extension ActionArea {
/// Manages presentation and adaptivity for the Action Area.
final class Controller: UIViewController, ActionAreaBarButtonItemDelegate {
private enum Metrics {
static let defaultAnimationDuration: TimeInterval = 0.4
// The fractional width of the master content area.
static let preferredPrimaryColumnWidthFraction: CGFloat = 0.6
}
/// The state of the Action Area.
enum State {
/// In the `normal` state, content can be shown and dismissed without restriction.
case normal
/// In the `modal` state, the primary content cannot be changed, and detail content is
/// hidden and re-shown instead of being dismissed.
case modal
}
private enum Layout: Equatable, CustomDebugStringConvertible {
enum Mode: Equatable, CustomDebugStringConvertible {
case collapsed
case expanded
var debugDescription: String {
switch self {
case .collapsed:
return "collapsed"
case .expanded:
return "expanded"
}
}
}
case portrait(CGSize, Mode)
case landscape(CGSize, Mode)
init(_ size: CGSize, and masterContentCount: Int = 0) {
if UIDevice.current.userInterfaceIdiom == .pad {
if size.isWiderThanTall {
if masterContentCount > 0 {
self = .landscape(size, .expanded)
} else {
self = .landscape(size, .collapsed)
}
} else {
self = .portrait(size, .collapsed)
}
} else {
self = .portrait(size, .collapsed)
}
}
var isExpanded: Bool {
switch self {
case .landscape(_, .expanded):
return true
case .portrait, .landscape(_, .collapsed):
return false
}
}
var isCollapsed: Bool {
return isExpanded == false
}
var mode: Mode {
switch self {
case let .portrait(_, mode), let .landscape(_, mode):
return mode
}
}
var divider: CGFloat { return 0.5 }
var detailWidth: CGFloat { return 476 }
var detailOffset: CGFloat { return detailWidth + divider }
private var minMasterWidth: CGFloat { return maxMasterWidth - detailWidth - divider }
private var maxMasterWidthInset: CGFloat { return maxMasterWidth - minMasterWidth }
private var masterWidthInset: CGFloat { return (maxMasterWidth - minMasterWidth) / 2 }
private var maxMasterWidth: CGFloat {
switch self {
case let .portrait(size, _), let .landscape(size, _):
return size.width
}
}
func masterLayoutMargins(
including systemMinimumLayoutMargins: NSDirectionalEdgeInsets
) -> UIEdgeInsets {
return UIEdgeInsets(
top: 0,
left: masterWidthInset,
bottom: 0,
right: masterWidthInset + systemMinimumLayoutMargins.leading
)
}
func expand() -> Layout {
switch self {
case .portrait:
preconditionFailure("Expansion is not supported in portrait.")
case .landscape(_, .expanded):
preconditionFailure("The layout is already expanded.")
case let .landscape(size, .collapsed):
return .landscape(size, .expanded)
}
}
func collapse() -> Layout {
switch self {
case .portrait:
preconditionFailure("Expansion is not supported in portrait.")
case .landscape(_, .collapsed):
preconditionFailure("The layout is already collapsed.")
case let .landscape(size, .expanded):
return .landscape(size, .collapsed)
}
}
var debugDescription: String {
switch self {
case .portrait:
return "portrait(\(mode))"
case let .landscape(_, mode):
return "landscape(\(mode))"
}
}
}
private var layout: Layout = Layout(UIScreen.main.bounds.size) {
didSet {
// The modal VC may need to update its bar button items when the layout changes.
modalDetailViewController?.actionAreaStateDidChange(self)
}
}
/// If the Action Area is using the expanded layout.
var isExpanded: Bool { return layout.isExpanded }
/// If the master content is currently visible.
var isMasterVisible: Bool {
if isExpanded { return true }
return presentedDetailViewController == nil
}
/// The navigation controller that displays content in a master context.
/// This is currently here for backwards compatibility. Consider using `show` if appropriate.
private(set) lazy var navController: UINavigationController = {
let nc = UINavigationController()
nc.isNavigationBarHidden = true
nc.delegate = self
return nc
}()
private lazy var detailNavController: UINavigationController = {
let nc = UINavigationController()
nc.isNavigationBarHidden = true
nc.delegate = self
return nc
}()
private lazy var masterBarViewController = BarViewController(content: navController)
private lazy var detailBarViewController = BarViewController(content: detailNavController)
private var presentedMasterViewController: MasterContent? {
return navController.topViewController as? MasterContent
}
private var presentedDetailViewController: DetailContent? {
if isExpanded, detailNavController.topViewController is ActionArea.EmptyState {
return nil
}
return detailContent.last
}
private var modalDetailViewController: DetailContent?
private typealias TargetAction = (target: AnyObject?, action: Selector)
private var masterTargetsAndActions: [TargetAction] = []
private var transitionType: MasterTransitionType = .external
private lazy var keyTint = KeyTint(
provider: { self.masterContent.last?.customTint },
tintables: [masterBarViewController, detailBarViewController]
)
private(set) var state: State = .normal {
willSet {
guard state != newValue else {
fatalError("Setting the state to the existing state is not allowed.")
}
}
didSet {
guard presentedDetailViewController != nil || modalDetailViewController != nil else {
fatalError("The state can only be changed when a detailViewController is shown.")
}
switch state {
case .normal:
modalDetailViewController?.actionAreaStateDidChange(self)
modalDetailViewController = nil
case .modal:
modalDetailViewController = presentedDetailViewController
modalDetailViewController?.actionAreaStateDidChange(self)
}
updateBarButtonItems(with: transitionCoordinator)
transitionCoordinator?.animate(alongsideTransition: { _ in
self.keyTint.updateAndApply()
})
}
}
// Initiate a transition by presenting a hidden view controller. This ensures we use the
// same duration and easing curves, and it simplifies animation code because it doesn't have
// to handle animating without a `transitionCoordinator`.
@discardableResult
private func initiateLocalTransition(
completion: @escaping () -> Void = {}
) -> UIViewControllerTransitionCoordinator {
let hidden = UIViewController()
// Hide the view, so the currently visible content won't be affected.
hidden.view.isHidden = true
// Use an `over` presentation style, so the presenting view won't be removed.
hidden.modalPresentationStyle = .overFullScreen
present(hidden, animated: true) {
// Dismiss the view controller to return things to the previous state.
hidden.dismiss(animated: false) {
completion()
}
}
guard let transitionCoordinator = hidden.transitionCoordinator else {
preconditionFailure("Initiating a presentation should create a transition coordinator.")
}
return transitionCoordinator
}
private weak var actionEnabler: FeatureEnabler? {
didSet {
oldValue?.unobserve()
actionEnabler?.observe(animateActionEnablement(actionsAreEnabled:))
}
}
private weak var barElevator: FeatureEnabler? {
didSet {
oldValue?.unobserve()
barElevator?.observe(animateBarElevation(barIsElevated:))
}
}
private var masterContent: [MasterContent] {
return navController.viewControllers.reduce(into: []) { masterContent, vc in
if let master = vc as? ActionArea.MasterContent {
masterContent.append(master)
}
}
}
private var emptyStates: [EmptyState] {
return masterContent.map { $0.emptyState }
}
private var detailContent: [DetailContent] {
return (navController.viewControllers + detailNavController.viewControllers)
.reduce(into: []) { detailContent, aVC in
if let detail = aVC as? DetailContent {
detailContent.append(detail)
}
}
}
// MARK: - Initializers
init() {
precondition(FeatureFlags.isActionAreaEnabled,
"This class can only be used when Action Area is enabled.")
super.init(nibName: nil, bundle: nil)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Lifecycle
private lazy var layoutConstraints = MutuallyExclusiveConstraints<Layout.Mode> { constraints in
constraints[.collapsed] = masterBarViewController.view.snp.prepareConstraints { prepare in
prepare.bottom.trailing.equalToSuperview()
}
constraints[.expanded] = detailBarViewController.view.snp.prepareConstraints { prepare in
prepare.bottom.trailing.equalToSuperview()
}
}
private lazy var transitioningNavigationController: UINavigationController = {
let vc = UINavigationController()
vc.isNavigationBarHidden = true
vc.view.isHidden = true
return vc
}()
private lazy var actionAreaContent: UIViewController = {
let vc = UIViewController()
vc.view.backgroundColor = .black
return vc
}()
override func viewDidLoad() {
super.viewDidLoad()
addChild(actionAreaContent)
view.addSubview(actionAreaContent.view)
actionAreaContent.didMove(toParent: self)
actionAreaContent.addChild(masterBarViewController)
actionAreaContent.view.addSubview(masterBarViewController.view)
masterBarViewController.didMove(toParent: actionAreaContent)
masterBarViewController.view.snp.makeConstraints { make in
make.top.leading.equalToSuperview()
}
actionAreaContent.addChild(detailBarViewController)
actionAreaContent.view.addSubview(detailBarViewController.view)
detailBarViewController.didMove(toParent: actionAreaContent)
detailBarViewController.view.snp.makeConstraints { make in
make.width.equalTo(layout.detailWidth)
make.top.bottom.equalTo(masterBarViewController.view)
make.leading.equalTo(masterBarViewController.view.snp.trailing).offset(layout.divider)
}
addChild(transitioningNavigationController)
view.addSubview(transitioningNavigationController.view)
transitioningNavigationController.didMove(toParent: self)
updateTraitCollectionOverrides()
layoutConstraints.activate(layout.mode)
}
override func viewWillTransition(
to size: CGSize,
with coordinator: UIViewControllerTransitionCoordinator
) {
transitionType.update(
for: .willTransition(to: size),
and: masterContent.count
)
transition(
layout: layout,
type: transitionType,
source: .viewWillTransition,
with: coordinator
)
super.viewWillTransition(to: size, with: coordinator)
}
private func updateTraitCollectionOverrides() {
let horizontallyCompact = UITraitCollection(horizontalSizeClass: .compact)
let horizontallyRegular = UITraitCollection(horizontalSizeClass: .regular)
if UIDevice.current.userInterfaceIdiom == .pad {
// Overriding the master content ensures it can still use an appropriate layout.
actionAreaContent.setOverrideTraitCollection(
horizontallyRegular, forChild: masterBarViewController
)
// Detail content in the expanded layout should always be horizontally compact.
actionAreaContent.setOverrideTraitCollection(
horizontallyCompact, forChild: detailBarViewController
)
} else {
// On iPhone, we use a horizontally compact layout to prevent expansion on plus/max devices.
actionAreaContent
.setOverrideTraitCollection(horizontallyCompact, forChild: masterBarViewController)
}
}
private func updateBarButtonItems(with coordinator: UIViewControllerTransitionCoordinator?) {
let newActionItem = currentActionItem()
if isExpanded == false {
masterBarViewController
.transition(.update(with: newActionItem, isEnabled: actionsAreEnabled), with: coordinator)
} else {
detailBarViewController
.transition(.update(with: newActionItem, isEnabled: actionsAreEnabled), with: coordinator)
}
}
// The current `ActionItem` for the appropriate content.
private func currentActionItem() -> ActionItem {
if let detail = presentedDetailViewController ?? modalDetailViewController {
// Use the presented detail content if it exists.
return detail.currentActionItem(for: state, delegatingTo: self)
} else if let master = presentedMasterViewController {
// Otherwise the top-most master content.
return master.currentActionItem(for: state, delegatingTo: self)
} else {
return .empty
}
}
func barButtonItemWillExecuteAction(_ item: ActionArea.BarButtonItem) {
initiateLocalTransition()
}
func barButtonItemDidExecuteAction(_ item: ActionArea.BarButtonItem) {
toggleState()
}
private func toggleState() {
state = state == .normal ? .modal : .normal
}
// MARK: - API
/// Present content in a master context.
///
/// - Parameters:
/// - vc: The view controller to show.
/// - sender: The object calling this method.
override func show(_ vc: UIViewController, sender: Any?) {
guard state == .normal else {
fatalError("View controllers cannot be pushed during a modal detail presentation.")
}
// TODO: Consider integrating this validation logic into the transition types.
guard vc is ActionArea.MasterContent, masterContent.isEmpty, case .landscape = layout else {
navController.pushViewController(vc, animated: true)
return
}
navController.pushViewController(vc, animated: false)
}
/// Present content in a detail context.
///
/// - Parameters:
/// - vc: The view controller to show.
/// - sender: The object calling this method.
override func showDetailViewController(_ vc: UIViewController, sender: Any?) {
// Wrap the detail content in the default container if it isn't already. This ensures we can
// enforce behaviors like swapping the close button with a hide button in the `modal` state.
// It also lets us make `presentedDetailViewController` be `DetailContent`, which avoids
// casting later.
let detail: ActionArea.DetailContent
if let vc = vc as? ActionArea.DetailContent {
detail = vc
} else {
detail = ActionArea.DetailContentContainerViewController(
content: vc,
mode: .stateless(actionItem: .empty)
)
}
if isExpanded == false {
navController.pushViewController(detail, animated: true)
} else {
detailNavController.pushViewController(detail, animated: true)
}
}
/// Re-present detail content that was dismissed in the `modal` state.
///
/// Calling this method when no detail content was presented or the Action Area is not in the
/// `modal` state is an error.
func reshowDetail() {
guard let modalDetailViewController = modalDetailViewController else {
fatalError("A detailViewController is not currently being shown.")
}
guard state == .modal else {
fatalError("The Action Area can only reshow in the modal state.")
}
if isExpanded == false {
showDetailViewController(modalDetailViewController, sender: self)
}
// Otherwise the detailViewController is already on screen.
}
func revealMaster() {
guard let firstDetail = detailContent.first else {
preconditionFailure("A detailViewController is not currently being shown.")
}
guard isExpanded == false else { return }
guard let index = navController.viewControllers.firstIndex(of: firstDetail) else {
preconditionFailure("Expected detail content in navController.")
}
precondition(index > navController.viewControllers.startIndex,
"Expected master content under detail content.")
let before = navController.viewControllers.index(before: index)
guard let newTop = navController.viewControllers[before] as? MasterContent else {
preconditionFailure("Expected new top view controller to be master content.")
}
navController.popToViewController(newTop, animated: true)
}
}
}
// MARK: - ActionItem
private extension ActionArea.Content {
func currentActionItem(
for state: ActionArea.Controller.State,
delegatingTo delegate: ActionAreaBarButtonItemDelegate
) -> ActionArea.ActionItem {
switch (state, mode) {
case let (_, .stateless(actionItem)),
let (.normal, .stateful(actionItem, _)),
let (.modal, .stateful(_, actionItem)):
actionItem.primary?.delegate = delegate
return actionItem
}
}
}
// MARK: - Transitions
private extension ActionArea.Controller {
/// The master transition type.
private enum MasterTransitionType {
/// Entering the Action Area from external content.
case enter
/// Transitioning between master content within the Action Area.
case `internal`
/// Leaving the Action Area to external content.
case leave
/// Transitions outside of the Action Area that are using its master navigation controller.
case external
/// Layout transitions related to size changes or rotation.
case size(Layout)
/// The next transition type.
///
/// This method is called during the various transition phases to determine the next transition
/// type. The transition type will not always change.
///
/// - Parameters:
/// - phase: The current transition phase.
/// - contentCount: The number of master content view controllers.
mutating func update(
for phase: MasterTransitionPhase,
and contentCount: Int
) {
switch (self, phase) {
case (.external, .willShow) where contentCount > 0:
self = .enter
case (.external, .willShow) where contentCount == 0:
self = .external
case (.enter, .didShow):
self = .internal
case (.internal, .willShow) where contentCount > 0:
self = .internal
case (.internal, .back) where contentCount == 1:
self = .leave
case (.leave, .didShow):
self = .external
case let (_, .willTransition(to: newSize)):
self = .size(Layout(newSize, and: contentCount))
case (.size, .didTransition):
self = contentCount > 0 ? .internal : .external
default:
break
}
}
}
/// The master transition phase.
///
/// These values represent where transition-related operations take place, and they are used
/// as part of the input to determine when to change the `MasterTransitionType`.
private enum MasterTransitionPhase {
case back
case willShow
case didShow
case willTransition(to: CGSize)
case didTransition
}
/// The master transition source.
///
/// Transitions can be initiated from either a user action or a navgiation controller transition.
private enum TransitionSource {
/// Either a back-button tap or swipe gesture.
case backAction
/// A `UINavigationControllerDelegate` call.
case delegate
/// A `viewWillTransition(to:with:)` call.
case viewWillTransition
}
/// Coordinate a master content transition.
///
/// - Parameters:
/// - layout: The layout for this transition.
/// - type: The type of this transition.
/// - source: The source of the current phase of the transition.
/// - coordinator: The coordinator of the transition.
private func transition(
layout: Layout,
type: MasterTransitionType,
source: TransitionSource,
with coordinator: UIViewControllerTransitionCoordinator?
) {
switch (layout, type, source) {
case (_, .external, _):
// There is nothing to do here for external transitions.
break
case let (_, .size(newLayout), _):
transition(from: layout, to: newLayout, source: source, with: coordinator)
case (.portrait, _, _):
transition(portrait: layout, type: type, source: source, with: coordinator)
case (.landscape, _, _):
transition(landscape: layout, type: type, source: source, with: coordinator)
}
}
private func transition(
portrait: Layout,
type: MasterTransitionType,
source: TransitionSource,
with coordinator: UIViewControllerTransitionCoordinator?
) {
guard case .portrait = portrait else {
preconditionFailure("This method only handle portrait transitions.")
}
switch (type, source) {
case (.enter, .backAction):
preconditionFailure("The Action Area cannot be entered through a back action.")
case (.enter, .delegate):
masterBarViewController.transition(
.raise(with: currentActionItem(), isEnabled: actionsAreEnabled),
with: coordinator
)
case (.internal, .backAction):
sendOverriddenMasterBackButtonAction()
case (.internal, .delegate):
masterBarViewController.transition(
.update(with: currentActionItem(), isEnabled: actionsAreEnabled),
with: coordinator
)
case (.leave, .backAction):
sendOverriddenMasterBackButtonAction()
case (.leave, .delegate):
masterBarViewController.transition(.lower, with: coordinator)
case (.external, _), (.size, _), (_, .viewWillTransition):
preconditionFailure("This method only handle portrait transitions.")
}
}
private func transition(
landscape: Layout,
type: MasterTransitionType,
source: TransitionSource,
with coordinator: UIViewControllerTransitionCoordinator?
) {
guard case .landscape = landscape else {
preconditionFailure("This method only handle landscape transitions.")
}
switch (type, source) {
case (.enter, .backAction):
preconditionFailure("The Action Area cannot be entered through a back action.")
case (.enter, .delegate):
guard let presentedMasterViewController = presentedMasterViewController else {
preconditionFailure("Expected a presentedMasterViewController.")
}
// Snapshot current content.
let fromSnapshot = UIViewController()
fromSnapshot.view = actionAreaContent.view.snapshotView(afterScreenUpdates: false)!
// Cover current content.
transitioningNavigationController.setViewControllers([fromSnapshot], animated: false)
transitioningNavigationController.view.isHidden = false
// Update layout.
let newLayout = layout.expand()
layoutConstraints.activate(newLayout.mode)
layout = newLayout
// Prepare new content under snapshot.
actionEnabler = presentedMasterViewController.actionEnabler
detailNavController
.pushViewController(presentedMasterViewController.emptyState, animated: false)
detailBarViewController.transition(
.raise(with: currentActionItem(), isEnabled: actionsAreEnabled),
with: coordinator
)
view.layoutIfNeeded()
// Snapshot new content.
let toSnapshot = UIViewController()
toSnapshot.view = actionAreaContent.view.snapshotView(afterScreenUpdates: true)!
// Transition content snapshots.
transitioningNavigationController.pushViewController(toSnapshot, animated: true)
transitioningNavigationController.transitionCoordinator?.animate(
alongsideTransition: nil,
completion: { _ in
// Clean up snapshots.
self.transitioningNavigationController.view.isHidden = true
self.transitioningNavigationController.setViewControllers([], animated: false)
}
)
case (.internal, .backAction):
sendOverriddenMasterBackButtonAction()
if let master = masterContent.last {
detailNavController.popToViewController(master.emptyState, animated: true)
}
case (.internal, .delegate):
actionEnabler = presentedMasterViewController?.actionEnabler
detailNavController.setViewControllers(emptyStates, animated: true)
case (.leave, .backAction):
// Snapshot current content.
let fromSnapshot = UIViewController()
fromSnapshot.view = actionAreaContent.view.snapshotView(afterScreenUpdates: false)!
// Cover current content.
transitioningNavigationController.setViewControllers([fromSnapshot], animated: false)
transitioningNavigationController.view.isHidden = false
// Update layout.
let newLayout = layout.collapse()
layoutConstraints.activate(newLayout.mode)
layout = newLayout
// Prepare new content under snapshot.
navController.popViewController(animated: false)
view.layoutIfNeeded()
// Snapshot new content.
let toSnapshot = UIViewController()
toSnapshot.view = actionAreaContent.view.snapshotView(afterScreenUpdates: true)!
// Prepare transition controller.
transitioningNavigationController
.setViewControllers([toSnapshot, fromSnapshot], animated: false)
// Transition content snapshots.
transitioningNavigationController.popViewController(animated: true)
transitioningNavigationController.transitionCoordinator?.animate(
alongsideTransition: nil,
completion: { _ in
// Clean up snapshots and detail content.
self.transitioningNavigationController.view.isHidden = true
self.transitioningNavigationController.setViewControllers([], animated: false)
self.detailNavController.setViewControllers([], animated: false)
}
)
case (.leave, .delegate):
coordinator?.animate(alongsideTransition: nil, completion: { _ in
self.presentedMasterViewController?.view.layoutMargins = .zero
})
case (.external, _), (.size, _), (_, .viewWillTransition):
preconditionFailure("This method only handle landscape transitions.")
}
}
private func transition(
from oldLayout: Layout,
to newLayout: Layout,
source: TransitionSource,
with coordinator: UIViewControllerTransitionCoordinator?
) {
func createCurrentSizeConstraint() -> Constraint? {
var sizeConstraint: Constraint?
masterBarViewController.view.snp.makeConstraints { make in
sizeConstraint = make.size.equalTo(masterBarViewController.view.bounds.size).constraint
}
return sizeConstraint
}
switch (oldLayout, newLayout, source) {
case let (oldLayout, newLayout, .viewWillTransition)
where oldLayout.isCollapsed && newLayout.isExpanded: // Expand.
guard let topMasterContent = masterContent.last else {
preconditionFailure("The layout cannot be expanded without any master content.")
}
// Prepare new content.
var detailNavViewControllers: [UIViewController] = emptyStates
switch state {
case .normal:
// normal state:
// - [master]
// - [master, detail]
detailNavViewControllers.append(contentsOf: detailContent)
case .modal:
// TODO: Consider storing the `modalDetailViewController` in the state itself.
guard let modalDetailViewController = modalDetailViewController else {
preconditionFailure("A modalDetailViewController must be set in the modal state.")
}
// TODO: Handle the case where the modal detail is interleaved within the detail content.
// Currently detail content that can enter the modal state is only shown directly
// between master and other detail content, but nothing prevents that possibility.
//
// modal state:
// - [master]
// - [master, modal]
// - [master, detail] (modal was hidden)
// - [master, modal, detail]
if detailContent.contains(where: { $0 === modalDetailViewController }) == false {
detailNavViewControllers.append(modalDetailViewController)
}
detailNavViewControllers.append(contentsOf: detailContent)
}
// Update layout.
self.layout = newLayout
layoutConstraints.deactivateAll()
let sizeConstraint = createCurrentSizeConstraint()
view.layoutIfNeeded()
// Snapshot existing content.
var navControllerSnapshot: UIView?
if navController.topViewController != topMasterContent {
navControllerSnapshot =
navController.topViewController?.view.snapshotView(afterScreenUpdates: false)
navControllerSnapshot.map { navController.view.addSubview($0) }
}
detailNavController.setViewControllers(emptyStates, animated: false)
var detailNavControllerSnapshot: UIView?
if detailNavController.topViewController != detailNavViewControllers.last {
detailNavControllerSnapshot =
detailNavController.topViewController?.view.snapshotView(afterScreenUpdates: false)
detailNavControllerSnapshot.map { detailNavController.view.addSubview($0) }
}
// Replace content.
navController.popToViewController(topMasterContent, animated: false)
detailNavController.setViewControllers(detailNavViewControllers, animated: false)
coordinator?.animate(alongsideTransition: { _ in
sizeConstraint?.deactivate()
self.layoutConstraints.activate(newLayout.mode)
self.view.layoutIfNeeded()
self.masterBarViewController
.transition(.update(with: .empty, isEnabled: self.actionsAreEnabled), with: coordinator)
self.detailBarViewController.transition(
.update(with: self.currentActionItem(), isEnabled: self.actionsAreEnabled),
with: coordinator
)
navControllerSnapshot?.alpha = 0
detailNavControllerSnapshot?.alpha = 0
}) { _ in
// TODO: Rotating in the middle of a transition results in the final frame value from the
// transition being set after the rotation animations complete, which leaves the frame with
// the wrong size. This fixes the problem, but it seems like there should be some way to
// serialize the animations or handle this more gracefully.
self.navController.topViewController?.view.frame = self.navController.view.frame
navControllerSnapshot?.removeFromSuperview()
detailNavControllerSnapshot?.removeFromSuperview()
self.transitionType.update(for: .didTransition, and: self.masterContent.count)
}
case let (oldLayout, newLayout, .viewWillTransition)
where oldLayout.isExpanded && newLayout.isCollapsed: // Collapse.
guard let topEmptyState = emptyStates.last else {
preconditionFailure("The layout cannot be collapsed without any empty states.")
}
// Prepare new content.
var navViewControllers = navController.viewControllers
navViewControllers.append(contentsOf: detailContent)
// Update layout.
self.layout = newLayout
layoutConstraints.deactivateAll()
let sizeConstraint = createCurrentSizeConstraint()
view.layoutIfNeeded()
// Snapshot existing content.
var navControllerSnapshot: UIView?
if navController.topViewController != navViewControllers.last {
navControllerSnapshot =
navController.topViewController?.view.snapshotView(afterScreenUpdates: false)
navControllerSnapshot.map { navController.view.addSubview($0) }
}
var detailNavControllerSnapshot: UIView?
if detailNavController.topViewController != topEmptyState {
detailNavControllerSnapshot =
detailNavController.topViewController?.view.snapshotView(afterScreenUpdates: false)
detailNavControllerSnapshot.map { detailNavController.view.addSubview($0) }
}
// Replace content.
detailNavController.popToViewController(topEmptyState, animated: false)
navController.setViewControllers(navViewControllers, animated: false)
coordinator?.animate(alongsideTransition: { _ in
sizeConstraint?.deactivate()
self.layoutConstraints.activate(newLayout.mode)
self.view.layoutIfNeeded()
self.masterBarViewController.transition(
.update(with: self.currentActionItem(), isEnabled: self.actionsAreEnabled),
with: coordinator
)
self.detailBarViewController
.transition(.update(with: .empty, isEnabled: self.actionsAreEnabled), with: coordinator)
navControllerSnapshot?.alpha = 0
detailNavControllerSnapshot?.alpha = 0
}) { _ in
self.detailNavController.setViewControllers([], animated: false)
navControllerSnapshot?.removeFromSuperview()
detailNavControllerSnapshot?.removeFromSuperview()
self.transitionType.update(for: .didTransition, and: self.masterContent.count)
}
case let (_, newLayout, .viewWillTransition):
self.layout = newLayout
self.transitionType.update(for: .didTransition, and: self.masterContent.count)
case (_, _, .backAction):
preconditionFailure("Back actions should never initiate a layout transition.")
case (_, _, .delegate):
// There is nothing to do here for delegate calls during a transitions.
break
}
}
var actionsAreEnabled: Bool {
return presentedMasterViewController?.actionEnabler?.isEnabled ?? true
}
func animateActionEnablement(actionsAreEnabled: Bool) {
guard let master = presentedMasterViewController else { return }
let coordinator = initiateLocalTransition()
if isExpanded {
detailBarViewController.transition(.enable(actionsAreEnabled), with: coordinator)
master.emptyState.isEnabled = actionsAreEnabled
} else {
masterBarViewController.transition(.enable(actionsAreEnabled), with: coordinator)
}
}
var barIsElevated: Bool {
let defaultBarIsElevated = false
if isExpanded {
return presentedDetailViewController?.barElevator?.isEnabled ?? defaultBarIsElevated
} else {
return presentedMasterViewController?.barElevator?.isEnabled ?? defaultBarIsElevated
}
}
func animateBarElevation(barIsElevated: Bool) {
guard presentedMasterViewController != nil else { return }
func animate() {
if isExpanded {
detailBarViewController.barIsElevated = barIsElevated
} else {
masterBarViewController.barIsElevated = barIsElevated
}
}
UIView.animate(withDuration: Metrics.defaultAnimationDuration) {
animate()
}
}