|
21 | 21 | /// Displays the backing `ViewControllerDescription` for a given `Screen`.
|
22 | 22 | ///
|
23 | 23 | public final class DescribedViewController: UIViewController {
|
24 |
| - var currentViewController: UIViewController |
| 24 | + var content: UIViewController |
25 | 25 |
|
26 | 26 | public init(description: ViewControllerDescription) {
|
27 |
| - self.currentViewController = description.buildViewController() |
| 27 | + self.content = description.buildViewController() |
28 | 28 | super.init(nibName: nil, bundle: nil)
|
29 | 29 |
|
30 |
| - addChild(currentViewController) |
31 |
| - currentViewController.didMove(toParent: self) |
| 30 | + addChild(content) |
| 31 | + content.didMove(toParent: self) |
32 | 32 | }
|
33 | 33 |
|
34 | 34 | public convenience init<S: Screen>(screen: S, environment: ViewEnvironment) {
|
|
40 | 40 | fatalError("init(coder:) is unavailable")
|
41 | 41 | }
|
42 | 42 |
|
43 |
| - public func update(description: ViewControllerDescription) { |
44 |
| - if description.canUpdate(viewController: currentViewController) { |
45 |
| - description.update(viewController: currentViewController) |
| 43 | + public func update(description: ViewControllerDescription, animated: Bool = false) { |
| 44 | + if description.canUpdate(viewController: content) { |
| 45 | + description.update(viewController: content) |
46 | 46 | } else {
|
47 |
| - currentViewController.willMove(toParent: nil) |
48 |
| - currentViewController.viewIfLoaded?.removeFromSuperview() |
49 |
| - currentViewController.removeFromParent() |
| 47 | + let old = content |
| 48 | + let new = description.buildViewController() |
50 | 49 |
|
51 |
| - currentViewController = description.buildViewController() |
52 |
| - |
53 |
| - addChild(currentViewController) |
| 50 | + content = new |
54 | 51 |
|
55 | 52 | if isViewLoaded {
|
56 |
| - currentViewController.view.frame = view.bounds |
57 |
| - view.addSubview(currentViewController.view) |
58 |
| - updatePreferredContentSizeIfNeeded() |
| 53 | + let animated = animated && view.window != nil |
| 54 | + |
| 55 | + addChild(new) |
| 56 | + old.willMove(toParent: nil) |
| 57 | + |
| 58 | + description.transition.transition( |
| 59 | + from: old.view, |
| 60 | + to: new.view, |
| 61 | + in: view, |
| 62 | + animated: animated, |
| 63 | + setup: { |
| 64 | + self.view.addSubview(new.view) |
| 65 | + }, |
| 66 | + completion: { |
| 67 | + new.didMove(toParent: self) |
| 68 | + |
| 69 | + old.view.removeFromSuperview() |
| 70 | + old.removeFromParent() |
| 71 | + |
| 72 | + self.currentViewControllerChanged() |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + } else { |
| 77 | + addChild(new) |
| 78 | + new.didMove(toParent: self) |
| 79 | + |
| 80 | + old.willMove(toParent: nil) |
| 81 | + old.removeFromParent() |
59 | 82 | }
|
60 | 83 |
|
61 |
| - currentViewController.didMove(toParent: self) |
62 |
| - |
63 | 84 | updatePreferredContentSizeIfNeeded()
|
64 | 85 | }
|
65 | 86 | }
|
66 | 87 |
|
67 | 88 | public func update<S: Screen>(screen: S, environment: ViewEnvironment) {
|
68 |
| - if let screen = screen as? AnyContentContainerScreen { |
| 89 | + if let screen = screen as? AnyContentScreen { |
69 | 90 | update(description: screen.content.viewControllerDescription(environment: environment))
|
70 | 91 | } else {
|
71 | 92 | update(description: screen.viewControllerDescription(environment: environment))
|
|
75 | 96 | override public func viewDidLoad() {
|
76 | 97 | super.viewDidLoad()
|
77 | 98 |
|
78 |
| - currentViewController.view.frame = view.bounds |
79 |
| - view.addSubview(currentViewController.view) |
| 99 | + content.view.frame = view.bounds |
| 100 | + view.addSubview(content.view) |
80 | 101 |
|
81 | 102 | updatePreferredContentSizeIfNeeded()
|
82 | 103 | }
|
83 | 104 |
|
84 | 105 | override public func viewDidLayoutSubviews() {
|
85 | 106 | super.viewDidLayoutSubviews()
|
86 |
| - currentViewController.view.frame = view.bounds |
| 107 | + content.view.frame = view.bounds |
87 | 108 | }
|
88 | 109 |
|
89 | 110 | override public var childForStatusBarStyle: UIViewController? {
|
90 |
| - return currentViewController |
| 111 | + return content |
91 | 112 | }
|
92 | 113 |
|
93 | 114 | override public var childForStatusBarHidden: UIViewController? {
|
94 |
| - return currentViewController |
| 115 | + return content |
95 | 116 | }
|
96 | 117 |
|
97 | 118 | override public var childForHomeIndicatorAutoHidden: UIViewController? {
|
98 |
| - return currentViewController |
| 119 | + return content |
99 | 120 | }
|
100 | 121 |
|
101 | 122 | override public var childForScreenEdgesDeferringSystemGestures: UIViewController? {
|
102 |
| - return currentViewController |
| 123 | + return content |
103 | 124 | }
|
104 | 125 |
|
105 | 126 | override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
|
106 |
| - return currentViewController.supportedInterfaceOrientations |
| 127 | + return content.supportedInterfaceOrientations |
107 | 128 | }
|
108 | 129 |
|
109 | 130 | override public var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
|
110 |
| - return currentViewController.preferredStatusBarUpdateAnimation |
| 131 | + return content.preferredStatusBarUpdateAnimation |
111 | 132 | }
|
112 | 133 |
|
113 | 134 | @available(iOS 14.0, *)
|
114 | 135 | override public var childViewControllerForPointerLock: UIViewController? {
|
115 |
| - return currentViewController |
| 136 | + return content |
116 | 137 | }
|
117 | 138 |
|
118 | 139 | override public func preferredContentSizeDidChange(
|
119 | 140 | forChildContentContainer container: UIContentContainer
|
120 | 141 | ) {
|
121 | 142 | super.preferredContentSizeDidChange(forChildContentContainer: container)
|
122 | 143 |
|
123 |
| - guard container === currentViewController else { return } |
| 144 | + guard container === content else { return } |
124 | 145 |
|
125 | 146 | updatePreferredContentSizeIfNeeded()
|
126 | 147 | }
|
127 | 148 |
|
128 | 149 | private func updatePreferredContentSizeIfNeeded() {
|
129 |
| - let newPreferredContentSize = currentViewController.preferredContentSize |
| 150 | + let newPreferredContentSize = content.preferredContentSize |
130 | 151 |
|
131 | 152 | guard newPreferredContentSize != preferredContentSize else { return }
|
132 | 153 |
|
133 | 154 | preferredContentSize = newPreferredContentSize
|
134 | 155 | }
|
| 156 | + |
| 157 | + private func currentViewControllerChanged() { |
| 158 | + setNeedsFocusUpdate() |
| 159 | + setNeedsUpdateOfHomeIndicatorAutoHidden() |
| 160 | + |
| 161 | + if #available(iOS 14.0, *) { |
| 162 | + self.setNeedsUpdateOfPrefersPointerLocked() |
| 163 | + } |
| 164 | + |
| 165 | + setNeedsUpdateOfScreenEdgesDeferringSystemGestures() |
| 166 | + setNeedsStatusBarAppearanceUpdate() |
| 167 | + |
| 168 | + UIAccessibility.post(notification: .screenChanged, argument: nil) |
| 169 | + } |
135 | 170 | }
|
136 | 171 | #endif
|
0 commit comments