Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions Sources/CodeScanner/CodeScanner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Bool>
public var videoCaptureDevice: AVCaptureDevice?
public var completion: (Result<ScanResult, ScanError>) -> Void

public var isTorchOn: Bool {
get { torchMode == .on }
set { torchMode = newValue ? .on : .off }
}

public init(
codeTypes: [AVMetadataObject.ObjectType],
scanMode: ScanMode = .once,
Expand All @@ -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<Bool> = .constant(false),
videoCaptureDevice: AVCaptureDevice? = AVCaptureDevice.bestForVideo,
Expand All @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Sources/CodeScanner/ScannerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down