|
| 1 | +import SwiftCrossUI |
| 2 | +import UIKit |
| 3 | + |
| 4 | +public struct UIViewControllerRepresentableContext<Coordinator> { |
| 5 | + public let coordinator: Coordinator |
| 6 | + public internal(set) var environment: EnvironmentValues |
| 7 | +} |
| 8 | + |
| 9 | +public protocol UIViewControllerRepresentable: View |
| 10 | +where Content == Never { |
| 11 | + associatedtype UIViewControllerType: UIViewController |
| 12 | + associatedtype Coordinator = Void |
| 13 | + |
| 14 | + /// Create the initial UIViewController instance. |
| 15 | + func makeUIViewController(context: UIViewControllerRepresentableContext<Coordinator>) |
| 16 | + -> UIViewControllerType |
| 17 | + |
| 18 | + /// Update the view with new values. |
| 19 | + /// - Parameters: |
| 20 | + /// - uiViewController: The controller to update. |
| 21 | + /// - context: The context, including the coordinator and potentially new environment |
| 22 | + /// values. |
| 23 | + /// - Note: This may be called even when `context` has not changed. |
| 24 | + func updateUIViewController( |
| 25 | + _ uiViewController: UIViewControllerType, |
| 26 | + context: UIViewControllerRepresentableContext<Coordinator>) |
| 27 | + |
| 28 | + /// Make the coordinator for this controller. |
| 29 | + /// |
| 30 | + /// The coordinator is used when the controller needs to communicate changes to the rest of |
| 31 | + /// the view hierarchy (i.e. through bindings). |
| 32 | + func makeCoordinator() -> Coordinator |
| 33 | + |
| 34 | + /// Compute the view's size. |
| 35 | + /// - Parameters: |
| 36 | + /// - proposal: The proposed frame for the view to render in. |
| 37 | + /// - uiViewController: The controller being queried for its view's preferred size. |
| 38 | + /// - context: The context, including the coordinator and environment values. |
| 39 | + /// - Returns: Information about the view's size. The ``SwiftCrossUI/ViewSize/size`` |
| 40 | + /// property is what frame the view will actually be rendered with if the current layout |
| 41 | + /// pass is not a dry run, while the other properties are used to inform the layout engine |
| 42 | + /// how big or small the view can be. The ``SwiftCrossUI/ViewSize/idealSize`` property |
| 43 | + /// should not vary with the `proposal`, and should only depend on the view's contents. |
| 44 | + /// Pass `nil` for the maximum width/height if the view has no maximum size (and therefore |
| 45 | + /// may occupy the entire screen). |
| 46 | + /// |
| 47 | + /// The default implementation uses `uiViewController.view.intrinsicContentSize` and |
| 48 | + /// `uiViewController.view.systemLayoutSizeFitting(_:)` to determine the return value. |
| 49 | + func determineViewSize( |
| 50 | + for proposal: SIMD2<Int>, uiViewController: UIViewControllerType, |
| 51 | + context: UIViewControllerRepresentableContext<Coordinator> |
| 52 | + ) -> ViewSize |
| 53 | + |
| 54 | + /// Called to clean up the view when it's removed. |
| 55 | + /// - Parameters: |
| 56 | + /// - uiViewController: The controller being dismantled. |
| 57 | + /// - coordinator: The coordinator. |
| 58 | + /// |
| 59 | + /// The default implementation does nothing. |
| 60 | + static func dismantleUIViewController( |
| 61 | + _ uiViewController: UIViewControllerType, coordinator: Coordinator) |
| 62 | +} |
| 63 | + |
| 64 | +extension UIViewControllerRepresentable { |
| 65 | + public static func dismantleUIViewController( |
| 66 | + _: UIViewControllerType, coordinator _: Coordinator |
| 67 | + ) { |
| 68 | + // no-op |
| 69 | + } |
| 70 | + |
| 71 | + public func determineViewSize( |
| 72 | + for proposal: SIMD2<Int>, uiViewController: UIViewControllerType, |
| 73 | + context: UIViewControllerRepresentableContext<Coordinator> |
| 74 | + ) -> ViewSize { |
| 75 | + defaultViewSize(proposal: proposal, view: uiViewController.view) |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +extension View |
| 80 | +where Self: UIViewControllerRepresentable { |
| 81 | + public var body: Never { |
| 82 | + preconditionFailure("This should never be called") |
| 83 | + } |
| 84 | + |
| 85 | + public func children<Backend: AppBackend>( |
| 86 | + backend _: Backend, |
| 87 | + snapshots _: [ViewGraphSnapshotter.NodeSnapshot]?, |
| 88 | + environment _: EnvironmentValues |
| 89 | + ) -> any ViewGraphNodeChildren { |
| 90 | + EmptyViewChildren() |
| 91 | + } |
| 92 | + |
| 93 | + public func layoutableChildren<Backend: AppBackend>( |
| 94 | + backend _: Backend, |
| 95 | + children _: any ViewGraphNodeChildren |
| 96 | + ) -> [LayoutSystem.LayoutableChild] { |
| 97 | + [] |
| 98 | + } |
| 99 | + |
| 100 | + public func asWidget<Backend: AppBackend>( |
| 101 | + _: any ViewGraphNodeChildren, |
| 102 | + backend _: Backend |
| 103 | + ) -> Backend.Widget { |
| 104 | + if let widget = ControllerRepresentingWidget(representable: self) as? Backend.Widget { |
| 105 | + return widget |
| 106 | + } else { |
| 107 | + fatalError("UIViewControllerRepresentable requested by \(Backend.self)") |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public func update<Backend: AppBackend>( |
| 112 | + _ widget: Backend.Widget, |
| 113 | + children _: any ViewGraphNodeChildren, |
| 114 | + proposedSize: SIMD2<Int>, |
| 115 | + environment: EnvironmentValues, |
| 116 | + backend _: Backend, |
| 117 | + dryRun: Bool |
| 118 | + ) -> ViewUpdateResult { |
| 119 | + let representingWidget = widget as! ControllerRepresentingWidget<Self> |
| 120 | + representingWidget.update(with: environment) |
| 121 | + |
| 122 | + let size = |
| 123 | + representingWidget.representable.determineViewSize( |
| 124 | + for: proposedSize, |
| 125 | + uiViewController: representingWidget.subcontroller, |
| 126 | + context: representingWidget.context! |
| 127 | + ) |
| 128 | + |
| 129 | + if !dryRun { |
| 130 | + representingWidget.width = size.size.x |
| 131 | + representingWidget.height = size.size.y |
| 132 | + } |
| 133 | + |
| 134 | + return ViewUpdateResult.leafView(size: size) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +extension UIViewControllerRepresentable |
| 139 | +where Coordinator == Void { |
| 140 | + public func makeCoordinator() { |
| 141 | + return () |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +final class ControllerRepresentingWidget<Representable: UIViewControllerRepresentable>: |
| 146 | + BaseControllerWidget |
| 147 | +{ |
| 148 | + var representable: Representable |
| 149 | + var context: UIViewControllerRepresentableContext<Representable.Coordinator>? |
| 150 | + |
| 151 | + lazy var subcontroller: Representable.UIViewControllerType = |
| 152 | + { |
| 153 | + let subcontroller = representable.makeUIViewController(context: context!) |
| 154 | + |
| 155 | + view.addSubview(subcontroller.view) |
| 156 | + addChild(subcontroller) |
| 157 | + subcontroller.didMove(toParent: self) |
| 158 | + |
| 159 | + subcontroller.view.translatesAutoresizingMaskIntoConstraints = false |
| 160 | + NSLayoutConstraint.activate([ |
| 161 | + subcontroller.view.topAnchor.constraint(equalTo: view.topAnchor), |
| 162 | + subcontroller.view.leadingAnchor.constraint(equalTo: view.leadingAnchor), |
| 163 | + subcontroller.view.trailingAnchor.constraint(equalTo: view.trailingAnchor), |
| 164 | + subcontroller.view.bottomAnchor.constraint(equalTo: view.bottomAnchor), |
| 165 | + ]) |
| 166 | + |
| 167 | + return subcontroller |
| 168 | + }() |
| 169 | + |
| 170 | + func update(with environment: EnvironmentValues) { |
| 171 | + if context == nil { |
| 172 | + context = .init(coordinator: representable.makeCoordinator(), environment: environment) |
| 173 | + } else { |
| 174 | + context!.environment = environment |
| 175 | + representable.updateUIViewController(subcontroller, context: context!) |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + init(representable: Representable) { |
| 180 | + self.representable = representable |
| 181 | + super.init() |
| 182 | + } |
| 183 | + |
| 184 | + deinit { |
| 185 | + if let context { |
| 186 | + Representable.dismantleUIViewController(subcontroller, coordinator: context.coordinator) |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments