-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCameraContainerView.swift
More file actions
256 lines (233 loc) · 9.98 KB
/
CameraContainerView.swift
File metadata and controls
256 lines (233 loc) · 9.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
//
// CameraContainerView.swift
// SnapSafe
//
// Created by Claude on 9/6/25.
//
import AVFoundation
import SwiftUI
import FactoryKit
import Logging
struct CameraContainerView: View {
@StateObject private var cameraModel = CameraViewModel()
@EnvironmentObject private var nav: AppNavigationState
// Local camera UI state
@State private var isShutterAnimating = false
@State private var deviceOrientation = UIDevice.current.orientation
@State private var showZoomSlider = false
@State private var isPinching = false
var body: some View {
ZStack {
CameraView(cameraModel: cameraModel, onPinchStarted: {
isPinching = true
withAnimation {
showZoomSlider = true
}
}, onPinchChanged: {
isPinching = true
}, onPinchEnded: {
isPinching = false
})
.edgesIgnoringSafeArea(.all)
// Shutter animation overlay
if isShutterAnimating {
Color.black
.opacity(0.8)
.edgesIgnoringSafeArea(.all)
.transition(.opacity)
}
// Camera controls overlay
VStack {
// Top control bar with flash toggle and camera switch
HStack {
// Camera switch button
Button(action: {
Task {
let newPosition: AVCaptureDevice.Position = (cameraModel.cameraPosition == .back) ? .front : .back
await cameraModel.switchCamera(to: newPosition)
}
}) {
Image(systemName: "arrow.triangle.2.circlepath.camera")
.font(.system(size: 20))
.foregroundColor(.white)
.padding(12)
.background(Color.black.opacity(0.6))
.clipShape(Circle())
}
.padding(.top, 16)
.padding(.leading, 16)
Spacer()
// Flash control button - disabled for front camera
Button(action: {
Logger.ui.info("Flash button tapped, current mode: \(cameraModel.flashMode)")
cameraModel.toggleFlashMode()
}) {
Image(systemName: cameraModel.flashIcon)
.font(.system(size: 20))
.foregroundColor(cameraModel.cameraPosition == .front ? .gray : .white)
.padding(12)
.background(Color.black.opacity(0.6))
.clipShape(Circle())
}
.disabled(cameraModel.cameraPosition == .front)
.buttonStyle(PlainButtonStyle())
.padding(.top, 16)
.padding(.trailing, 16)
}
Spacer()
// Zoom slider (full control)
if showZoomSlider {
ZoomSliderView(cameraModel: cameraModel, isVisible: $showZoomSlider, isPinching: isPinching)
.padding(.horizontal, 16)
.padding(.bottom, 10)
} else {
// Simple zoom level indicator
ZStack {
Capsule()
.fill(Color.black.opacity(0.6))
.frame(width: 80, height: 30)
Text(String(format: "%.1fx", cameraModel.zoomFactor))
.font(.system(size: 14, weight: .bold))
.foregroundColor(.white)
}
.opacity(cameraModel.zoomFactor != 1.0 ? 1.0 : 0.0)
.animation(.easeInOut, value: cameraModel.zoomFactor)
.padding(.bottom, 10)
.rotationEffect(Utils.getRotationAngle())
.animation(.easeInOut, value: deviceOrientation)
.gesture(
// Use exclusively to properly distinguish single vs double tap
TapGesture(count: 2)
.onEnded { _ in
Logger.camera.debug("Double tap detected on zoom indicator")
handleDoubleTabZoomIndicator()
}
.exclusively(before:
TapGesture(count: 1)
.onEnded { _ in
Logger.camera.debug("Single tap detected on zoom indicator")
withAnimation {
showZoomSlider = true
}
}
)
)
}
HStack {
Button(action: {
nav.navigate(to:.gallery)
}) {
ZStack {
Image(systemName: "photo.on.rectangle")
.font(.system(size: 24))
.foregroundColor(cameraModel.isSavingPhoto ? .gray : .white)
.padding()
.background(Color.black.opacity(0.6))
.clipShape(Circle())
if cameraModel.isSavingPhoto {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .white))
.scaleEffect(0.7)
}
}
}
.disabled(cameraModel.isSavingPhoto)
.padding()
Spacer()
// Capture button
Button(action: {
triggerShutterEffect()
cameraModel.capturePhoto()
}) {
ZStack {
// Background circle
Circle()
.strokeBorder(cameraModel.isPermissionGranted ? Color.white : Color.gray, lineWidth: 4)
.frame(width: 80, height: 80)
.background(
Circle()
.fill(cameraModel.isPermissionGranted ? Color.white : Color.gray.opacity(0.5))
)
// Overlay shutter icon
Image("snapshutter")
.resizable()
.scaledToFit()
.frame(width: 90, height: 90)
.foregroundColor(.black)
}
.padding()
}
.disabled(!cameraModel.isPermissionGranted)
Spacer()
Button(action: {
nav.navigate(to:.settings)
}) {
Image(systemName: "gear")
.font(.system(size: 24))
.foregroundColor(.white)
.padding()
.background(Color.black.opacity(0.6))
.clipShape(Circle())
}
.padding()
}
.padding(.bottom)
}
}
.animation(.easeInOut(duration: 0.1), value: isShutterAnimating)
.onAppear {
// Start monitoring orientation changes
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
NotificationCenter.default.addObserver(forName: UIDevice.orientationDidChangeNotification,
object: nil,
queue: .main) { _ in
self.deviceOrientation = UIDevice.current.orientation
}
// Initial camera setup - check permissions and configure camera
Task {
await cameraModel.checkAndSetupCamera()
}
}
.onDisappear {
// Stop monitoring orientation changes
NotificationCenter.default.removeObserver(self, name: UIDevice.orientationDidChangeNotification, object: nil)
UIDevice.current.endGeneratingDeviceOrientationNotifications()
}
}
// MARK: - Private Methods
private func triggerShutterEffect() {
isShutterAnimating = true
Task {
try await Task.sleep(for: .milliseconds(150))
await MainActor.run {
isShutterAnimating = false
}
}
}
private func handleDoubleTabZoomIndicator() {
cameraModel.resetZoomLevel()
let generator = UIImpactFeedbackGenerator(style: .medium)
generator.impactOccurred()
}
}
#Preview {
// Create a mock camera permission repository with granted permissions for preview
// @MainActor
// class MockCameraPermissionRepository: CameraPermissionRepository {
// override init() {
// super.init()
// // Force permission to be granted for preview
// Task {
// await self.checkAndUpdatePermissions()
// }
// }
//
// // Override to always return true for preview
// override var isPermissionGranted: Bool {
// return true
// }
// }
return CameraContainerView()
.environmentObject(AppNavigationState())
// .environmentObject(MockCameraPermissionRepository())
}