Fix camera position switch - detect change on settings sheet dismiss

- Track lastCameraPosition to detect changes
- Use sheet onDismiss callback to check if position changed
- Force camera recreation only when position actually changes
- Works around @Observable tracking issues with nested cloudSync data
This commit is contained in:
Matt Bruce 2026-01-04 15:02:29 -06:00
parent 19e17b7051
commit 55c4d7e05a

View File

@ -21,6 +21,9 @@ struct ContentView: View {
/// Incrementing this value creates a new camera session with fresh AVCapturePhotoOutput
@State private var cameraSessionKey = UUID()
/// Track camera position to detect changes when settings sheet closes
@State private var lastCameraPosition: CameraPosition = .front
var body: some View {
ZStack {
// Camera view - wrapped in EquatableView to prevent re-evaluation on settings changes
@ -70,6 +73,10 @@ struct ContentView: View {
.padding(.top, Design.Spacing.small)
}
.animation(.easeInOut(duration: Design.Animation.quick), value: showPhotoReview)
.onAppear {
// Initialize tracking of camera position
lastCameraPosition = settings.cameraPosition
}
.onChange(of: settings.isRingLightEnabled) { _, newValue in
if settings.isFlashSyncedWithRingLight {
settings.flashMode = newValue ? .on : .off
@ -80,11 +87,14 @@ struct ContentView: View {
settings.flashMode = settings.isRingLightEnabled ? .on : .off
}
}
.onChange(of: settings.cameraPosition) { _, _ in
// Force camera session recreation when camera position changes
cameraSessionKey = UUID()
}
.sheet(isPresented: $showSettings) {
.sheet(isPresented: $showSettings, onDismiss: {
// Check if camera position changed while settings was open
if settings.cameraPosition != lastCameraPosition {
lastCameraPosition = settings.cameraPosition
// Force camera session recreation with new position
cameraSessionKey = UUID()
}
}) {
SettingsView(viewModel: settings, showPaywall: $showPaywall)
}
.sheet(isPresented: $showPaywall) {