67 lines
2.3 KiB
Swift
67 lines
2.3 KiB
Swift
|
import AVFoundation
|
||
|
import SwiftUI
|
||
|
|
||
|
struct CameraCellPreview: View {
|
||
|
@Environment(\.router) var router
|
||
|
@State private var cameraAuthorized = false
|
||
|
|
||
|
var body: some View {
|
||
|
Group {
|
||
|
if cameraAuthorized {
|
||
|
ZStack {
|
||
|
CameraView()
|
||
|
.aspectRatio(1, contentMode: .fit)
|
||
|
.frame(maxWidth: .infinity)
|
||
|
Image(systemName: "camera")
|
||
|
.resizable()
|
||
|
.aspectRatio(contentMode: .fit)
|
||
|
.frame(width: 40, height: 40)
|
||
|
.foregroundColor(.white)
|
||
|
.padding(8)
|
||
|
.background(Color.black.opacity(0.5))
|
||
|
.clipShape(Circle())
|
||
|
.padding(8)
|
||
|
}
|
||
|
.onTapGesture {
|
||
|
router.showScreen(.fullScreenCover) { _ in
|
||
|
CameraPicker()
|
||
|
.ignoresSafeArea(.all)
|
||
|
}
|
||
|
}
|
||
|
} else {
|
||
|
Button {
|
||
|
openAppSettings()
|
||
|
} label: {
|
||
|
ZStack {
|
||
|
Rectangle()
|
||
|
.fill(Color.Material.Background.light)
|
||
|
.overlay {
|
||
|
VStack {
|
||
|
Image(systemName: "camera")
|
||
|
.foregroundColor(.Material.Elements.active)
|
||
|
.font(.system(size: 30))
|
||
|
Text("Allow camera access")
|
||
|
.foregroundColor(.Material.Text.main)
|
||
|
.font(.body3)
|
||
|
}
|
||
|
}
|
||
|
.frame(height: 100)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
.task {
|
||
|
await checkAuthorization()
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private func checkAuthorization() async {
|
||
|
let status = AVCaptureDevice.authorizationStatus(for: .video)
|
||
|
var isAuthorized = status == .authorized
|
||
|
if status == .notDetermined {
|
||
|
isAuthorized = await AVCaptureDevice.requestAccess(for: .video)
|
||
|
}
|
||
|
cameraAuthorized = isAuthorized
|
||
|
}
|
||
|
}
|