Skip to content

Commit 8af7ed9

Browse files
committed
feat: replace isTorchon to torchMode
1 parent 5e88643 commit 8af7ed9

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

Sources/CodeScanner/CodeScanner.swift

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public enum ScanError: Error {
2020

2121
/// Initialization failed.
2222
case initError(_ error: Error)
23-
24-
/// The camera permission is denied
23+
24+
/// The camera permission is denied
2525
case permissionDenied
2626
}
2727

@@ -35,10 +35,10 @@ public struct ScanResult {
3535

3636
/// The type of code that was matched.
3737
public let type: AVMetadataObject.ObjectType
38-
38+
3939
/// The image of the code that was matched
4040
public let image: UIImage?
41-
41+
4242
/// The corner coordinates of the scanned code.
4343
public let corners: [CGPoint]
4444
}
@@ -76,7 +76,6 @@ public enum ScanMode {
7676
/// For testing inside the simulator, set the `simulatedData` property to some test data you want to send back.
7777
@available(macCatalyst 14.0, *)
7878
public struct CodeScannerView: UIViewControllerRepresentable {
79-
8079
public let codeTypes: [AVMetadataObject.ObjectType]
8180
public let scanMode: ScanMode
8281
public let manualSelect: Bool
@@ -85,12 +84,17 @@ public struct CodeScannerView: UIViewControllerRepresentable {
8584
public let requiresPhotoOutput: Bool
8685
public var simulatedData = ""
8786
public var shouldVibrateOnSuccess: Bool
88-
public var isTorchOn: Bool
87+
public var torchMode: AVCaptureDevice.TorchMode
8988
public var isPaused: Bool
9089
public var isGalleryPresented: Binding<Bool>
9190
public var videoCaptureDevice: AVCaptureDevice?
9291
public var completion: (Result<ScanResult, ScanError>) -> Void
9392

93+
public var isTorchOn: Bool {
94+
get { torchMode == .on }
95+
set { torchMode = newValue ? .on : .off }
96+
}
97+
9498
public init(
9599
codeTypes: [AVMetadataObject.ObjectType],
96100
scanMode: ScanMode = .once,
@@ -101,6 +105,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
101105
simulatedData: String = "",
102106
shouldVibrateOnSuccess: Bool = true,
103107
isTorchOn: Bool = false,
108+
torchMode: AVCaptureDevice.TorchMode? = nil,
104109
isPaused: Bool = false,
105110
isGalleryPresented: Binding<Bool> = .constant(false),
106111
videoCaptureDevice: AVCaptureDevice? = AVCaptureDevice.bestForVideo,
@@ -114,7 +119,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
114119
self.scanInterval = scanInterval
115120
self.simulatedData = simulatedData
116121
self.shouldVibrateOnSuccess = shouldVibrateOnSuccess
117-
self.isTorchOn = isTorchOn
122+
self.torchMode = torchMode ?? (isTorchOn ? .on : .off)
118123
self.isPaused = isPaused
119124
self.isGalleryPresented = isGalleryPresented
120125
self.videoCaptureDevice = videoCaptureDevice
@@ -128,20 +133,18 @@ public struct CodeScannerView: UIViewControllerRepresentable {
128133
public func updateUIViewController(_ uiViewController: ScannerViewController, context: Context) {
129134
uiViewController.parentView = self
130135
uiViewController.updateViewController(
131-
isTorchOn: isTorchOn,
136+
torchMode: torchMode,
132137
isGalleryPresented: isGalleryPresented.wrappedValue,
133138
isManualCapture: scanMode.isManual,
134139
isManualSelect: manualSelect
135140
)
136141
}
137-
138142
}
139143

140144
@available(macCatalyst 14.0, *)
141-
extension CodeScannerView {
142-
145+
public extension CodeScannerView {
143146
@available(*, deprecated, renamed: "requiresPhotoOutput")
144-
public var requirePhotoOutput: Bool {
147+
var requirePhotoOutput: Bool {
145148
requiresPhotoOutput
146149
}
147150
}

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)