@@ -10,9 +10,9 @@ public protocol WidgetProtocol: UIResponder {
10
10
var controller : UIViewController ? { get }
11
11
12
12
var childWidgets : [ any WidgetProtocol ] { get set }
13
- var parentWidget : ( any WidgetProtocol ) ? { get }
13
+ var parentWidget : ( any WidgetProtocol ) ? { get set }
14
14
15
- func add( toWidget other : some WidgetProtocol )
15
+ func add( childWidget : some WidgetProtocol )
16
16
func removeFromParentWidget( )
17
17
}
18
18
@@ -136,13 +136,23 @@ class BaseViewWidget: UIView, WidgetProtocolHelpers {
136
136
updateTopConstraint ( )
137
137
}
138
138
139
- func add( toWidget other : some WidgetProtocol ) {
140
- if parentWidget === other { return }
141
- removeFromParentWidget ( )
139
+ func add( childWidget : some WidgetProtocol ) {
140
+ if childWidget . parentWidget === self { return }
141
+ childWidget . removeFromParentWidget ( )
142
142
143
- other. view. addSubview ( self )
144
- parentWidget = other
145
- other. childWidgets. append ( self )
143
+ let childController = childWidget. controller
144
+
145
+ addSubview ( childWidget. view)
146
+
147
+ if let controller,
148
+ let childController
149
+ {
150
+ controller. addChild ( childController)
151
+ childController. didMove ( toParent: controller)
152
+ }
153
+
154
+ childWidgets. append ( childWidget)
155
+ childWidget. parentWidget = self
146
156
}
147
157
148
158
func removeFromParentWidget( ) {
@@ -208,19 +218,21 @@ class BaseControllerWidget: UIViewController, WidgetProtocolHelpers {
208
218
fatalError ( " init(coder:) is not used for this view " )
209
219
}
210
220
211
- func add( toWidget other: some WidgetProtocol ) {
212
- if parentWidget === other { return }
213
- removeFromParentWidget ( )
221
+ func add( childWidget: some WidgetProtocol ) {
222
+ if childWidget. parentWidget === self { return }
223
+ childWidget. removeFromParentWidget ( )
224
+
225
+ let childController = childWidget. controller
214
226
215
- other . view. addSubview ( view)
227
+ view. addSubview ( childWidget . view)
216
228
217
- if let otherController = other . controller {
218
- otherController . addChild ( self )
219
- didMove ( toParent: otherController )
229
+ if let childController {
230
+ addChild ( childController )
231
+ childController . didMove ( toParent: self )
220
232
}
221
233
222
- parentWidget = other
223
- other . childWidgets . append ( self )
234
+ childWidgets . append ( childWidget )
235
+ childWidget . parentWidget = self
224
236
}
225
237
226
238
func removeFromParentWidget( ) {
@@ -270,6 +282,32 @@ class ContainerWidget: BaseControllerWidget {
270
282
init ( child: some WidgetProtocol ) {
271
283
self . child = child
272
284
super. init ( )
273
- child. add ( toWidget: self )
285
+ add ( childWidget: child)
286
+ }
287
+ }
288
+
289
+ class WrapperControllerWidget < Controller: UIViewController > : BaseControllerWidget {
290
+ let child : Controller
291
+
292
+ init ( child: Controller ) {
293
+ self . child = child
294
+ super. init ( )
295
+ }
296
+
297
+ override func loadView( ) {
298
+ super. loadView ( )
299
+
300
+ view. addSubview ( child. view)
301
+ addChild ( child)
302
+
303
+ child. view. translatesAutoresizingMaskIntoConstraints = false
304
+ NSLayoutConstraint . activate ( [
305
+ child. view. topAnchor. constraint ( equalTo: view. topAnchor) ,
306
+ child. view. leadingAnchor. constraint ( equalTo: view. leadingAnchor) ,
307
+ child. view. bottomAnchor. constraint ( equalTo: view. bottomAnchor) ,
308
+ child. view. trailingAnchor. constraint ( equalTo: view. trailingAnchor) ,
309
+ ] )
310
+
311
+ child. didMove ( toParent: self )
274
312
}
275
313
}
0 commit comments