Skip to content

Open source CapCut alternative.

TypeScript

fix(ios): the capture preview outlives tab switches

David Lec469a29a9a35
diff --git a/apps/ios/Donkey/Controllers/CameraController.swift b/apps/ios/Donkey/Controllers/CameraController.swift
index e86a0bf4..f8050c4d 100644
--- a/apps/ios/Donkey/Controllers/CameraController.swift
+++ b/apps/ios/Donkey/Controllers/CameraController.swift
@@ -6,6 +6,9 @@ import UIKit
/// engine, and routing engine events into CameraModel state.
final class CameraController: CameraControlling {
let session: AVCaptureSession
+ /// One preview view for the app's lifetime; its layer attaches to the
+ /// session here, while the session is idle and the lock is free.
+ let previewView = CameraPreviewView.PreviewHostView()
weak var model: CameraModel?
var onRecordingFinished: ((URL, TimeInterval, Data?) -> Void)?
@@ -13,6 +16,8 @@ final class CameraController: CameraControlling {
init() {
session = engine.session
+ previewView.previewLayer.session = session
+ previewView.previewLayer.videoGravity = .resizeAspectFill
engine.events = { [weak self] event in
Task { @MainActor in self?.handle(event) }
}
diff --git a/apps/ios/Donkey/Support/CameraPreviewView.swift b/apps/ios/Donkey/Support/CameraPreviewView.swift
index 41d3646c..d61f0451 100644
--- a/apps/ios/Donkey/Support/CameraPreviewView.swift
+++ b/apps/ios/Donkey/Support/CameraPreviewView.swift
@@ -4,28 +4,39 @@ import DonkeyKitModels
import SwiftUI
import UIKit
-/// SwiftUI has no native capture preview; this hosts an
-/// AVCaptureVideoPreviewLayer and attaches the hardware capture events
+/// SwiftUI has no native capture preview; this hosts the controller's
+/// persistent preview view and attaches the hardware capture events
/// (volume buttons, Camera Control) to the record action.
+///
+/// The preview layer attaches to the session once, at controller init while
+/// the session is idle. Attaching or detaching later runs on the main thread
+/// and takes the session's lock, which stalls the UI for as long as the
+/// capture queue is busy starting or stopping — so the host view lives for
+/// the app's lifetime and tab switches only re-parent it.
struct CameraPreviewView: UIViewRepresentable {
- let session: AVCaptureSession
+ let host: PreviewHostView
let onCaptureEvent: () -> Void
- func makeUIView(context: Context) -> PreviewHostView {
- let view = PreviewHostView()
- view.previewLayer.session = session
- view.previewLayer.videoGravity = .resizeAspectFill
+ func makeUIView(context: Context) -> UIView {
+ let container = UIView()
+ host.frame = container.bounds
+ host.autoresizingMask = [.flexibleWidth, .flexibleHeight]
+ container.addSubview(host)
let interaction = AVCaptureEventInteraction { event in
guard event.phase == .ended else { return }
context.coordinator.onCaptureEvent()
}
- view.addInteraction(interaction)
- return view
+ container.addInteraction(interaction)
+ return container
}
- func updateUIView(_ uiView: PreviewHostView, context: Context) {
+ func updateUIView(_ uiView: UIView, context: Context) {
context.coordinator.onCaptureEvent = onCaptureEvent
+ if host.superview !== uiView {
+ host.frame = uiView.bounds
+ uiView.addSubview(host)
+ }
}
func makeCoordinator() -> Coordinator {