From 11dd17b6f83547bc2735e16063cf59caf7c89686 Mon Sep 17 00:00:00 2001 From: islam <2553451+qalandarov@users.noreply.github.com> Date: Sun, 14 Jan 2024 14:05:15 +0000 Subject: [PATCH 1/2] Ability to pause scanning (i.e. when presenting) --- Sources/CodeScanner/CodeScanner.swift | 3 +++ Sources/CodeScanner/ScannerViewController.swift | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/Sources/CodeScanner/CodeScanner.swift b/Sources/CodeScanner/CodeScanner.swift index 0f9acd1..de40c30 100644 --- a/Sources/CodeScanner/CodeScanner.swift +++ b/Sources/CodeScanner/CodeScanner.swift @@ -85,6 +85,7 @@ public struct CodeScannerView: UIViewControllerRepresentable { public var simulatedData = "" public var shouldVibrateOnSuccess: Bool public var isTorchOn: Bool + public var isPaused: Bool public var isGalleryPresented: Binding public var videoCaptureDevice: AVCaptureDevice? public var completion: (Result) -> Void @@ -99,6 +100,7 @@ public struct CodeScannerView: UIViewControllerRepresentable { simulatedData: String = "", shouldVibrateOnSuccess: Bool = true, isTorchOn: Bool = false, + isPaused: Bool = false, isGalleryPresented: Binding = .constant(false), videoCaptureDevice: AVCaptureDevice? = AVCaptureDevice.bestForVideo, completion: @escaping (Result) -> Void @@ -112,6 +114,7 @@ public struct CodeScannerView: UIViewControllerRepresentable { self.simulatedData = simulatedData self.shouldVibrateOnSuccess = shouldVibrateOnSuccess self.isTorchOn = isTorchOn + self.isPaused = isPaused self.isGalleryPresented = isGalleryPresented self.videoCaptureDevice = videoCaptureDevice self.completion = completion diff --git a/Sources/CodeScanner/ScannerViewController.swift b/Sources/CodeScanner/ScannerViewController.swift index da1b430..acb4488 100644 --- a/Sources/CodeScanner/ScannerViewController.swift +++ b/Sources/CodeScanner/ScannerViewController.swift @@ -436,9 +436,12 @@ extension CodeScannerView { extension CodeScannerView.ScannerViewController: AVCaptureMetadataOutputObjectsDelegate { public func metadataOutput(_ output: AVCaptureMetadataOutput, didOutput metadataObjects: [AVMetadataObject], from connection: AVCaptureConnection) { if let metadataObject = metadataObjects.first { - guard let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject else { return } - guard let stringValue = readableObject.stringValue else { return } - guard !didFinishScanning && !isCapturing else { return } + guard !parentView.isPaused && !didFinishScanning && !isCapturing, + let readableObject = metadataObject as? AVMetadataMachineReadableCodeObject, + let stringValue = readableObject.stringValue + else { + return + } handler = { [self] image in let result = ScanResult(string: stringValue, type: readableObject.type, image: image, corners: readableObject.corners) From 24ffca48d9d15b0fafbc8cc8f203dbfc4dafbac7 Mon Sep 17 00:00:00 2001 From: islam <2553451+qalandarov@users.noreply.github.com> Date: Sun, 14 Jan 2024 15:01:03 +0000 Subject: [PATCH 2/2] Minor improvement --- Sources/CodeScanner/ScannerViewController.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/CodeScanner/ScannerViewController.swift b/Sources/CodeScanner/ScannerViewController.swift index acb4488..366419d 100644 --- a/Sources/CodeScanner/ScannerViewController.swift +++ b/Sources/CodeScanner/ScannerViewController.swift @@ -12,7 +12,7 @@ import UIKit @available(macCatalyst 14.0, *) extension CodeScannerView { - public class ScannerViewController: UIViewController, UINavigationControllerDelegate { + public final class ScannerViewController: UIViewController, UINavigationControllerDelegate { private let photoOutput = AVCapturePhotoOutput() private var isCapturing = false private var handler: ((UIImage?) -> Void)? @@ -174,7 +174,7 @@ extension CodeScannerView { } private func setupSession() { - guard let captureSession = captureSession else { + guard let captureSession else { return }