From 558e49799efc16e52917b06ccd405e406055413d Mon Sep 17 00:00:00 2001 From: Jordan Chapelle Date: Wed, 29 Jan 2025 09:29:34 +0100 Subject: [PATCH] feat: replace isTorchon to torchMode --- Sources/CodeScanner/CodeScanner.swift | 12 +++++++++--- Sources/CodeScanner/ScannerViewController.swift | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/Sources/CodeScanner/CodeScanner.swift b/Sources/CodeScanner/CodeScanner.swift index 85b7994..4c9dd4f 100644 --- a/Sources/CodeScanner/CodeScanner.swift +++ b/Sources/CodeScanner/CodeScanner.swift @@ -85,12 +85,17 @@ public struct CodeScannerView: UIViewControllerRepresentable { public let requiresPhotoOutput: Bool public var simulatedData = "" public var shouldVibrateOnSuccess: Bool - public var isTorchOn: Bool + public var torchMode: AVCaptureDevice.TorchMode public var isPaused: Bool public var isGalleryPresented: Binding public var videoCaptureDevice: AVCaptureDevice? public var completion: (Result) -> Void + public var isTorchOn: Bool { + get { torchMode == .on } + set { torchMode = newValue ? .on : .off } + } + public init( codeTypes: [AVMetadataObject.ObjectType], scanMode: ScanMode = .once, @@ -101,6 +106,7 @@ public struct CodeScannerView: UIViewControllerRepresentable { simulatedData: String = "", shouldVibrateOnSuccess: Bool = true, isTorchOn: Bool = false, + torchMode: AVCaptureDevice.TorchMode? = nil, isPaused: Bool = false, isGalleryPresented: Binding = .constant(false), videoCaptureDevice: AVCaptureDevice? = AVCaptureDevice.bestForVideo, @@ -114,7 +120,7 @@ public struct CodeScannerView: UIViewControllerRepresentable { self.scanInterval = scanInterval self.simulatedData = simulatedData self.shouldVibrateOnSuccess = shouldVibrateOnSuccess - self.isTorchOn = isTorchOn + self.torchMode = torchMode ?? (isTorchOn ? .on : .off) self.isPaused = isPaused self.isGalleryPresented = isGalleryPresented self.videoCaptureDevice = videoCaptureDevice @@ -128,7 +134,7 @@ public struct CodeScannerView: UIViewControllerRepresentable { public func updateUIViewController(_ uiViewController: ScannerViewController, context: Context) { uiViewController.parentView = self uiViewController.updateViewController( - isTorchOn: isTorchOn, + torchMode: torchMode, isGalleryPresented: isGalleryPresented.wrappedValue, isManualCapture: scanMode.isManual, isManualSelect: manualSelect diff --git a/Sources/CodeScanner/ScannerViewController.swift b/Sources/CodeScanner/ScannerViewController.swift index 292194a..d8fd1ba 100644 --- a/Sources/CodeScanner/ScannerViewController.swift +++ b/Sources/CodeScanner/ScannerViewController.swift @@ -373,14 +373,14 @@ extension CodeScannerView { } #endif - func updateViewController(isTorchOn: Bool, isGalleryPresented: Bool, isManualCapture: Bool, isManualSelect: Bool) { + func updateViewController(torchMode: AVCaptureDevice.TorchMode, isGalleryPresented: Bool, isManualCapture: Bool, isManualSelect: Bool) { guard let videoCaptureDevice = parentView.videoCaptureDevice ?? fallbackVideoCaptureDevice else { return } if videoCaptureDevice.hasTorch { try? videoCaptureDevice.lockForConfiguration() - videoCaptureDevice.torchMode = isTorchOn ? .on : .off + videoCaptureDevice.torchMode = torchMode videoCaptureDevice.unlockForConfiguration() }