Skip to content

Commit

Permalink
Show certificate type.
Browse files Browse the repository at this point in the history
  • Loading branch information
yspreen committed Apr 21, 2021
1 parent 48d3402 commit 2d15019
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 34 deletions.
17 changes: 17 additions & 0 deletions PatientScannerDemo/Extensions/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,25 @@ extension Date {
}

static let isoFormatter = formatter(for: "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
static let dateFormatter = formatter(for: "yyyy-MM-dd")

var isoString: String {
Date.isoFormatter.string(from: self)
}
var dateString: String {
Date.dateFormatter.string(from: self)
}

init?(isoString: String) {
guard let date = Date.isoFormatter.date(from: isoString) else {
return nil
}
self = date
}
init?(dateString: String) {
guard let date = Date.dateFormatter.date(from: dateString) else {
return nil
}
self = date
}
}
106 changes: 97 additions & 9 deletions PatientScannerDemo/Models/HCert.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,42 @@ enum ClaimKey: String {
enum AttributeKey: String {
case firstName
case lastName
case gender
case dateOfBirth
case testStatements
case vaccineStatements
case recoveryStatements
case personIdentifiers
case identifierType = "t"
case identifierCountry = "c"
case identifierValue = "i"
case vaccineShotNo = "seq"
case vaccineShotTotal = "tot"
}

enum HCertType: String {
case test = "Test"
case vaccineOne = "Vaccine Shot #1"
case vaccineTwo = "Vaccine Shot #2"
case recovery = "Recovery"
}

let identifierNames: [String: String] = [
"PP": "Passport Number",
"NN": "National Person Identifier",
"CZ": "Citizenship Card Number",
"HC": "Health Card Number",
]

let attributeKeys: [AttributeKey: [String]] = [
.firstName: ["sub", "gn"],
.lastName: ["sub", "fn"],
.gender: ["sub", "gen"],
.dateOfBirth: ["sub", "dob"],
.personIdentifiers: ["sub", "id"],
.testStatements: ["tst"],
.vaccineStatements: ["vac"],
.recoveryStatements: ["rec"],
]

struct InfoSection {
Expand Down Expand Up @@ -83,6 +114,20 @@ struct HCert {
}
}

func get(_ attribute: AttributeKey) -> JSON {
var object = body
for key in attributeKeys[attribute] ?? [] {
object = object[key]
}
return object
}

var info: [InfoSection] {
[
InfoSection(header: "Certificate Type", content: type.rawValue)
] + personIdentifiers
}

var header: JSON
var body: JSON

Expand All @@ -92,17 +137,60 @@ struct HCert {
return "\(first) \(last)"
}

func get(_ attribute: AttributeKey) -> JSON {
var object = body
for key in attributeKeys[attribute] ?? [] {
object = object[key]
var dateOfBirth: Date? {
guard let dateString = get(.dateOfBirth).string else {
return nil
}
return object
return Date(dateString: dateString)
}

var info: [InfoSection] {
[
InfoSection(header: "Test", content: "Test Test")
]
var personIdentifiers: [InfoSection] {
guard let identifiers = get(.personIdentifiers).array else {
return []
}
return identifiers.map {
let type = $0[AttributeKey.identifierType.rawValue].string ?? ""
let country = $0[AttributeKey.identifierCountry.rawValue].string
let value = $0[AttributeKey.identifierValue.rawValue].string ?? ""

var header = identifierNames[type] ?? "Unknown Identifier"
if let country = country {
header += " (\(country))"
}

return InfoSection(header: header, content: value)
}
}

var testStatements: [JSON] {
return get(.testStatements).array ?? []
}
var vaccineStatements: [JSON] {
return get(.vaccineStatements).array ?? []
}
var recoveryStatements: [JSON] {
return get(.recoveryStatements).array ?? []
}
var hasLastShot: Bool {
for statement in vaccineStatements {
let no = statement[AttributeKey.vaccineShotNo.rawValue].int ?? 1
let total = statement[AttributeKey.vaccineShotTotal.rawValue].int ?? 2
if no == total {
return true
}
}
return false
}
var type: HCertType {
if hasLastShot {
return .vaccineTwo
}
if !vaccineStatements.isEmpty {
return .vaccineOne
}
if !recoveryStatements.isEmpty {
return .recovery
}
return .test
}
}
23 changes: 3 additions & 20 deletions PatientScannerDemo/Storyboards/CertificateViewer.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,6 @@
<color key="textColor" red="0.37632042253521125" green="1" blue="0.54548555866148907" alpha="1" colorSpace="calibratedRGB"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Identification Number" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="shh-mW-RSP">
<rect key="frame" x="16" y="246.33333333333329" width="173.33333333333334" height="21.666666666666657"/>
<fontDescription key="fontDescription" type="system" pointSize="18"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="DE4382974329" textAlignment="natural" lineBreakMode="tailTruncation" numberOfLines="0" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Rk4-u2-ei3">
<rect key="frame" x="16" y="272" width="343" height="38.333333333333314"/>
<fontDescription key="fontDescription" type="system" weight="medium" pointSize="32"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<segmentedControl opaque="NO" userInteractionEnabled="NO" contentMode="scaleToFill" contentHorizontalAlignment="left" contentVerticalAlignment="top" segmentControlStyle="plain" selectedSegmentIndex="0" translatesAutoresizingMaskIntoConstraints="NO" id="9hO-yQ-B4J">
<rect key="frame" x="16" y="199.33333333333334" width="343" height="32"/>
<segments>
Expand All @@ -77,8 +65,8 @@
</segments>
<color key="selectedSegmentTintColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</segmentedControl>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" contentInsetAdjustmentBehavior="never" dataMode="prototypes" style="plain" separatorStyle="default" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="NIJ-V3-NpL">
<rect key="frame" x="0.0" y="318.33333333333326" width="375" height="328.66666666666674"/>
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" contentInsetAdjustmentBehavior="never" dataMode="prototypes" style="plain" separatorStyle="none" rowHeight="-1" estimatedRowHeight="-1" sectionHeaderHeight="28" sectionFooterHeight="28" translatesAutoresizingMaskIntoConstraints="NO" id="NIJ-V3-NpL">
<rect key="frame" x="0.0" y="246.33333333333329" width="375" height="400.66666666666674"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<color key="sectionIndexBackgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<prototypes>
Expand Down Expand Up @@ -123,24 +111,19 @@
</subviews>
<constraints>
<constraint firstAttribute="trailing" secondItem="9hO-yQ-B4J" secondAttribute="trailing" constant="16" id="1gH-1K-TaY"/>
<constraint firstItem="shh-mW-RSP" firstAttribute="top" secondItem="9hO-yQ-B4J" secondAttribute="bottom" constant="16" id="4W6-fQ-RkN"/>
<constraint firstItem="DVW-2y-e4I" firstAttribute="top" secondItem="yEe-AH-rsr" secondAttribute="top" constant="32" id="6Py-fR-Th3"/>
<constraint firstAttribute="trailing" secondItem="DVW-2y-e4I" secondAttribute="trailing" constant="16" id="7qh-Db-iAm"/>
<constraint firstAttribute="bottom" secondItem="a9u-Ia-KEX" secondAttribute="bottom" constant="4" id="99V-nH-ZeI"/>
<constraint firstItem="shh-mW-RSP" firstAttribute="leading" secondItem="yEe-AH-rsr" secondAttribute="leading" constant="16" id="Gtl-Ej-tkw"/>
<constraint firstItem="NIJ-V3-NpL" firstAttribute="top" secondItem="9hO-yQ-B4J" secondAttribute="bottom" constant="16" id="GFZ-yy-x5z"/>
<constraint firstItem="NIJ-V3-NpL" firstAttribute="leading" secondItem="yEe-AH-rsr" secondAttribute="leading" id="SsD-py-p7P"/>
<constraint firstAttribute="trailing" secondItem="NIJ-V3-NpL" secondAttribute="trailing" id="bMe-Iq-2qV"/>
<constraint firstItem="9hO-yQ-B4J" firstAttribute="top" secondItem="NjI-L7-yat" secondAttribute="bottom" constant="16" id="frU-2J-Kdb"/>
<constraint firstItem="DVW-2y-e4I" firstAttribute="leading" secondItem="yEe-AH-rsr" secondAttribute="leading" constant="16" id="iQB-29-cRZ"/>
<constraint firstItem="a9u-Ia-KEX" firstAttribute="centerX" secondItem="yEe-AH-rsr" secondAttribute="centerX" id="j6y-x9-na3"/>
<constraint firstItem="Rk4-u2-ei3" firstAttribute="leading" secondItem="yEe-AH-rsr" secondAttribute="leading" constant="16" id="jLL-GJ-mV7"/>
<constraint firstItem="NjI-L7-yat" firstAttribute="leading" secondItem="yEe-AH-rsr" secondAttribute="leading" constant="16" id="jha-Fk-syh"/>
<constraint firstItem="NjI-L7-yat" firstAttribute="top" secondItem="DVW-2y-e4I" secondAttribute="bottom" constant="8" id="lP1-ux-6At"/>
<constraint firstItem="9hO-yQ-B4J" firstAttribute="leading" secondItem="yEe-AH-rsr" secondAttribute="leading" constant="16" id="mYn-Jw-TYN"/>
<constraint firstAttribute="trailing" secondItem="Rk4-u2-ei3" secondAttribute="trailing" constant="16" id="mqO-vV-psd"/>
<constraint firstAttribute="trailing" secondItem="NjI-L7-yat" secondAttribute="trailing" constant="16" id="ox4-at-JzV"/>
<constraint firstItem="NIJ-V3-NpL" firstAttribute="top" secondItem="Rk4-u2-ei3" secondAttribute="bottom" constant="8" id="uXx-lV-Dnj"/>
<constraint firstItem="Rk4-u2-ei3" firstAttribute="top" secondItem="shh-mW-RSP" secondAttribute="bottom" constant="4" id="vIy-z6-z49"/>
<constraint firstItem="a9u-Ia-KEX" firstAttribute="top" secondItem="NIJ-V3-NpL" secondAttribute="bottom" constant="32" id="wZx-rx-Vmt"/>
</constraints>
</view>
Expand Down
6 changes: 6 additions & 0 deletions PatientScannerDemo/ViewControllers/CertificateViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ class CertificateViewerVC: UIViewController {
func draw() {
nameLabel.text = hCert.fullName
infoTable.reloadData()
typeSegments.selectedSegmentIndex = [
HCertType.test,
HCertType.vaccineOne,
HCertType.vaccineTwo,
HCertType.recovery
].firstIndex(of: hCert.type) ?? 0
}

override func viewDidLoad() {
Expand Down
10 changes: 5 additions & 5 deletions PatientScannerDemo/ViewControllers/Scan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class ScanVC: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// checkPermissions()
// setupCameraLiveView()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
self.observationHandler(payloadS: nil)
}
checkPermissions()
setupCameraLiveView()
// DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
// self.observationHandler(payloadS: nil)
// }
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
Expand Down

0 comments on commit 2d15019

Please sign in to comment.