Skip to content

Commit 558e497

Browse files
committed
feat: replace isTorchon to torchMode
1 parent 5e88643 commit 558e497

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Sources/CodeScanner/CodeScanner.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,17 @@ public struct CodeScannerView: UIViewControllerRepresentable {
8585
public let requiresPhotoOutput: Bool
8686
public var simulatedData = ""
8787
public var shouldVibrateOnSuccess: Bool
88-
public var isTorchOn: Bool
88+
public var torchMode: AVCaptureDevice.TorchMode
8989
public var isPaused: Bool
9090
public var isGalleryPresented: Binding<Bool>
9191
public var videoCaptureDevice: AVCaptureDevice?
9292
public var completion: (Result<ScanResult, ScanError>) -> Void
9393

94+
public var isTorchOn: Bool {
95+
get { torchMode == .on }
96+
set { torchMode = newValue ? .on : .off }
97+
}
98+
9499
public init(
95100
codeTypes: [AVMetadataObject.ObjectType],
96101
scanMode: ScanMode = .once,
@@ -101,6 +106,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
101106
simulatedData: String = "",
102107
shouldVibrateOnSuccess: Bool = true,
103108
isTorchOn: Bool = false,
109+
torchMode: AVCaptureDevice.TorchMode? = nil,
104110
isPaused: Bool = false,
105111
isGalleryPresented: Binding<Bool> = .constant(false),
106112
videoCaptureDevice: AVCaptureDevice? = AVCaptureDevice.bestForVideo,
@@ -114,7 +120,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
114120
self.scanInterval = scanInterval
115121
self.simulatedData = simulatedData
116122
self.shouldVibrateOnSuccess = shouldVibrateOnSuccess
117-
self.isTorchOn = isTorchOn
123+
self.torchMode = torchMode ?? (isTorchOn ? .on : .off)
118124
self.isPaused = isPaused
119125
self.isGalleryPresented = isGalleryPresented
120126
self.videoCaptureDevice = videoCaptureDevice
@@ -128,7 +134,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
128134
public func updateUIViewController(_ uiViewController: ScannerViewController, context: Context) {
129135
uiViewController.parentView = self
130136
uiViewController.updateViewController(
131-
isTorchOn: isTorchOn,
137+
torchMode: torchMode,
132138
isGalleryPresented: isGalleryPresented.wrappedValue,
133139
isManualCapture: scanMode.isManual,
134140
isManualSelect: manualSelect

Sources/CodeScanner/ScannerViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,14 @@ extension CodeScannerView {
373373
}
374374
#endif
375375

376-
func updateViewController(isTorchOn: Bool, isGalleryPresented: Bool, isManualCapture: Bool, isManualSelect: Bool) {
376+
func updateViewController(torchMode: AVCaptureDevice.TorchMode, isGalleryPresented: Bool, isManualCapture: Bool, isManualSelect: Bool) {
377377
guard let videoCaptureDevice = parentView.videoCaptureDevice ?? fallbackVideoCaptureDevice else {
378378
return
379379
}
380380

381381
if videoCaptureDevice.hasTorch {
382382
try? videoCaptureDevice.lockForConfiguration()
383-
videoCaptureDevice.torchMode = isTorchOn ? .on : .off
383+
videoCaptureDevice.torchMode = torchMode
384384
videoCaptureDevice.unlockForConfiguration()
385385
}
386386

0 commit comments

Comments
 (0)