Skip to content

Commit 1bb6160

Browse files
committed
mrz filter selection, fixed crash on calling deallocated getter property
1 parent c2febc1 commit 1bb6160

3 files changed

Lines changed: 94 additions & 21 deletions

File tree

Samples/Advanced/DocumentReader-Swift/DocumentReader-Swift.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
8640EC3226F9C35700C8FF4F /* MRZFormat+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8640EC3126F9C35700C8FF4F /* MRZFormat+Extensions.swift */; };
1011
868341E726F0C11B00644618 /* DocFormat+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 868341E626F0C11B00644618 /* DocFormat+Extensions.swift */; };
1112
86878F0626EF96360082F481 /* NSObject+ClassName.swift in Sources */ = {isa = PBXBuildFile; fileRef = 86878F0526EF96360082F481 /* NSObject+ClassName.swift */; };
1213
9D00FDD225D2E3DA00D6FEF1 /* BarcodeTypes+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9D00FDD125D2E3DA00D6FEF1 /* BarcodeTypes+Extensions.swift */; };
@@ -56,6 +57,7 @@
5657
0BBE1365BE97A7AE1B68DF58 /* Pods-DocumentReader-Swift.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DocumentReader-Swift.release.xcconfig"; path = "Target Support Files/Pods-DocumentReader-Swift/Pods-DocumentReader-Swift.release.xcconfig"; sourceTree = "<group>"; };
5758
0DC5DA1B48C6A7B2425AF7F0 /* Pods_DocumentReader_Swift.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DocumentReader_Swift.framework; sourceTree = BUILT_PRODUCTS_DIR; };
5859
2391B685CCD628B0DFA19E1A /* Pods-DocumentReader-Swift.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DocumentReader-Swift.debug.xcconfig"; path = "Target Support Files/Pods-DocumentReader-Swift/Pods-DocumentReader-Swift.debug.xcconfig"; sourceTree = "<group>"; };
60+
8640EC3126F9C35700C8FF4F /* MRZFormat+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "MRZFormat+Extensions.swift"; sourceTree = "<group>"; };
5961
868341E626F0C11B00644618 /* DocFormat+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "DocFormat+Extensions.swift"; sourceTree = "<group>"; };
6062
86878F0526EF96360082F481 /* NSObject+ClassName.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSObject+ClassName.swift"; sourceTree = "<group>"; };
6163
9D00FDD125D2E3DA00D6FEF1 /* BarcodeTypes+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BarcodeTypes+Extensions.swift"; sourceTree = "<group>"; };
@@ -138,6 +140,7 @@
138140
9DFBD7D925E6689900428089 /* RFIDAccessControlProcedureType+Extensions.swift */,
139141
86878F0526EF96360082F481 /* NSObject+ClassName.swift */,
140142
868341E626F0C11B00644618 /* DocFormat+Extensions.swift */,
143+
8640EC3126F9C35700C8FF4F /* MRZFormat+Extensions.swift */,
141144
);
142145
path = Extensions;
143146
sourceTree = "<group>";
@@ -405,6 +408,7 @@
405408
9D658B0725CBF3EE00DDB739 /* ResultType+Extensions.swift in Sources */,
406409
9DFBD7DA25E6689900428089 /* RFIDAccessControlProcedureType+Extensions.swift in Sources */,
407410
9D9F7DB725D573620091C675 /* GetValuesViewController.swift in Sources */,
411+
8640EC3226F9C35700C8FF4F /* MRZFormat+Extensions.swift in Sources */,
408412
9D33E2A425CA940700FF9561 /* MainViewController.swift in Sources */,
409413
9D053B6F25CD6D770014EE23 /* FieldType+Extensions.swift in Sources */,
410414
9D33E2A025CA940700FF9561 /* AppDelegate.swift in Sources */,
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//
2+
// MRZFormat+Extensions.swift
3+
// DocumentReader-Swift
4+
//
5+
// Created by Pavel Kondrashkov on 9/21/21.
6+
// Copyright © 2021 Regula. All rights reserved.
7+
//
8+
9+
import DocumentReader
10+
11+
extension MRZFormat: CaseIterable {
12+
public typealias AllCases = [MRZFormat]
13+
14+
public static var allCases: [MRZFormat] = [
15+
.IDL,
16+
.ID1,
17+
.ID2,
18+
.ID3,
19+
.CAN,
20+
.ID1_2_30,
21+
]
22+
}
23+
24+
extension MRZFormat: LosslessStringConvertible {
25+
public var description: String {
26+
switch self {
27+
case .IDL: return "IDL"
28+
case .ID1: return "ID1"
29+
case .ID2: return "ID2"
30+
case .ID3: return "ID3"
31+
case .CAN: return "CAN"
32+
case .ID1_2_30: return "ID1_2_30"
33+
@unknown default:
34+
fatalError()
35+
}
36+
}
37+
38+
public init?(_ description: String) {
39+
guard let match = Self.allCases.first(where: { $0.description == description }) else {
40+
return nil
41+
}
42+
self.init(rawValue: match.rawValue)
43+
}
44+
}

Samples/Advanced/DocumentReader-Swift/DocumentReader-Swift/SettingsViewController/SettingsViewController.swift

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class SettingsViewController: UIViewController {
4242
tableView.tableFooterView = UIView(frame: .zero)
4343

4444
segmentedControl.selectedSegmentIndex = 0
45-
segmentedControl.addTarget(self, action: #selector(segmentedControlAction(_:)), for: .valueChanged)
45+
segmentedControl.addTarget(self, action: #selector(onSegmentControlValueChange(_:)), for: .valueChanged)
4646
navigationItem.titleView = segmentedControl
4747

4848
let resetBarButton = UIBarButtonItem(title: "Reset", style: .plain, target: self, action: #selector(resetButtonAction(_:)))
@@ -55,6 +55,9 @@ class SettingsViewController: UIViewController {
5555
case 0:
5656
ApplicationSettings.shared.isRfidEnabled = false
5757
ApplicationSettings.shared.useCustomRfidController = false
58+
59+
initApplicationSettings()
60+
sectionsData = applicationGroups
5861
case 1:
5962
let scenario = DocReader.shared.selectedScenario()
6063
let defaultProcessParams = ProcessParams()
@@ -63,14 +66,17 @@ class SettingsViewController: UIViewController {
6366
let defaultFunctionality = Functionality()
6467
ApplicationSettings.shared.functionality = defaultFunctionality
6568
DocReader.shared.functionality = defaultFunctionality
69+
70+
initAPISettings()
71+
sectionsData = apiGroups
6672
default:
6773
break
6874
}
6975
tableView.reloadData()
7076
}
7177

72-
@objc func segmentedControlAction(_ sender: UISegmentedControl) {
73-
switch sender.selectedSegmentIndex {
78+
@objc func onSegmentControlValueChange(_ sender: UISegmentedControl) {
79+
switch segmentedControl.selectedSegmentIndex {
7480
case 0:
7581
sectionsData = applicationGroups
7682
case 1:
@@ -82,6 +88,8 @@ class SettingsViewController: UIViewController {
8288
}
8389

8490
private func initApplicationSettings() {
91+
self.applicationGroups.removeAll()
92+
8593
let settings = ApplicationSettings.shared
8694

8795
// 1. RFID
@@ -97,6 +105,8 @@ class SettingsViewController: UIViewController {
97105
}
98106

99107
private func initAPISettings() {
108+
self.apiGroups.removeAll()
109+
100110
let functionality = DocReader.shared.functionality
101111
let params = DocReader.shared.processParams
102112

@@ -337,7 +347,7 @@ class SettingsViewController: UIViewController {
337347
guard let self = self else { return }
338348
let currentValue = params.forceDocFormat.map { $0.intValue }.flatMap { DocFormat(rawValue: $0)?.description } ?? "nil"
339349
let options: [String] = DocFormat.allCases.map { $0.description } + ["nil"]
340-
self.showOptionsPicker(title: "Force Doc Format", current: currentValue, options: options) { (result) in
350+
self.showOptionsPicker(title: "Force Doc Format", current: [currentValue], options: options) { (result) in
341351
let param = DocFormat(result).map { NSNumber(value: $0.rawValue) }
342352
DocReader.shared.processParams.forceDocFormat = param
343353
self.tableView.reloadData()
@@ -352,13 +362,24 @@ class SettingsViewController: UIViewController {
352362

353363
let mrzFormatsFilter = SettingsActionItem(title: "MRZ Formats Filter") { [weak self] in
354364
guard let self = self else { return }
355-
self.showIntegerArrayEditor(title: "MRZ Formats Filter", inputArray: DocReader.shared.processParams.mrzFormatsFilter as? [Int]) { output in
356-
DocReader.shared.processParams.mrzFormatsFilter = output
365+
let currentValue = params.mrzFormatsFilter?.map { $0.intValue }.compactMap { MRZFormat(rawValue: $0)?.description } ?? []
366+
let options: [String] = MRZFormat.allCases.map { $0.description }
367+
self.showOptionsPicker(title: "MRZ Formats Filter", current: currentValue, options: options) { (result) in
368+
guard let param = MRZFormat(result).map({ NSNumber(value: $0.rawValue) }) else { return }
369+
370+
var filters = params.mrzFormatsFilter ?? []
371+
if let paramIndex = filters.firstIndex(of: param) {
372+
filters.remove(at: paramIndex)
373+
} else {
374+
filters.append(param)
375+
}
376+
377+
DocReader.shared.processParams.mrzFormatsFilter = filters
357378
self.tableView.reloadData()
358379
}
359380
} state: {
360381
let currentList = DocReader.shared.processParams.mrzFormatsFilter ?? []
361-
let value = currentList.compactMap { $0.stringValue }.joined(separator: ", ")
382+
let value = currentList.compactMap { MRZFormat(rawValue: $0.intValue)?.description }.joined(separator: ", ")
362383
return value.isEmpty ? "nil" : value
363384
}
364385

@@ -534,20 +555,24 @@ class SettingsViewController: UIViewController {
534555
present(actionSheet, animated: true, completion: nil)
535556
}
536557

537-
private func showOptionsPicker(title: String, current: String, options: [String], completion: @escaping (String) -> Void) {
538-
let actionSheet = UIAlertController(title: nil, message: title, preferredStyle: self.alertStyleForDevice())
539-
for option in options {
540-
let action = UIAlertAction(title: option, style: .default) { _ in
541-
completion(option)
542-
}
543-
if option == current {
544-
action.setValue(true, forKey: "checked")
545-
}
546-
actionSheet.addAction(action)
547-
}
548-
actionSheet.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
549-
present(actionSheet, animated: true, completion: nil)
550-
}
558+
private func showOptionsPicker(title: String, current: [String], options: [String], completion: @escaping (String) -> Void) {
559+
let actionSheet = UIAlertController(title: nil, message: title, preferredStyle: self.alertStyleForDevice())
560+
561+
for option in options {
562+
let action = UIAlertAction(title: option, style: .default) { _ in
563+
completion(option)
564+
}
565+
566+
for selected in current where selected == option {
567+
action.setValue(true, forKey: "checked")
568+
}
569+
570+
actionSheet.addAction(action)
571+
}
572+
573+
actionSheet.addAction(UIAlertAction.init(title: "Cancel", style: .cancel, handler: nil))
574+
present(actionSheet, animated: true, completion: nil)
575+
}
551576

552577
private func showCaptureModeList(_ completion: VoidClosure? = nil) {
553578
let modes: [CaptureMode] = [.auto, .captureVideo, .captureFrame]

0 commit comments

Comments
 (0)