579 lines
20 KiB
Swift
579 lines
20 KiB
Swift
import AVFoundation
|
|
import Combine
|
|
import SwiftUI
|
|
|
|
#if os(iOS)
|
|
import UIKit
|
|
#elseif os(macOS)
|
|
import AppKit
|
|
#endif
|
|
|
|
struct QRScannerSheet: View {
|
|
let title: String
|
|
let description: String
|
|
let navigationTitleText: String
|
|
let onCodeScanned: (String) -> Void
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
|
@State private var manualFallback = ""
|
|
|
|
init(
|
|
title: String = "Scan QR",
|
|
description: String = "Use the camera to scan an idp.global QR challenge.",
|
|
navigationTitle: String = "Scan QR",
|
|
onCodeScanned: @escaping (String) -> Void
|
|
) {
|
|
self.title = title
|
|
self.description = description
|
|
self.navigationTitleText = navigationTitle
|
|
self.onCodeScanned = onCodeScanned
|
|
}
|
|
|
|
var body: some View {
|
|
NavigationStack {
|
|
ZStack(alignment: .top) {
|
|
LiveQRScannerView { payload in
|
|
onCodeScanned(payload)
|
|
dismiss()
|
|
}
|
|
.ignoresSafeArea()
|
|
|
|
VStack(spacing: 12) {
|
|
IdPGlassCapsule {
|
|
VStack(alignment: .leading, spacing: 6) {
|
|
Text(title)
|
|
.font(.headline)
|
|
Text(description)
|
|
.font(.subheadline)
|
|
.foregroundStyle(.secondary)
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
}
|
|
|
|
Spacer()
|
|
|
|
ManualFallbackCard(
|
|
manualFallback: $manualFallback,
|
|
onUsePayload: {
|
|
let chosen = manualFallback.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
guard !chosen.isEmpty else { return }
|
|
onCodeScanned(chosen)
|
|
dismiss()
|
|
},
|
|
onPasteFromClipboard: {
|
|
manualFallback = ClipboardPayloadReader.readString()
|
|
}
|
|
)
|
|
}
|
|
.padding(16)
|
|
}
|
|
.navigationTitle(navigationTitleText)
|
|
.applyInlineNavigationTitleDisplayMode()
|
|
.toolbar {
|
|
ToolbarItem(placement: .cancellationAction) {
|
|
Button("Close") {
|
|
dismiss()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private extension View {
|
|
@ViewBuilder
|
|
func applyInlineNavigationTitleDisplayMode() -> some View {
|
|
#if os(macOS)
|
|
self
|
|
#else
|
|
navigationBarTitleDisplayMode(.inline)
|
|
#endif
|
|
}
|
|
}
|
|
|
|
struct LiveQRScannerView: View {
|
|
let onCodeScanned: (String) -> Void
|
|
|
|
@StateObject private var scanner = QRScannerViewModel()
|
|
@State private var didDetectCode = false
|
|
|
|
var body: some View {
|
|
ZStack {
|
|
Group {
|
|
if scanner.isPreviewAvailable {
|
|
ScannerPreview(session: scanner.captureSession)
|
|
} else {
|
|
Color.black
|
|
|
|
VStack(alignment: .leading, spacing: 10) {
|
|
Image(systemName: "video.slash.fill")
|
|
.font(.system(size: 24, weight: .semibold))
|
|
.foregroundStyle(.white)
|
|
Text("Camera preview unavailable")
|
|
.font(.title3.weight(.semibold))
|
|
.foregroundStyle(.white)
|
|
Text(scanner.statusMessage)
|
|
.foregroundStyle(.white.opacity(0.78))
|
|
}
|
|
.padding(24)
|
|
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading)
|
|
}
|
|
}
|
|
|
|
LinearGradient(
|
|
colors: [Color.black.opacity(0.64), Color.clear, Color.black.opacity(0.78)],
|
|
startPoint: .top,
|
|
endPoint: .bottom
|
|
)
|
|
.ignoresSafeArea()
|
|
|
|
ScanFrameOverlay(detected: didDetectCode)
|
|
.padding(40)
|
|
|
|
VStack {
|
|
HStack {
|
|
ScannerStatusBadge(title: scanner.isPreviewAvailable ? "Camera ready" : "Manual fallback")
|
|
Spacer()
|
|
}
|
|
.padding(.horizontal, 20)
|
|
.padding(.top, 18)
|
|
|
|
Spacer()
|
|
|
|
VStack(alignment: .leading, spacing: 8) {
|
|
Text("Align the pairing QR inside the frame")
|
|
.font(.headline.weight(.semibold))
|
|
.foregroundStyle(.white)
|
|
|
|
Text(scanner.statusMessage)
|
|
.font(.subheadline)
|
|
.foregroundStyle(.white.opacity(0.84))
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
}
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
.padding(18)
|
|
.background(Color.black.opacity(0.34), in: RoundedRectangle(cornerRadius: 24, style: .continuous))
|
|
.overlay(
|
|
RoundedRectangle(cornerRadius: 24, style: .continuous)
|
|
.stroke(Color.white.opacity(0.12), lineWidth: 1)
|
|
)
|
|
.padding(.horizontal, 18)
|
|
.padding(.bottom, 18)
|
|
}
|
|
}
|
|
.task {
|
|
scanner.onCodeScanned = { payload in
|
|
withAnimation(.spring(response: 0.3, dampingFraction: 0.82)) {
|
|
didDetectCode = true
|
|
}
|
|
|
|
Task {
|
|
try? await Task.sleep(for: .milliseconds(180))
|
|
onCodeScanned(payload)
|
|
}
|
|
}
|
|
await scanner.start()
|
|
}
|
|
.onDisappear {
|
|
scanner.stop()
|
|
}
|
|
}
|
|
}
|
|
|
|
private struct ScanFrameOverlay: View {
|
|
let detected: Bool
|
|
|
|
var body: some View {
|
|
GeometryReader { geometry in
|
|
let size = min(geometry.size.width, geometry.size.height) * 0.5
|
|
let inset = detected ? 18.0 : 0
|
|
|
|
ZStack {
|
|
RoundedRectangle(cornerRadius: 30, style: .continuous)
|
|
.fill(Color.black.opacity(0.18))
|
|
.frame(width: size - inset, height: size - inset)
|
|
|
|
RoundedRectangle(cornerRadius: 30, style: .continuous)
|
|
.stroke(
|
|
Color.white.opacity(detected ? 0.95 : 0.75),
|
|
style: StrokeStyle(lineWidth: detected ? 3 : 2, lineCap: .round, lineJoin: .round)
|
|
)
|
|
.frame(width: size - inset, height: size - inset)
|
|
|
|
CornerTick(rotation: .degrees(0))
|
|
.frame(width: size, height: size)
|
|
CornerTick(rotation: .degrees(90))
|
|
.frame(width: size, height: size)
|
|
CornerTick(rotation: .degrees(180))
|
|
.frame(width: size, height: size)
|
|
CornerTick(rotation: .degrees(270))
|
|
.frame(width: size, height: size)
|
|
}
|
|
.frame(width: size - inset, height: size - inset)
|
|
.position(x: geometry.size.width / 2, y: geometry.size.height / 2)
|
|
.animation(.spring(response: 0.3, dampingFraction: 0.82), value: detected)
|
|
}
|
|
.allowsHitTesting(false)
|
|
}
|
|
}
|
|
|
|
private struct ScannerStatusBadge: View {
|
|
let title: String
|
|
|
|
var body: some View {
|
|
HStack(spacing: 8) {
|
|
Circle()
|
|
.fill(IdP.tint)
|
|
.frame(width: 8, height: 8)
|
|
|
|
Text(title)
|
|
.font(.footnote.weight(.semibold))
|
|
.foregroundStyle(.white)
|
|
}
|
|
.padding(.horizontal, 12)
|
|
.padding(.vertical, 8)
|
|
.background(Color.black.opacity(0.26), in: Capsule(style: .continuous))
|
|
.overlay(
|
|
Capsule(style: .continuous)
|
|
.stroke(Color.white.opacity(0.10), lineWidth: 1)
|
|
)
|
|
}
|
|
}
|
|
|
|
private struct ManualFallbackCard: View {
|
|
@Binding var manualFallback: String
|
|
let onUsePayload: () -> Void
|
|
let onPasteFromClipboard: () -> Void
|
|
|
|
private var hasManualPayload: Bool {
|
|
!manualFallback.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
|
}
|
|
|
|
var body: some View {
|
|
VStack(alignment: .leading, spacing: 14) {
|
|
HStack(alignment: .top, spacing: 12) {
|
|
Image(systemName: "doc.text.viewfinder")
|
|
.font(.system(size: 18, weight: .semibold))
|
|
.foregroundStyle(IdP.tint)
|
|
.frame(width: 34, height: 34)
|
|
.background(IdP.tint.opacity(0.12), in: RoundedRectangle(cornerRadius: 12, style: .continuous))
|
|
|
|
VStack(alignment: .leading, spacing: 4) {
|
|
Text("Paste pairing link")
|
|
.font(.headline)
|
|
|
|
Text("If scanning is unavailable, paste the idp.global pairing link from your browser or clipboard.")
|
|
.font(.footnote)
|
|
.foregroundStyle(Color.idpMutedForeground)
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
}
|
|
}
|
|
|
|
ZStack(alignment: .topLeading) {
|
|
TextEditor(text: $manualFallback)
|
|
.font(.footnote.monospaced())
|
|
.scrollContentBackground(.hidden)
|
|
.frame(minHeight: 110)
|
|
.padding(10)
|
|
.background(Color.idpTertiaryFill, in: RoundedRectangle(cornerRadius: IdP.cardRadius, style: .continuous))
|
|
|
|
if manualFallback.isEmpty {
|
|
Text("idp.global://pair?token=...")
|
|
.font(.footnote.monospaced())
|
|
.foregroundStyle(Color.idpMutedForeground)
|
|
.padding(.horizontal, 16)
|
|
.padding(.vertical, 18)
|
|
.allowsHitTesting(false)
|
|
}
|
|
}
|
|
|
|
VStack(spacing: 10) {
|
|
Button("Use pasted payload", action: onUsePayload)
|
|
.buttonStyle(PrimaryActionStyle())
|
|
.disabled(!hasManualPayload)
|
|
|
|
Button {
|
|
onPasteFromClipboard()
|
|
} label: {
|
|
Label("Paste from clipboard", systemImage: "doc.on.clipboard")
|
|
}
|
|
.buttonStyle(SecondaryActionStyle())
|
|
}
|
|
}
|
|
.padding(18)
|
|
.background(.regularMaterial, in: RoundedRectangle(cornerRadius: IdP.cardRadius, style: .continuous))
|
|
.overlay(
|
|
RoundedRectangle(cornerRadius: IdP.cardRadius, style: .continuous)
|
|
.stroke(Color.white.opacity(0.10), lineWidth: 1)
|
|
)
|
|
}
|
|
}
|
|
|
|
private enum ClipboardPayloadReader {
|
|
static func readString() -> String {
|
|
#if os(iOS)
|
|
UIPasteboard.general.string?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
|
#elseif os(macOS)
|
|
NSPasteboard.general.string(forType: .string)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
|
|
#else
|
|
""
|
|
#endif
|
|
}
|
|
}
|
|
|
|
private struct CornerTick: View {
|
|
let rotation: Angle
|
|
|
|
var body: some View {
|
|
Path { path in
|
|
let length: CGFloat = 34
|
|
path.move(to: CGPoint(x: 0, y: length))
|
|
path.addLine(to: CGPoint(x: 0, y: 0))
|
|
path.addLine(to: CGPoint(x: length, y: 0))
|
|
}
|
|
.stroke(.white, style: StrokeStyle(lineWidth: 5, lineCap: .round, lineJoin: .round))
|
|
.rotationEffect(rotation)
|
|
}
|
|
}
|
|
|
|
private final class QRScannerViewModel: NSObject, ObservableObject, AVCaptureMetadataOutputObjectsDelegate {
|
|
@Published var isPreviewAvailable = false
|
|
@Published var statusMessage = "Point the camera at the QR code from the idp.global web portal."
|
|
|
|
let captureSession = AVCaptureSession()
|
|
|
|
var onCodeScanned: ((String) -> Void)?
|
|
|
|
private let queue = DispatchQueue(label: "global.idp.qrscanner")
|
|
private var isConfigured = false
|
|
private var hasDeliveredCode = false
|
|
|
|
func start() async {
|
|
#if os(iOS) && targetEnvironment(simulator)
|
|
await MainActor.run {
|
|
isPreviewAvailable = false
|
|
statusMessage = "The iOS simulator has no live camera feed. Paste a pairing link below instead."
|
|
}
|
|
#endif
|
|
|
|
#if !(os(iOS) && targetEnvironment(simulator))
|
|
let authorization = AVCaptureDevice.authorizationStatus(for: .video)
|
|
switch authorization {
|
|
case .authorized:
|
|
await configureIfNeeded()
|
|
startRunning()
|
|
case .notDetermined:
|
|
let granted = await requestCameraAccess()
|
|
await MainActor.run {
|
|
self.statusMessage = granted
|
|
? "Point the camera at the QR code from the idp.global web portal."
|
|
: "Camera access was denied. Use the manual fallback instead."
|
|
}
|
|
guard granted else { return }
|
|
await configureIfNeeded()
|
|
startRunning()
|
|
case .denied, .restricted:
|
|
await MainActor.run {
|
|
isPreviewAvailable = false
|
|
statusMessage = "Camera access is unavailable. Use the manual fallback instead."
|
|
}
|
|
@unknown default:
|
|
await MainActor.run {
|
|
isPreviewAvailable = false
|
|
statusMessage = "Camera access could not be initialized on this device."
|
|
}
|
|
}
|
|
#endif
|
|
}
|
|
|
|
func stop() {
|
|
queue.async {
|
|
if self.captureSession.isRunning {
|
|
self.captureSession.stopRunning()
|
|
}
|
|
}
|
|
}
|
|
|
|
func metadataOutput(
|
|
_ output: AVCaptureMetadataOutput,
|
|
didOutput metadataObjects: [AVMetadataObject],
|
|
from connection: AVCaptureConnection
|
|
) {
|
|
guard !hasDeliveredCode,
|
|
let readable = metadataObjects.first as? AVMetadataMachineReadableCodeObject,
|
|
readable.type == .qr,
|
|
let payload = readable.stringValue else {
|
|
return
|
|
}
|
|
|
|
hasDeliveredCode = true
|
|
stop()
|
|
|
|
#if os(iOS)
|
|
UINotificationFeedbackGenerator().notificationOccurred(.success)
|
|
#endif
|
|
|
|
DispatchQueue.main.async { [onCodeScanned] in
|
|
onCodeScanned?(payload)
|
|
}
|
|
}
|
|
|
|
private func requestCameraAccess() async -> Bool {
|
|
await withCheckedContinuation { continuation in
|
|
AVCaptureDevice.requestAccess(for: .video) { granted in
|
|
continuation.resume(returning: granted)
|
|
}
|
|
}
|
|
}
|
|
|
|
private func configureIfNeeded() async {
|
|
guard !isConfigured else {
|
|
await MainActor.run {
|
|
self.isPreviewAvailable = true
|
|
}
|
|
return
|
|
}
|
|
|
|
await withCheckedContinuation { (continuation: CheckedContinuation<Void, Never>) in
|
|
queue.async {
|
|
self.captureSession.beginConfiguration()
|
|
defer {
|
|
self.captureSession.commitConfiguration()
|
|
continuation.resume()
|
|
}
|
|
|
|
guard let device = AVCaptureDevice.default(for: .video) else {
|
|
DispatchQueue.main.async {
|
|
self.isPreviewAvailable = false
|
|
self.statusMessage = "No compatible camera was found. Use the manual fallback instead."
|
|
}
|
|
return
|
|
}
|
|
|
|
guard let input = try? AVCaptureDeviceInput(device: device) else {
|
|
DispatchQueue.main.async {
|
|
self.isPreviewAvailable = false
|
|
self.statusMessage = "No compatible camera was found. Use the manual fallback instead."
|
|
}
|
|
return
|
|
}
|
|
|
|
guard self.captureSession.canAddInput(input) else {
|
|
DispatchQueue.main.async {
|
|
self.isPreviewAvailable = false
|
|
self.statusMessage = "No compatible camera was found. Use the manual fallback instead."
|
|
}
|
|
return
|
|
}
|
|
|
|
self.captureSession.addInput(input)
|
|
|
|
let output = AVCaptureMetadataOutput()
|
|
guard self.captureSession.canAddOutput(output) else {
|
|
self.captureSession.removeInput(input)
|
|
DispatchQueue.main.async {
|
|
self.isPreviewAvailable = false
|
|
self.statusMessage = "Unable to configure QR scanning on this device."
|
|
}
|
|
return
|
|
}
|
|
|
|
self.captureSession.addOutput(output)
|
|
output.setMetadataObjectsDelegate(self, queue: DispatchQueue.main)
|
|
|
|
let supportedTypes = output.availableMetadataObjectTypes
|
|
guard supportedTypes.contains(.qr) else {
|
|
self.captureSession.removeOutput(output)
|
|
self.captureSession.removeInput(input)
|
|
DispatchQueue.main.async {
|
|
self.isPreviewAvailable = false
|
|
self.statusMessage = "This camera does not support QR scanning. Use the manual fallback instead."
|
|
}
|
|
return
|
|
}
|
|
|
|
output.metadataObjectTypes = [.qr]
|
|
self.isConfigured = true
|
|
|
|
DispatchQueue.main.async {
|
|
self.isPreviewAvailable = true
|
|
self.statusMessage = "Point the camera at the QR code from the idp.global web portal."
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private func startRunning() {
|
|
queue.async {
|
|
guard !self.captureSession.isRunning else { return }
|
|
self.hasDeliveredCode = false
|
|
self.captureSession.startRunning()
|
|
}
|
|
}
|
|
}
|
|
|
|
extension QRScannerViewModel: @unchecked Sendable {}
|
|
|
|
#if os(iOS)
|
|
private struct ScannerPreview: UIViewRepresentable {
|
|
let session: AVCaptureSession
|
|
|
|
func makeUIView(context: Context) -> ScannerPreviewUIView {
|
|
let view = ScannerPreviewUIView()
|
|
view.previewLayer.session = session
|
|
view.previewLayer.videoGravity = .resizeAspectFill
|
|
return view
|
|
}
|
|
|
|
func updateUIView(_ uiView: ScannerPreviewUIView, context: Context) {
|
|
uiView.previewLayer.session = session
|
|
}
|
|
}
|
|
|
|
private final class ScannerPreviewUIView: UIView {
|
|
override class var layerClass: AnyClass {
|
|
AVCaptureVideoPreviewLayer.self
|
|
}
|
|
|
|
var previewLayer: AVCaptureVideoPreviewLayer {
|
|
layer as! AVCaptureVideoPreviewLayer
|
|
}
|
|
}
|
|
#elseif os(macOS)
|
|
private struct ScannerPreview: NSViewRepresentable {
|
|
let session: AVCaptureSession
|
|
|
|
func makeNSView(context: Context) -> ScannerPreviewNSView {
|
|
let view = ScannerPreviewNSView()
|
|
view.attach(session: session)
|
|
return view
|
|
}
|
|
|
|
func updateNSView(_ nsView: ScannerPreviewNSView, context: Context) {
|
|
nsView.attach(session: session)
|
|
}
|
|
}
|
|
|
|
private final class ScannerPreviewNSView: NSView {
|
|
private var previewLayer: AVCaptureVideoPreviewLayer?
|
|
|
|
override init(frame frameRect: NSRect) {
|
|
super.init(frame: frameRect)
|
|
wantsLayer = true
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
super.init(coder: coder)
|
|
wantsLayer = true
|
|
}
|
|
|
|
func attach(session: AVCaptureSession) {
|
|
let layer = previewLayer ?? AVCaptureVideoPreviewLayer(session: session)
|
|
layer.session = session
|
|
layer.videoGravity = .resizeAspectFill
|
|
self.layer = layer
|
|
previewLayer = layer
|
|
}
|
|
}
|
|
#endif
|