17
17
#if canImport(UIKit)
18
18
19
19
import UIKit
20
+ import ViewEnvironment
21
+ @_spi ( ViewEnvironmentWiring) import ViewEnvironmentUI
20
22
21
23
/// Generic base class that can be subclassed in order to to define a UI implementation that is powered by the
22
24
/// given screen type.
@@ -39,31 +41,41 @@ open class ScreenViewController<ScreenType: Screen>: UIViewController {
39
41
public private( set) final var screen : ScreenType
40
42
41
43
public final var screenType : Screen . Type {
42
- return ScreenType . self
44
+ ScreenType . self
43
45
}
44
46
45
- public private( set ) final var environment : ViewEnvironment
47
+ private var _environment : ViewEnvironment
46
48
47
49
public required init ( screen: ScreenType , environment: ViewEnvironment ) {
48
50
self . screen = screen
49
- self . environment = environment
51
+ self . _environment = environment
50
52
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 }
51
65
}
52
66
53
67
@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 ( ) }
57
69
58
70
public final func update( screen: ScreenType , environment: ViewEnvironment ) {
59
71
let previousScreen = self . screen
60
72
self . screen = screen
61
73
let previousEnvironment = self . environment
62
- self . environment = environment
74
+ _environment = environment
75
+ setNeedsEnvironmentUpdate ( )
63
76
screenDidChange ( from: previousScreen, previousEnvironment: previousEnvironment)
64
77
}
65
78
66
- /// Subclasses should override this method in order to update any relevant UI bits when the screen model changes.
67
79
open func screenDidChange( from previousScreen: ScreenType , previousEnvironment: ViewEnvironment ) { }
68
80
}
69
81
0 commit comments