@@ -48,39 +48,81 @@ public struct Coordinator<WrappedValue: ViewCoordinator>: DynamicProperty, Prope
48
48
49
49
extension View {
50
50
#if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
51
- public func coordinator< C : UIViewControllerCoordinator < Route > , Route > (
52
- _ coordinator: C
51
+ public func coordinator< Route , Coordinator : UIViewControllerCoordinator < Route > > (
52
+ _ coordinator: Coordinator
53
53
) -> some View {
54
- environmentObject ( coordinator)
55
- . environmentObject ( AnyViewCoordinator ( coordinator) )
56
- . onAppKitOrUIKitViewControllerResolution {
57
- if coordinator. rootViewController == nil {
58
- coordinator. rootViewController = $0
59
- }
60
- }
61
-
54
+ modifier ( AttachUIViewControllerCoordinator ( coordinator: coordinator) )
62
55
}
63
56
64
- public func coordinator< C : UIWindowCoordinator < Route > , Route > (
65
- _ coordinator: C
57
+ public func coordinator< Route , Coordinator : UIWindowCoordinator < Route > > (
58
+ _ coordinator: Coordinator
66
59
) -> some View {
67
- environmentObject ( coordinator)
68
- . environmentObject ( AnyViewCoordinator ( coordinator) )
69
- . onAppKitOrUIKitViewControllerResolution {
70
- if coordinator. window == nil {
71
- coordinator. window = $0. view. window
72
- }
73
- }
60
+ modifier ( AttachUIWindowCoordinator ( coordinator: coordinator) )
74
61
}
75
62
76
- public func coordinator< C : ViewCoordinator > (
77
- _ coordinator: C
63
+ public func coordinator< Coordinator : ViewCoordinator > (
64
+ _ coordinator: Coordinator
78
65
) -> some View {
79
- environmentObject ( coordinator)
80
- . environmentObject ( ( coordinator as? AnyViewCoordinator < C . Route > ) ?? AnyViewCoordinator ( coordinator) )
81
- . onAppKitOrUIKitViewControllerResolution {
82
- AnyViewCoordinator ( coordinator) . _setViewController ( $0)
83
- }
66
+ modifier ( AttachViewCoordinator ( coordinator: coordinator) )
84
67
}
85
68
#endif
86
69
}
70
+
71
+ #if os(iOS) || os(tvOS) || targetEnvironment(macCatalyst)
72
+ private struct AttachUIViewControllerCoordinator < Route, Coordinator: UIViewControllerCoordinator < Route > > : ViewModifier {
73
+ @ObservedObject var coordinator : Coordinator
74
+
75
+ func body( content: Content ) -> some View {
76
+ PassthroughView {
77
+ if coordinator. rootViewController == nil {
78
+ ZeroSizeView ( )
79
+ } else {
80
+ content
81
+ }
82
+ }
83
+ . environmentObject ( coordinator)
84
+ . environmentObject ( AnyViewCoordinator ( coordinator) )
85
+ . onAppKitOrUIKitViewControllerResolution {
86
+ if coordinator. rootViewController == nil {
87
+ coordinator. rootViewController = $0
88
+ }
89
+ }
90
+ }
91
+ }
92
+
93
+ private struct AttachUIWindowCoordinator < Route, Coordinator: UIWindowCoordinator < Route > > : ViewModifier {
94
+ @ObservedObject var coordinator : Coordinator
95
+
96
+ func body( content: Content ) -> some View {
97
+ PassthroughView {
98
+ if coordinator. window == nil {
99
+ ZeroSizeView ( )
100
+ } else {
101
+ content
102
+ }
103
+ }
104
+ . environmentObject ( coordinator)
105
+ . environmentObject ( AnyViewCoordinator ( coordinator) )
106
+ . onAppKitOrUIKitViewControllerResolution {
107
+ if coordinator. window == nil {
108
+ coordinator. window = $0. view. window
109
+ }
110
+ }
111
+ }
112
+ }
113
+
114
+ private struct AttachViewCoordinator < Coordinator: ViewCoordinator > : ViewModifier {
115
+ @ObservedObject var coordinator : Coordinator
116
+
117
+ func body( content: Content ) -> some View {
118
+ PassthroughView {
119
+ content
120
+ }
121
+ . environmentObject ( coordinator)
122
+ . environmentObject ( ( coordinator as? AnyViewCoordinator < Coordinator . Route > ) ?? AnyViewCoordinator ( coordinator) )
123
+ . onAppKitOrUIKitViewControllerResolution {
124
+ AnyViewCoordinator ( coordinator) . _setViewController ( $0)
125
+ }
126
+ }
127
+ }
128
+ #endif
0 commit comments