You can use a UIViewRepresentable together with a singleton, not sure if this is the best way to do so but it works for me :)
here's a quick example:
Model (here you setup your RoomCaptureView)
final class Model : ObservableObject, RoomCaptureViewDelegate {
static var shared = Model()
@Published var roomCaptureView : RoomCaptureView
var captureSessionConfig : RoomCaptureSession.Configuration
init() {
roomCaptureView = RoomCaptureView(frame: .zero)
captureSessionConfig = RoomCaptureSession.Configuration()
}
func startSession() {
roomCaptureView.captureSession.run(configuration: captureSessionConfig)
}
func stopSession() {
roomCaptureView.captureSession.stop()
}
func captureView(shouldPresent roomDataForProcessing: CapturedRoomData, error: Error?) -> Bool {
return true
}
// Here you will get the final post-processed results.
func captureView(didPresent processedResult: CapturedRoom, error: Error?) {
if let error = error {
print("Error: \(error)")
}
}
}
UIViewRepresentable
struct RoomCaptureRep: UIViewRepresentable {
func makeUIView(context: Context) -> RoomCaptureView {
return Model.shared.roomCaptureView
}
func updateUIView(_ uiView: RoomCaptureView, context: Context) {
}
}
Usage
struct CoolRoomPlanView : View {
@StateObject var model = Model.shared
var body : some View {
ZStack {
RoomCaptureRep()
Button {
model.startSession()
} label: {
Text( "Start")
}
}
}
}