|
import UIKit |
|
import SwiftUI |
|
|
|
protocol HeavyUIViewDelegate: AnyObject { |
|
func heavyView(_ view: HeavyUIView, to thing: Int) |
|
func heavyView(_ view: HeavyUIView, titleForThing: String) -> String |
|
} |
|
|
|
class HeavyUIView: UIView { } |
|
|
|
struct SwiftUIWrapper: UIViewRepresentable { |
|
|
|
let wrappedView: HeavyUIView |
|
let proxyDelegate = ProxyDelegate() |
|
|
|
func makeUIView( |
|
context: UIViewRepresentableContext<SwiftUIWrapper> |
|
) -> some HeavyUIView { |
|
wrappedView |
|
} |
|
|
|
func updateUIView(_ uiView: UIViewType, context: Context) { } |
|
|
|
func makeCoordinator() -> Coordinator { |
|
Coordinator(self) |
|
} |
|
} |
|
|
|
// MARK: ViewModifiers |
|
extension SwiftUIWrapper { |
|
func heavyViewSomeThingHappened( |
|
_ implementation: @escaping ( |
|
_ view: HeavyUIView, |
|
_ thing: Int |
|
) -> Void |
|
) -> SwiftUIWrapper { |
|
proxyDelegate.heavyViewSomeThingHappened = implementation |
|
return self |
|
} |
|
|
|
func heavyViewTitleForThing( |
|
_ implementation: @escaping ( |
|
_ view: HeavyUIView, |
|
_ titleForThing: String |
|
) -> String |
|
) -> SwiftUIWrapper { |
|
proxyDelegate.heavyViewTitleForThing = implementation |
|
return self |
|
} |
|
} |
|
|
|
// MARK: Coordinator |
|
extension SwiftUIWrapper { |
|
class Coordinator: HeavyUIViewDelegate { |
|
let control: SwiftUIWrapper |
|
|
|
init(_ control: SwiftUIWrapper) { |
|
self.control = control |
|
} |
|
|
|
func heavyView(_ view: HeavyUIView, to thing: Int) { |
|
control.proxyDelegate.heavyViewSomeThingHappened(view, thing) |
|
} |
|
|
|
func heavyView(_ view: HeavyUIView, titleForThing: String) -> String { |
|
control.proxyDelegate.heavyViewTitleForThing(view, titleForThing) |
|
} |
|
} |
|
} |
|
|
|
// MARK: ProxyDelegate |
|
extension SwiftUIWrapper { |
|
class ProxyDelegate { |
|
var heavyViewSomeThingHappened: ( |
|
_ view: HeavyUIView, |
|
_ thing: Int |
|
) -> Void = { view, thing in |
|
// default implementation ... |
|
} |
|
|
|
var heavyViewTitleForThing: ( |
|
_ view: HeavyUIView, |
|
_ titleForThing: String |
|
) -> String = { view, titleForThing in |
|
// default implementation ... |
|
String(titleForThing.reversed()) |
|
} |
|
} |
|
} |
|
|
|
// MARK: Example Callsite |
|
SwiftUIWrapper(wrappedView: .init()) |
|
.heavyViewSomeThingHappened { view, thing in |
|
// consumer provided implementation |
|
} |
|
.heavyViewTitleForThing { view, titleForThing in |
|
"\(titleForThing.reversed()) - \(titleForThing)" |
|
} |