|
3 | 3 |
|
4 | 4 | import AVFoundation |
5 | 5 | import SwiftUI |
| 6 | +import UIKit |
6 | 7 |
|
7 | 8 | struct SimpleQRCodeScannerView: UIViewControllerRepresentable { |
8 | 9 | @Environment(\.presentationMode) var presentationMode |
9 | 10 | var completion: (Result<String, Error>) -> Void |
10 | 11 |
|
11 | | - // MARK: - Coordinator |
| 12 | + func makeUIViewController(context _: Context) -> UINavigationController { |
| 13 | + let scannerVC = SimpleQRCodeScannerViewController { result in |
| 14 | + completion(result) |
| 15 | + } |
12 | 16 |
|
13 | | - class Coordinator: NSObject, AVCaptureMetadataOutputObjectsDelegate { |
14 | | - var parent: SimpleQRCodeScannerView |
15 | | - var session: AVCaptureSession? |
| 17 | + let navController = UINavigationController(rootViewController: scannerVC) |
16 | 18 |
|
17 | | - init(parent: SimpleQRCodeScannerView) { |
18 | | - self.parent = parent |
| 19 | + // Apply dark mode if needed |
| 20 | + if Storage.shared.forceDarkMode.value { |
| 21 | + scannerVC.overrideUserInterfaceStyle = .dark |
| 22 | + navController.overrideUserInterfaceStyle = .dark |
19 | 23 | } |
20 | 24 |
|
21 | | - func metadataOutput(_: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from _: AVCaptureConnection) { |
22 | | - if let session, session.isRunning { |
23 | | - if let metadataObject = metadataObjects.first as? AVMetadataMachineReadableCodeObject, |
24 | | - metadataObject.type == .qr, |
25 | | - let stringValue = metadataObject.stringValue |
26 | | - { |
27 | | - DispatchQueue.global(qos: .userInitiated).async { |
28 | | - session.stopRunning() |
29 | | - } |
30 | | - parent.completion(.success(stringValue)) |
31 | | - } |
32 | | - } |
33 | | - } |
| 25 | + return navController |
| 26 | + } |
| 27 | + |
| 28 | + func updateUIViewController(_: UINavigationController, context _: Context) {} |
| 29 | +} |
| 30 | + |
| 31 | +class SimpleQRCodeScannerViewController: UIViewController { |
| 32 | + private var session: AVCaptureSession? |
| 33 | + private var completion: (Result<String, Error>) -> Void |
| 34 | + |
| 35 | + init(completion: @escaping (Result<String, Error>) -> Void) { |
| 36 | + self.completion = completion |
| 37 | + super.init(nibName: nil, bundle: nil) |
34 | 38 | } |
35 | 39 |
|
36 | | - // MARK: - UIViewControllerRepresentable Methods |
| 40 | + @available(*, unavailable) |
| 41 | + required init?(coder _: NSCoder) { |
| 42 | + fatalError("init(coder:) has not been implemented") |
| 43 | + } |
| 44 | + |
| 45 | + override func viewDidLoad() { |
| 46 | + super.viewDidLoad() |
| 47 | + |
| 48 | + // Add cancel button |
| 49 | + navigationItem.rightBarButtonItem = UIBarButtonItem( |
| 50 | + barButtonSystemItem: .cancel, |
| 51 | + target: self, |
| 52 | + action: #selector(cancelTapped) |
| 53 | + ) |
| 54 | + navigationItem.title = "Scan QR Code" |
37 | 55 |
|
38 | | - func makeCoordinator() -> Coordinator { |
39 | | - Coordinator(parent: self) |
| 56 | + setupCamera() |
40 | 57 | } |
41 | 58 |
|
42 | | - func makeUIViewController(context: Context) -> UIViewController { |
43 | | - let controller = UIViewController() |
| 59 | + private func setupCamera() { |
44 | 60 | let session = AVCaptureSession() |
45 | | - context.coordinator.session = session // Assign session to coordinator |
| 61 | + self.session = session |
46 | 62 |
|
47 | 63 | guard let videoCaptureDevice = AVCaptureDevice.default(for: .video), |
48 | 64 | let videoInput = try? AVCaptureDeviceInput(device: videoCaptureDevice), |
49 | 65 | session.canAddInput(videoInput) |
50 | 66 | else { |
51 | 67 | let error = NSError(domain: "QRCodeScannerError", code: 1, userInfo: [NSLocalizedDescriptionKey: "Failed to set up camera input."]) |
52 | 68 | completion(.failure(error)) |
53 | | - return controller |
| 69 | + return |
54 | 70 | } |
55 | 71 |
|
56 | 72 | session.addInput(videoInput) |
57 | 73 |
|
58 | 74 | let metadataOutput = AVCaptureMetadataOutput() |
59 | 75 | if session.canAddOutput(metadataOutput) { |
60 | 76 | session.addOutput(metadataOutput) |
61 | | - metadataOutput.setMetadataObjectsDelegate(context.coordinator, queue: DispatchQueue.main) |
| 77 | + metadataOutput.setMetadataObjectsDelegate(self, queue: DispatchQueue.main) |
62 | 78 | metadataOutput.metadataObjectTypes = [.qr] |
63 | 79 | } else { |
64 | 80 | let error = NSError(domain: "QRCodeScannerError", code: 2, userInfo: [NSLocalizedDescriptionKey: "Failed to set up metadata output."]) |
65 | 81 | completion(.failure(error)) |
66 | | - return controller |
| 82 | + return |
67 | 83 | } |
68 | 84 |
|
69 | 85 | let previewLayer = AVCaptureVideoPreviewLayer(session: session) |
70 | | - previewLayer.frame = controller.view.layer.bounds |
| 86 | + previewLayer.frame = view.layer.bounds |
71 | 87 | previewLayer.videoGravity = .resizeAspectFill |
72 | | - controller.view.layer.addSublayer(previewLayer) |
| 88 | + view.layer.addSublayer(previewLayer) |
73 | 89 |
|
74 | 90 | DispatchQueue.global(qos: .userInitiated).async { |
75 | 91 | session.startRunning() |
76 | 92 | } |
77 | | - |
78 | | - return controller |
79 | 93 | } |
80 | 94 |
|
81 | | - func updateUIViewController(_: UIViewController, context _: Context) {} |
82 | | - |
83 | | - func dismantleUIViewController(_: UIViewController, coordinator: Coordinator) { |
| 95 | + @objc private func cancelTapped() { |
84 | 96 | DispatchQueue.global(qos: .userInitiated).async { |
85 | | - if let session = coordinator.session, session.isRunning { |
| 97 | + if let session = self.session, session.isRunning { |
86 | 98 | session.stopRunning() |
87 | 99 | } |
88 | 100 | } |
| 101 | + let error = NSError(domain: "QRCodeScannerError", code: 0, userInfo: [NSLocalizedDescriptionKey: "Scanning cancelled by user."]) |
| 102 | + completion(.failure(error)) |
| 103 | + } |
| 104 | + |
| 105 | + override func viewDidLayoutSubviews() { |
| 106 | + super.viewDidLayoutSubviews() |
| 107 | + if let previewLayer = view.layer.sublayers?.first as? AVCaptureVideoPreviewLayer { |
| 108 | + previewLayer.frame = view.layer.bounds |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +extension SimpleQRCodeScannerViewController: AVCaptureMetadataOutputObjectsDelegate { |
| 114 | + func metadataOutput(_: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from _: AVCaptureConnection) { |
| 115 | + if let session, session.isRunning { |
| 116 | + if let metadataObject = metadataObjects.first as? AVMetadataMachineReadableCodeObject, |
| 117 | + metadataObject.type == .qr, |
| 118 | + let stringValue = metadataObject.stringValue |
| 119 | + { |
| 120 | + DispatchQueue.global(qos: .userInitiated).async { |
| 121 | + session.stopRunning() |
| 122 | + } |
| 123 | + completion(.success(stringValue)) |
| 124 | + } |
| 125 | + } |
89 | 126 | } |
90 | 127 | } |
0 commit comments