Skip to content

Commit 7a8f319

Browse files
committed
Add bridging to ScreenViewController
1 parent 9dab8e5 commit 7a8f319

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

WorkflowUI/Sources/Screen/ScreenViewController.swift

+20-8
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#if canImport(UIKit)
1818

1919
import UIKit
20+
import ViewEnvironment
21+
@_spi(ViewEnvironmentWiring) import ViewEnvironmentUI
2022

2123
/// Generic base class that can be subclassed in order to to define a UI implementation that is powered by the
2224
/// given screen type.
@@ -39,31 +41,41 @@ open class ScreenViewController<ScreenType: Screen>: UIViewController {
3941
public private(set) final var screen: ScreenType
4042

4143
public final var screenType: Screen.Type {
42-
return ScreenType.self
44+
ScreenType.self
4345
}
4446

45-
public private(set) final var environment: ViewEnvironment
47+
private var _environment: ViewEnvironment
4648

4749
public required init(screen: ScreenType, environment: ViewEnvironment) {
4850
self.screen = screen
49-
self.environment = environment
51+
self._environment = environment
5052
super.init(nibName: nil, bundle: nil)
53+
54+
let ancestor = ViewEnvironmentPropagationNode(
55+
environmentDescendants: { [weak self] in
56+
[self].compactMap { $0 }
57+
},
58+
customizeEnvironment: { [weak self] environment in
59+
guard let self else { return }
60+
environment = self._environment
61+
}
62+
)
63+
64+
environmentAncestorOverride = { ancestor }
5165
}
5266

5367
@available(*, unavailable)
54-
public required init?(coder aDecoder: NSCoder) {
55-
fatalError("init(coder:) has not been implemented")
56-
}
68+
public required init?(coder aDecoder: NSCoder) { fatalError() }
5769

5870
public final func update(screen: ScreenType, environment: ViewEnvironment) {
5971
let previousScreen = self.screen
6072
self.screen = screen
6173
let previousEnvironment = self.environment
62-
self.environment = environment
74+
_environment = environment
75+
setNeedsEnvironmentUpdate()
6376
screenDidChange(from: previousScreen, previousEnvironment: previousEnvironment)
6477
}
6578

66-
/// Subclasses should override this method in order to update any relevant UI bits when the screen model changes.
6779
open func screenDidChange(from previousScreen: ScreenType, previousEnvironment: ViewEnvironment) {}
6880
}
6981

0 commit comments

Comments
 (0)