@@ -20,8 +20,8 @@ public enum ScanError: Error {
20
20
21
21
/// Initialization failed.
22
22
case initError( _ error: Error )
23
-
24
- /// The camera permission is denied
23
+
24
+ /// The camera permission is denied
25
25
case permissionDenied
26
26
}
27
27
@@ -35,10 +35,10 @@ public struct ScanResult {
35
35
36
36
/// The type of code that was matched.
37
37
public let type : AVMetadataObject . ObjectType
38
-
38
+
39
39
/// The image of the code that was matched
40
40
public let image : UIImage ?
41
-
41
+
42
42
/// The corner coordinates of the scanned code.
43
43
public let corners : [ CGPoint ]
44
44
}
@@ -76,7 +76,6 @@ public enum ScanMode {
76
76
/// For testing inside the simulator, set the `simulatedData` property to some test data you want to send back.
77
77
@available ( macCatalyst 14 . 0 , * )
78
78
public struct CodeScannerView : UIViewControllerRepresentable {
79
-
80
79
public let codeTypes : [ AVMetadataObject . ObjectType ]
81
80
public let scanMode : ScanMode
82
81
public let manualSelect : Bool
@@ -85,12 +84,17 @@ public struct CodeScannerView: UIViewControllerRepresentable {
85
84
public let requiresPhotoOutput : Bool
86
85
public var simulatedData = " "
87
86
public var shouldVibrateOnSuccess : Bool
88
- public var isTorchOn : Bool
87
+ public var torchMode : AVCaptureDevice . TorchMode
89
88
public var isPaused : Bool
90
89
public var isGalleryPresented : Binding < Bool >
91
90
public var videoCaptureDevice : AVCaptureDevice ?
92
91
public var completion : ( Result < ScanResult , ScanError > ) -> Void
93
92
93
+ public var isTorchOn : Bool {
94
+ get { torchMode == . on }
95
+ set { torchMode = newValue ? . on : . off }
96
+ }
97
+
94
98
public init (
95
99
codeTypes: [ AVMetadataObject . ObjectType ] ,
96
100
scanMode: ScanMode = . once,
@@ -101,6 +105,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
101
105
simulatedData: String = " " ,
102
106
shouldVibrateOnSuccess: Bool = true ,
103
107
isTorchOn: Bool = false ,
108
+ torchMode: AVCaptureDevice . TorchMode ? = nil ,
104
109
isPaused: Bool = false ,
105
110
isGalleryPresented: Binding < Bool > = . constant( false ) ,
106
111
videoCaptureDevice: AVCaptureDevice ? = AVCaptureDevice . bestForVideo,
@@ -114,7 +119,7 @@ public struct CodeScannerView: UIViewControllerRepresentable {
114
119
self . scanInterval = scanInterval
115
120
self . simulatedData = simulatedData
116
121
self . shouldVibrateOnSuccess = shouldVibrateOnSuccess
117
- self . isTorchOn = isTorchOn
122
+ self . torchMode = torchMode ?? ( isTorchOn ? . on : . off )
118
123
self . isPaused = isPaused
119
124
self . isGalleryPresented = isGalleryPresented
120
125
self . videoCaptureDevice = videoCaptureDevice
@@ -128,20 +133,18 @@ public struct CodeScannerView: UIViewControllerRepresentable {
128
133
public func updateUIViewController( _ uiViewController: ScannerViewController , context: Context ) {
129
134
uiViewController. parentView = self
130
135
uiViewController. updateViewController (
131
- isTorchOn : isTorchOn ,
136
+ torchMode : torchMode ,
132
137
isGalleryPresented: isGalleryPresented. wrappedValue,
133
138
isManualCapture: scanMode. isManual,
134
139
isManualSelect: manualSelect
135
140
)
136
141
}
137
-
138
142
}
139
143
140
144
@available ( macCatalyst 14 . 0 , * )
141
- extension CodeScannerView {
142
-
145
+ public extension CodeScannerView {
143
146
@available ( * , deprecated, renamed: " requiresPhotoOutput " )
144
- public var requirePhotoOutput : Bool {
147
+ var requirePhotoOutput : Bool {
145
148
requiresPhotoOutput
146
149
}
147
150
}
0 commit comments