-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathWorkflowHostingController.swift
188 lines (143 loc) · 6.24 KB
/
WorkflowHostingController.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
/*
* Copyright 2020 Square Inc.
*
* 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.
*/
#if canImport(UIKit)
import ReactiveSwift
import UIKit
@_spi(ViewEnvironmentWiring) import ViewEnvironmentUI
import Workflow
/// Drives view controllers from a root Workflow.
public final class WorkflowHostingController<ScreenType, Output>: WorkflowUIViewController where ScreenType: Screen {
public typealias CustomizeEnvironment = (inout ViewEnvironment) -> Void
/// Emits output events from the bound workflow.
public var output: Signal<Output, Never> {
return workflowHost.output
}
/// An environment customization that will be applied to the environment of the root screen.
public var customizeEnvironment: CustomizeEnvironment {
didSet { setNeedsEnvironmentUpdate() }
}
private(set) var rootViewController: UIViewController
private let workflowHost: WorkflowHost<AnyWorkflow<ScreenType, Output>>
private let (lifetime, token) = Lifetime.make()
public init<W: AnyWorkflowConvertible>(
workflow: W,
customizeEnvironment: @escaping CustomizeEnvironment = { _ in },
observers: [WorkflowObserver] = []
) where W.Rendering == ScreenType, W.Output == Output {
self.workflowHost = WorkflowHost(
workflow: workflow.asAnyWorkflow(),
observers: observers
)
self.customizeEnvironment = customizeEnvironment
var customizedEnvironment: ViewEnvironment = .empty
customizeEnvironment(&customizedEnvironment)
self.rootViewController = workflowHost
.rendering
.value
.viewControllerDescription(environment: customizedEnvironment)
.buildViewController()
super.init(nibName: nil, bundle: nil)
// Do not automatically forward environment did change notifications to the rendered screen's backing view
// controller. Instead rely on `ViewControllerDescription` to call `setNeedsEnvironmentUpdate()` when updates
// occur.
environmentDescendantsOverride = { [] }
addChild(rootViewController)
rootViewController.didMove(toParent: self)
workflowHost
.rendering
.signal
.take(during: lifetime)
.observeValues { [weak self] screen in
guard let self = self else { return }
self.update(screen: screen, environment: self.environment)
}
}
/// Updates the root Workflow in this container.
public func update<W: AnyWorkflowConvertible>(workflow: W) where W.Rendering == ScreenType, W.Output == Output {
workflowHost.update(workflow: workflow.asAnyWorkflow())
}
public required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func update(screen: ScreenType, environment: ViewEnvironment) {
let previousRoot = rootViewController
update(child: \.rootViewController, with: screen, in: environment)
if previousRoot !== rootViewController {
// If a new view controller was instantiated and added as a child we need to inform it that the environment
// should be re-requested in order to respond to customizations in this WorkflowHostingController or any
// view controller above it in the UIViewController hierarchy.
setNeedsEnvironmentUpdate()
}
updatePreferredContentSizeIfNeeded()
}
override public func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .clear
rootViewController.view.frame = view.bounds
view.addSubview(rootViewController.view)
updatePreferredContentSizeIfNeeded()
}
override public func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
applyEnvironmentIfNeeded()
}
override public func viewDidLayoutSubviews() {
defer { super.viewDidLayoutSubviews() }
rootViewController.view.frame = view.bounds
}
override public var childForStatusBarStyle: UIViewController? {
return rootViewController
}
override public var childForStatusBarHidden: UIViewController? {
return rootViewController
}
override public var childForHomeIndicatorAutoHidden: UIViewController? {
return rootViewController
}
override public var childForScreenEdgesDeferringSystemGestures: UIViewController? {
return rootViewController
}
override public var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return rootViewController.supportedInterfaceOrientations
}
override public var preferredStatusBarUpdateAnimation: UIStatusBarAnimation {
return rootViewController.preferredStatusBarUpdateAnimation
}
override public var childViewControllerForPointerLock: UIViewController? {
return rootViewController
}
override public func preferredContentSizeDidChange(
forChildContentContainer container: UIContentContainer
) {
super.preferredContentSizeDidChange(forChildContentContainer: container)
guard container === rootViewController else { return }
updatePreferredContentSizeIfNeeded()
}
private func updatePreferredContentSizeIfNeeded() {
let newPreferredContentSize = rootViewController.preferredContentSize
guard newPreferredContentSize != preferredContentSize else { return }
preferredContentSize = newPreferredContentSize
}
}
extension WorkflowHostingController: ViewEnvironmentObserving {
public func customize(environment: inout ViewEnvironment) {
customizeEnvironment(&environment)
}
public func environmentDidChange() {
update(screen: workflowHost.rendering.value, environment: environment)
}
}
#endif