Skip to content

Commit

Permalink
Merge pull request #39 from clutter/cancel-scan
Browse files Browse the repository at this point in the history
Add Ability to Cancel a Scan
  • Loading branch information
Jeremy Grenier authored Aug 27, 2019
2 parents 4d32138 + 289187f commit d72892e
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- Sections (0.8.0)
- SplitScreenScanner (8.0.0):
- SplitScreenScanner (8.1.0):
- Sections
- SwiftLint (0.34.0)

Expand All @@ -19,7 +19,7 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Sections: efc268a207d0e7afba82d2a0efd6cdf61872b5d4
SplitScreenScanner: 2a229d138ead2d387ba9ddfe4844eed260949c69
SplitScreenScanner: acf69a6e230161d23dab9d50f1d2fd57797a6db2
SwiftLint: 79d48a17c6565dc286c37efb8322c7b450f95c67

PODFILE CHECKSUM: 662fda703b72b77c2ece8658c02a5b890176b006
Expand Down
36 changes: 35 additions & 1 deletion Example/Tests/ScanHistoryViewModelTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class ScanHistoryViewModelTests: XCTestCase {
func testSubsequentScan() {
let firstScan = ScanHistory(barcode: "0000000001", scanResult: .success(description: nil))
let secondScan = ScanHistory(barcode: "0000000002", scanResult: .error(description: "We no longer store abstract concepts of thought"))
let scans = [firstScan, secondScan]
let scans = [secondScan, firstScan]

setUpVM(scans: scans, isScanningSessionExpirable: true)

Expand Down Expand Up @@ -125,6 +125,40 @@ class ScanHistoryViewModelTests: XCTestCase {
XCTAssertFalse(reloadedSectionHeader)
}

func testDropMostRecentScan() {
let firstScan = ScanHistory(barcode: "0000000001", scanResult: .success(description: nil))
setUpVM(scans: [firstScan], isScanningSessionExpirable: true)

var rowReloadedIndexPath: IndexPath?
viewModel.reloadRowObserver = { indexPath in
rowReloadedIndexPath = indexPath
}

viewModel.cancelScannedBarcode(firstScan.barcode)
XCTAssertEqual(rowReloadedIndexPath, IndexPath(row: 0, section: 0))

let canceledScan = ScanHistory(barcode: firstScan.barcode, scanResult: .warning(description: "Scan Canceled"))
XCTAssertEqual(viewModel.sections[0].rows[0], .historyRow(barcode: canceledScan.barcode, scanResult: canceledScan.scanResult))
}

func testDropOlderScan() {
let firstScan = ScanHistory(barcode: "0000000001", scanResult: .success(description: nil))
let secondScan = ScanHistory(barcode: "0000000002", scanResult: .error(description: "We no longer store abstract concepts of thought"))
let scans = [secondScan, firstScan]
setUpVM(scans: scans, isScanningSessionExpirable: true)

var rowDeletedIndexPath: IndexPath?
viewModel.reloadRowObserver = { indexPath in
rowDeletedIndexPath = indexPath
}

viewModel.cancelScannedBarcode(firstScan.barcode)
XCTAssertEqual(rowDeletedIndexPath, IndexPath(row: 1, section: 0))

let canceledScan = ScanHistory(barcode: firstScan.barcode, scanResult: .warning(description: "Scan Canceled"))
XCTAssertEqual(viewModel.sections[0].rows[1], .historyRow(barcode: canceledScan.barcode, scanResult: canceledScan.scanResult))
}

func testCreateExpireSessionTimer() {
setUpVM(scans: [], isScanningSessionExpirable: true)

Expand Down
2 changes: 1 addition & 1 deletion SplitScreenScanner.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Pod::Spec.new do |s|
s.name = 'SplitScreenScanner'
s.version = '8.0.0'
s.version = '8.1.0'
s.summary = 'Swift library for scanning barcodes with half the screen devoted to scan history'

# This description is used to generate tags and improve search results.
Expand Down
23 changes: 23 additions & 0 deletions SplitScreenScanner/Classes/ScanHistoryViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ extension ScanHistoryViewModel {
}
}

func cancelScannedBarcode(_ barcode: String) {
cancelScan(with: barcode)
}

func createExpireSessionTimer() {
guard isScanningSessionExpirable else { return }

Expand Down Expand Up @@ -146,4 +150,23 @@ private extension ScanHistoryViewModel {
insertRowObserver?(firstRowIndexPath)
}
}

func cancelScan(with barcode: String) {
let firstIndex = scans.firstIndex { scan in
if case .success = scan.scanResult, scan.barcode == barcode {
return true
}
return false
}

guard let index = firstIndex else { return }

let scan = scans[index]
let newScan = ScanHistory(barcode: scan.barcode, scanResult: .warning(description: "Scan Canceled"))
scans[index] = newScan

let indexPath = IndexPath(row: index, section: 0)
sections[indexPath.section].rows[indexPath.row] = .historyRow(barcode: newScan.barcode, scanResult: newScan.scanResult)
reloadRowObserver?(indexPath)
}
}
4 changes: 4 additions & 0 deletions SplitScreenScanner/Classes/SplitScannerCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ extension SplitScannerCoordinator {
barcodeScannerViewModel?.unblockScanner()
}

public func cancelScannedBarcode(_ barcode: String) {
scanHistoryViewModel?.cancelScannedBarcode(barcode)
}

public func resetLastScannedBarcode() {
barcodeScannerViewModel?.resetLastScannedBarcode()
}
Expand Down

0 comments on commit d72892e

Please sign in to comment.