Skip to content

Commit 10af6c8

Browse files
chore: Display a Selected Scan Button
Add the user interface for `OSBARCScanButton` when its feature is enabled, similar to what `OSBARCTorchButton` does. This involves creating a new `isOn` property. Indent `OSBARCTorchButton`'s view modifiers. Improve Scanning performance by filtering the readable types on `VNDetectBarcodesRequest` creation. Add vibration effect when a barcode is detected or the Scan button is activated. Hide status bar on Scanner UI.
1 parent b71474f commit 10af6c8

File tree

4 files changed

+42
-18
lines changed

4 files changed

+42
-18
lines changed

OSBarcodeLib/Scanner/Interface Elements/OSBARCScanButton.swift

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,44 @@ struct OSBARCScanButton: View {
66
let action: () -> Void
77
/// Text to be shown on the button.
88
let text: String
9+
/// Indicates if the feature is enabled or not.
10+
let isOn: Bool
911

10-
/// Colour to be set to the text.
11-
private let foregroundColour: Color = OSBARCScannerViewConfigurationValues.mainColour
12-
/// The colour to display outside the text.
13-
private let backgroundColour: Color = OSBARCScannerViewConfigurationValues.secondaryColour
12+
/// Text colour to use when the button's feature is set to `on`
13+
private let selectedForegroundColour: Color = OSBARCScannerViewConfigurationValues.tertiaryColour
14+
/// Text colour to use when the button's feature is set to `off`
15+
private let notSelectedForegroundColour: Color = OSBARCScannerViewConfigurationValues.mainColour
16+
/// Background colour to use when the button's feature is set to `on`.
17+
private let selectedBackgroundColour: Color = OSBARCScannerViewConfigurationValues.mainColour
18+
/// Background colour to use when the button's feature is set to `off`.
19+
private let notSelectedBackgroundColour: Color = OSBARCScannerViewConfigurationValues.secondaryColour
1420
/// The colour of the button's outer line.
1521
private let overlayColour: Color = OSBARCScannerViewConfigurationValues.tertiaryColour
1622
/// Considering the button is displayed as a Rounded Rectangle, this is the radius of the button's vertices.
1723
private let cornerRadius: CGFloat = OSBARCScannerViewConfigurationValues.defaultRadius
1824
/// Width of the button's outer line.
1925
private let stroke: CGFloat = OSBARCScannerViewConfigurationValues.defaultLineStroke
2026

27+
/// Calculates the text colour to be used based on the `isOn` value.
28+
private var foregroundColour: Color { isOn ? selectedForegroundColour : notSelectedForegroundColour }
29+
/// Calculates the background colour to be used based on the `isOn` value.
30+
private var backgroundColour: Color { isOn ? selectedBackgroundColour : notSelectedBackgroundColour }
31+
2132
var body: some View {
2233
Button(action: action) {
2334
Text(text)
2435
.padding()
2536
.foregroundStyle(forColour: foregroundColour)
26-
.overlay(
27-
RoundedRectangle(cornerRadius: cornerRadius)
28-
.stroke(overlayColour, style: .init(lineWidth: stroke))
29-
)
3037
.background(
3138
RoundedRectangle(cornerRadius: cornerRadius)
3239
.foregroundColor(backgroundColour)
3340
)
41+
.if(!isOn) {
42+
$0.overlay(
43+
RoundedRectangle(cornerRadius: cornerRadius)
44+
.stroke(overlayColour, style: .init(lineWidth: stroke))
45+
)
46+
}
3447
}
3548
}
3649
}

OSBarcodeLib/Scanner/Interface Elements/OSBARCTorchButton.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,16 @@ struct OSBARCTorchButton: View {
2929
Button(action: action) {
3030
Image(iconName, bundle: .init(for: OSBARCScannerBehaviour.self)) // xcassets item.
3131
.frame(width: size, height: size)
32-
.background(Circle().foregroundStyle(forColour: backgroundColour))
32+
.background(
33+
Circle()
34+
.foregroundStyle(forColour: backgroundColour)
35+
)
3336
.if(!isOn) {
34-
$0.overlay(Circle().stroke(overlayColour, lineWidth: stroke))
37+
$0.overlay(
38+
Circle()
39+
.stroke(overlayColour, lineWidth: stroke)
40+
)
3541
}
36-
.foregroundStyle(forColour: .clear)
3742
}
3843
}
3944
}

OSBarcodeLib/Scanner/OSBARCScannerView.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,11 @@ struct OSBARCScannerView: View {
7777
/// Scanning Button. It needs to enabled to appear.
7878
private var scanButton: OSBARCScanButton {
7979
OSBARCScanButton(action: {
80-
buttonScanEnabled = true
81-
}, text: buttonText)
80+
buttonScanEnabled.toggle()
81+
if buttonScanEnabled {
82+
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
83+
}
84+
}, text: buttonText, isOn: buttonScanEnabled)
8285
}
8386

8487
/// The scanning zone, containing the aim and the hole.
@@ -237,6 +240,7 @@ struct OSBARCScannerView: View {
237240
}
238241
}
239242
.customIgnoreSafeArea()
243+
.statusBarHidden(true)
240244
}
241245
}
242246

OSBarcodeLib/Scanner/OSBARCScannerViewControllerCoordinator.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ final class OSBARCScannerViewControllerCoordinator: NSObject, AVCaptureVideoData
3333

3434
func captureOutput(_ output: AVCaptureOutput, didOutput sampleBuffer: CMSampleBuffer, from connection: AVCaptureConnection) {
3535
// Output should only be processed when scanning is automatically or it has been enabled through the Scan Button.
36-
guard !self.scanThroughButton || scanButtonEnabled else { return }
36+
guard !self.scanThroughButton || self.scanButtonEnabled else { return }
3737
guard let pixelBuffer = CMSampleBufferGetImageBuffer(sampleBuffer) else { return }
3838

3939
var requestOptions: [VNImageOption: Any] = [:]
@@ -48,19 +48,21 @@ final class OSBARCScannerViewControllerCoordinator: NSObject, AVCaptureVideoData
4848

4949
/// Vision request to perform. It gets initialised only once and when needed.
5050
lazy private var detectBarcodeRequest: VNDetectBarcodesRequest = {
51-
.init(completionHandler: { request, error in
51+
let barcodeRequest = VNDetectBarcodesRequest(completionHandler: { request, error in
5252
guard error == nil else { return }
5353
self.processClassification(for: request)
5454
})
55+
barcodeRequest.symbologies = self.barcodeTypes
56+
57+
return barcodeRequest
5558
}()
5659

5760
/// Processes the Vision request to return the desired barcode value.
5861
/// - Parameter request: Vision request handler that performs image analysis.
5962
private func processClassification(for request: VNRequest) {
6063
DispatchQueue.main.async {
61-
if let bestResult = request.results?.first as? VNBarcodeObservation,
62-
let payload = bestResult.payloadStringValue,
63-
self.barcodeTypes.contains(bestResult.symbology) {
64+
if let bestResult = request.results?.first as? VNBarcodeObservation, let payload = bestResult.payloadStringValue {
65+
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate)
6466
self.scanResult = payload
6567
}
6668
}

0 commit comments

Comments
 (0)