1717#if canImport(UIKit)
1818
1919import 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