Skip to content
Merged
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
6 changes: 3 additions & 3 deletions WooCommerce/Classes/POS/Models/Cart+BarcodeScanError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,9 @@ extension PointOfSaleBarcodeScanError {
)

static let incompleteScan = NSLocalizedString(
"pointOfSale.barcodeScan.error.incompleteScan",
value: "Partial barcode scan",
comment: "Error message shown when scan is incomplete."
"pointOfSale.barcodeScan.error.incompleteScan.2",
value: "The scanner did not send an end-of-line character",
comment: "Error message shown when scanner times out without sending end-of-line character."
)

static let parsingError = NSLocalizedString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class GameControllerBarcodeParser {
private var buffer = ""
private var lastKeyPressTime: Date?
private var scanStartTime: Date?
private var timeoutTimer: Timer?

init(configuration: HIDBarcodeParserConfiguration,
onScan: @escaping (HIDBarcodeParserResult) -> Void,
Expand Down Expand Up @@ -49,6 +50,7 @@ final class GameControllerBarcodeParser {
}

buffer.append(character)
scheduleTimeoutTimer()
}
}

Expand Down Expand Up @@ -186,6 +188,31 @@ final class GameControllerBarcodeParser {
buffer = ""
lastKeyPressTime = nil
scanStartTime = nil
cancelTimeoutTimer()
}

private func scheduleTimeoutTimer() {
cancelTimeoutTimer()
timeoutTimer = timeProvider.scheduleTimer(
timeInterval: configuration.maximumInterCharacterTime,
target: self,
selector: #selector(handleTimeoutExpiry)
)
}

private func cancelTimeoutTimer() {
timeoutTimer?.invalidate()
timeoutTimer = nil
}

@objc private func handleTimeoutExpiry() {
guard !buffer.isEmpty else { return }

let scanDurationMs = calculateScanDurationMs()
let result = HIDBarcodeParserResult.failure(error: HIDBarcodeParserError.timedOut(barcode: buffer), scanDurationMs: scanDurationMs)

onScan(result)
resetScan()
}

private func calculateScanDurationMs() -> Int {
Expand All @@ -194,6 +221,7 @@ final class GameControllerBarcodeParser {
}

private func processScan() {
cancelTimeoutTimer()
checkForTimeoutBetweenKeystrokes()
let scanDurationMs = calculateScanDurationMs()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@ import Foundation

protocol TimeProvider {
func now() -> Date
func scheduleTimer(timeInterval: TimeInterval, target: Any, selector: Selector) -> Timer
}

struct DefaultTimeProvider: TimeProvider {
func now() -> Date {
Date()
}

func scheduleTimer(timeInterval: TimeInterval, target: Any, selector: Selector) -> Timer {
return Timer.scheduledTimer(timeInterval: timeInterval, target: target, selector: selector, userInfo: nil, repeats: false)
}
}
Loading