Skip to content

Commit 61abcde

Browse files
committed
Update barcode scanning to use specific HIDBarcodeParserError type
- Change Result<String, Error> to Result<String, HIDBarcodeParserError> throughout barcode scanning chain - Update GameControllerBarcodeParser.asResult to return specific error type - Update scan tester to handle specific error type with error.barcode property - Update tests to use HIDBarcodeParserError instead of generic TestError
1 parent eae2f57 commit 61abcde

File tree

8 files changed

+19
-27
lines changed

8 files changed

+19
-27
lines changed

WooCommerce/Classes/POS/Models/PointOfSaleAggregateModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ protocol PointOfSaleAggregateModelProtocol {
3434
var couponsSearchController: PointOfSaleSearchingItemsControllerProtocol { get }
3535

3636
var cart: Cart { get }
37-
func barcodeScanned(_ result: Result<String, Error>)
37+
func barcodeScanned(_ result: Result<String, HIDBarcodeParserError>)
3838
func addToCart(_ item: POSItem)
3939
func remove(cartItem: CartItem)
4040
func removeAllItemsFromCart(types: [CartItemType])
@@ -185,7 +185,7 @@ extension PointOfSaleAggregateModel {
185185
// MARK: - Barcode Scanning
186186
@available(iOS 17.0, *)
187187
extension PointOfSaleAggregateModel {
188-
func barcodeScanned(_ result: Result<String, Error>) {
188+
func barcodeScanned(_ result: Result<String, HIDBarcodeParserError>) {
189189
Task { @MainActor [weak self] in
190190
guard let self else { return }
191191
switch result {

WooCommerce/Classes/POS/Presentation/Barcode Scanner Setup/PointOfSaleBarcodeScannerSetupScanTester.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,14 @@ struct PointOfSaleBarcodeScannerSetupScanTester {
2020
barcodeDefinition.barcodeAsset
2121
}
2222

23-
func handleScan(_ scanResult: Result<String, Error>) {
23+
func handleScan(_ scanResult: Result<String, HIDBarcodeParserError>) {
2424
switch scanResult {
2525
case .success(barcodeDefinition.expectedValue):
2626
onTestPass()
2727
case .success(let scannedValue):
2828
onTestFailure(scannedValue)
29-
case .failure(HIDBarcodeParserError.scanTooShort(barcode: let scannedValue)):
30-
onTestFailure(scannedValue)
31-
case .failure(HIDBarcodeParserError.timedOut(barcode: let scannedValue)):
32-
onTestFailure(scannedValue)
33-
case .failure:
34-
onTestFailure("")
29+
case .failure(let error):
30+
onTestFailure(error.barcode)
3531
}
3632
}
3733

WooCommerce/Classes/POS/Presentation/Barcode Scanning/BarcodeScannerContainer.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ struct BarcodeScannerContainer: View {
1111
/// Configuration for the barcode scanner
1212
let configuration: HIDBarcodeParserConfiguration
1313
/// Callback that is triggered when a barcode scan completes (success or failure)
14-
let onScan: (Result<String, Error>) -> Void
14+
let onScan: (Result<String, HIDBarcodeParserError>) -> Void
1515

1616
init(
1717
configuration: HIDBarcodeParserConfiguration = .default,
18-
onScan: @escaping (Result<String, Error>) -> Void
18+
onScan: @escaping (Result<String, HIDBarcodeParserError>) -> Void
1919
) {
2020
self.configuration = configuration
2121
self.onScan = onScan
@@ -37,7 +37,7 @@ struct BarcodeScannerContainer: View {
3737
/// keyboard input for barcode scanning.
3838
struct BarcodeScannerContainerRepresentable: UIViewControllerRepresentable {
3939
let configuration: HIDBarcodeParserConfiguration
40-
let onScan: (Result<String, Error>) -> Void
40+
let onScan: (Result<String, HIDBarcodeParserError>) -> Void
4141

4242
func makeUIViewController(context: Context) -> UIViewController {
4343
return GameControllerBarcodeScannerHostingController(
@@ -56,7 +56,7 @@ final class GameControllerBarcodeScannerHostingController: UIHostingController<E
5656

5757
init(
5858
configuration: HIDBarcodeParserConfiguration,
59-
onScan: @escaping (Result<String, Error>) -> Void
59+
onScan: @escaping (Result<String, HIDBarcodeParserError>) -> Void
6060
) {
6161
super.init(rootView: EmptyView())
6262

WooCommerce/Classes/POS/Presentation/Barcode Scanning/BarcodeScanningModifier.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ struct BarcodeScanningModifier: ViewModifier {
77
/// Whether barcode scanning is enabled
88
@Binding var enabled: Bool
99
/// Callback that is triggered when a barcode is successfully scanned
10-
let onScan: (Result<String, Error>) -> Void
10+
let onScan: (Result<String, HIDBarcodeParserError>) -> Void
1111

1212
private var isBarcodeScani1FeatureEnabled: Bool {
1313
ServiceLocator.featureFlagService.isFeatureFlagEnabled(.pointOfSaleBarcodeScanningi1)
@@ -32,7 +32,7 @@ extension View {
3232
/// - onScan: Callback that is triggered when a barcode is successfully scanned.
3333
/// - Returns: A view with barcode scanning capability.
3434
func barcodeScanning(enabled: Binding<Bool> = .constant(true),
35-
onScan: @escaping (Result<String, Error>) -> Void) -> some View {
35+
onScan: @escaping (Result<String, HIDBarcodeParserError>) -> Void) -> some View {
3636
modifier(BarcodeScanningModifier(enabled: enabled, onScan: onScan))
3737
}
3838
}

WooCommerce/Classes/POS/Presentation/Barcode Scanning/GameControllerBarcodeObserver.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import GameController
99
///
1010
final class GameControllerBarcodeObserver {
1111
/// A closure that is called when a barcode scan is completed.
12-
/// The result will be a `success` with the barcode string or a `failure` with an error.
13-
let onScan: (Result<String, Error>) -> Void
12+
/// The result will be a `success` with the barcode string or a `failure` with an HIDBarcodeParserError.
13+
let onScan: (Result<String, HIDBarcodeParserError>) -> Void
1414

1515
/// Track the coalesced keyboard and its parser
1616
/// According to Apple's documentation, all connected keyboards are coalesced into one keyboard object
@@ -27,7 +27,7 @@ final class GameControllerBarcodeObserver {
2727
/// - Parameters:
2828
/// - configuration: The configuration to use for the barcode parser. Defaults to the standard configuration.
2929
/// - onScan: The closure to be called when a scan is completed.
30-
init(configuration: HIDBarcodeParserConfiguration = .default, onScan: @escaping (Result<String, Error>) -> Void) {
30+
init(configuration: HIDBarcodeParserConfiguration = .default, onScan: @escaping (Result<String, HIDBarcodeParserError>) -> Void) {
3131
self.onScan = onScan
3232
self.configuration = configuration
3333
addObservers()

WooCommerce/Classes/POS/Presentation/Barcode Scanning/GameControllerBarcodeParser.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ enum HIDBarcodeParserResult {
253253
case success(barcode: String, scanDurationMs: Int)
254254
case failure(error: HIDBarcodeParserError, scanDurationMs: Int)
255255

256-
var asResult: Result<String, Error> {
256+
var asResult: Result<String, HIDBarcodeParserError> {
257257
switch self {
258258
case .success(let barcode, _):
259259
return .success(barcode)

WooCommerce/WooCommerceTests/POS/Mocks/MockPointOfSaleAggregateModel.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ final class MockPointOfSaleAggregateModel: PointOfSaleAggregateModelProtocol {
6262

6363
var cart: Cart = .init()
6464

65-
func barcodeScanned(_ result: Result<String, Error>) { }
65+
func barcodeScanned(_ result: Result<String, HIDBarcodeParserError>) { }
6666

6767
func addToCart(_ item: POSItem) { }
6868

WooCommerce/WooCommerceTests/POS/Presentation/Barcode Scanner Setup/PointOfSaleBarcodeScannerSetupScanTesterTests.swift

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,14 @@ struct PointOfSaleBarcodeScannerSetupScanTesterTests {
7070
onTestTimeout: { onTestTimeoutCalled = true },
7171
barcodeDefinition: expectedBarcode)
7272

73-
// When the scan fails
74-
sut.handleScan(.failure(TestError.scanFailed))
73+
// When the scan fails with scanTooShort error
74+
sut.handleScan(.failure(HIDBarcodeParserError.scanTooShort(barcode: "short")))
7575

7676
// Then it calls the failure closure
7777
#expect(onTestPassCalled == false)
7878
#expect(onTestFailureCalled == true)
7979
#expect(onTestTimeoutCalled == false)
80-
#expect(receivedScanValue == "")
81-
}
82-
83-
private enum TestError: Error {
84-
case scanFailed
80+
#expect(receivedScanValue == "short")
8581
}
8682

8783
@Test func test_scanTester_provides_correct_barcode_asset() {

0 commit comments

Comments
 (0)