@@ -10,9 +10,9 @@ public protocol WidgetProtocol: UIResponder {
1010 var controller : UIViewController ? { get }
1111
1212 var childWidgets : [ any WidgetProtocol ] { get set }
13- var parentWidget : ( any WidgetProtocol ) ? { get }
13+ var parentWidget : ( any WidgetProtocol ) ? { get set }
1414
15- func add( toWidget other : some WidgetProtocol )
15+ func add( childWidget : some WidgetProtocol )
1616 func removeFromParentWidget( )
1717}
1818
@@ -136,13 +136,23 @@ class BaseViewWidget: UIView, WidgetProtocolHelpers {
136136 updateTopConstraint ( )
137137 }
138138
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 ( )
142142
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
146156 }
147157
148158 func removeFromParentWidget( ) {
@@ -208,19 +218,21 @@ class BaseControllerWidget: UIViewController, WidgetProtocolHelpers {
208218 fatalError ( " init(coder:) is not used for this view " )
209219 }
210220
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
214226
215- other . view. addSubview ( view)
227+ view. addSubview ( childWidget . view)
216228
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 )
220232 }
221233
222- parentWidget = other
223- other . childWidgets . append ( self )
234+ childWidgets . append ( childWidget )
235+ childWidget . parentWidget = self
224236 }
225237
226238 func removeFromParentWidget( ) {
@@ -270,6 +282,32 @@ class ContainerWidget: BaseControllerWidget {
270282 init ( child: some WidgetProtocol ) {
271283 self . child = child
272284 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 )
274312 }
275313}
0 commit comments