Skip to content

Commit

Permalink
Feature/app version cell (#31)
Browse files Browse the repository at this point in the history
* More forgiving date format.

* More descriptive errors.

* Add KID check.

* Extend date format.

* Add AppVersionCell.

* Add border-less cell.
  • Loading branch information
yspreen authored May 16, 2021
1 parent 79c315e commit 0792980
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 39 deletions.
43 changes: 43 additions & 0 deletions Sources/Components/AppVersionCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*-
* ---license-start
* eu-digital-green-certificates / dgca-app-core-ios
* ---
* Copyright (C) 2021 T-Systems International GmbH and all other contributors
* ---
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---license-end
*/
//
// AppVersionCell.swift
//
//
// Created by Yannick Spreen on 5/16/21.
//

#if os(iOS)
import Foundation
import UIKit

public class AppVersionCell: UITableViewCell {
@IBOutlet weak var versionLabel: UILabel!

public override func layoutSubviews() {
super.layoutSubviews()

let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String
let format = l10n("app-version")
versionLabel.text = String(format: format, version ?? "?")
removeSectionSeparator()
}
}
#endif
34 changes: 20 additions & 14 deletions Sources/Extensions/Date.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,24 +68,30 @@ extension Date {
self = date
}
init?(rfc3339DateTimeString str: String) {
var str = str
let rfc3339DateTimeFormatter = DateFormatter()

rfc3339DateTimeFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ssZZZZZ"
if let date = rfc3339DateTimeFormatter.date(from: str) {
self = date
return
if (try? NSRegularExpression(
pattern: "\\.[0-9]{6}Z?$", options: []
).matches(
in: str, options: [], range: NSRange(
str.startIndex..<str.endIndex,
in: str
)
))?.isEmpty == false {
str = str.trimmingCharacters(in: ["Z"])
str = String(str.prefix(str.count - 3)) + "Z"
}

rfc3339DateTimeFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ"
if let date = rfc3339DateTimeFormatter.date(from: str) {
self = date
return
}

rfc3339DateTimeFormatter.dateFormat = "yyyy-MM-dd't'HH:mm:ss.SSS'z'"
if let date = rfc3339DateTimeFormatter.date(from: str) {
self = date
return
for fmt in [
"yyyy-MM-dd'T'HH:mm:ssZZZZZ", "yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
"yyyy-MM-dd't'HH:mm:ss'z'", "yyyy-MM-dd't'HH:mm:ss.SSS'z'"
] {
rfc3339DateTimeFormatter.dateFormat = fmt
if let date = rfc3339DateTimeFormatter.date(from: str) {
self = date
return
}
}
return nil
}
Expand Down
50 changes: 50 additions & 0 deletions Sources/Extensions/UITableViewCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*-
* ---license-start
* eu-digital-green-certificates / dgca-app-core-ios
* ---
* Copyright (C) 2021 T-Systems International GmbH and all other contributors
* ---
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ---license-end
*/
//
// UITableViewCell.swift
//
//
// Created by Yannick Spreen on 5/16/21.
//

#if os(iOS)
import UIKit

public extension UITableViewCell {
func removeSectionSeparator() {
for subview in subviews {
if
subview != contentView,
abs(subview.frame.width - frame.width) <= 0.1,
subview.frame.height < 2
{
subview.alpha = 0
}
}
}
}

public class BorderLessSectionCell: UITableViewCell {
public override func layoutSubviews() {
super.layoutSubviews()
removeSectionSeparator()
}
}
#endif
4 changes: 4 additions & 0 deletions Sources/Services/Localization.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ public func country(for code: String) -> String {

class L10N {
static var bundle: Bundle {
#if SWIFT_PACKAGE
Bundle.module
#else
Bundle(for: Self.self)
#endif
}
}
59 changes: 34 additions & 25 deletions Tests/SwiftDGCTests/SwiftDGCTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,34 @@ final class SwiftDGCTests: XCTestCase {
for error in errors {
switch error {
case .base45:
XCTAssert(!expB45decode, "unexpected base45 err for \(descr)")
XCTAssert(expB45decode != true, "unexpected base45 err for \(descr)")
case .prefix:
XCTAssert(!expUnprefix, "unexpected prefix err for \(descr)")
XCTAssert(expUnprefix != true, "unexpected prefix err for \(descr)")
case .zlib:
XCTAssert(!expCompression, "unexpected zlib err for \(descr)")
XCTAssert(expCompression != true, "unexpected zlib err for \(descr)")
case .cbor:
XCTAssert(!expDecode, "unexpected cbor err for \(descr)")
XCTAssert(expDecode != true, "unexpected cbor err for \(descr)")
case .json(error: let error):
XCTAssert(!expSchemaValidation, "unexpected schema err for \(descr): \(error)")
XCTAssert(expSchemaValidation != true, "unexpected schema err for \(descr): \(error)")
case .version:
XCTAssert(!expSchemaValidation, "unexpected version err for \(descr)")
XCTAssert(expSchemaValidation != true, "unexpected version err for \(descr)")
}
}
guard let hcert = hcert else {
return
}
checkHcert(hcert: hcert)
}

func checkHcert(hcert: HCert) {
let kidMatches = hcert.kidStr == KID.string(from: KID.from(certString ?? ""))
let valid = kidMatches && hcert.cryptographicallyValid

if expVerify == true {
XCTAssert(hcert.cryptographicallyValid, "cose signature invalid for \(descr)")
XCTAssert(kidMatches, "cose KID mismatch for \(descr)")
} else if expVerify == false {
XCTAssert(!hcert.cryptographicallyValid, "cose signature valid for \(descr)")
XCTAssert(!valid, "cose signature valid for \(descr)")
}
if expExpired == true {
XCTAssert(clock != nil, "clock not set for \(descr)")
Expand All @@ -147,7 +154,6 @@ final class SwiftDGCTests: XCTestCase {

var json: JSON?

// TODO: Should probably check for nil values and ignore those errors.
var payloadString: String? {
json?["PREFIX"].string
}
Expand All @@ -159,40 +165,43 @@ final class SwiftDGCTests: XCTestCase {
}
var fileName: String?
var descr: String {
context["DESCRIPTION"].string ?? fileName ?? ""
if let description = context["DESCRIPTION"].string {
return "\(fileName ?? "") (\(description))"
}
return "\(fileName ?? "")"
}
var clock: Date? {
Date(rfc3339DateTimeString: context["VALIDATIONCLOCK"].string ?? "")
}
var expected: JSON {
json?["EXPECTEDRESULTS"] ?? .null
}
var expValidObject: Bool {
expected["EXPECTEDVALIDOBJECT"].bool ?? true
var expValidObject: Bool? {
expected["EXPECTEDVALIDOBJECT"].bool
}
var expSchemaValidation: Bool {
expected["EXPECTEDSCHEMAVALIDATION"].bool ?? true
var expSchemaValidation: Bool? {
expected["EXPECTEDSCHEMAVALIDATION"].bool
}
var expDecode: Bool {
expected["EXPECTEDDECODE"].bool ?? true
var expDecode: Bool? {
expected["EXPECTEDDECODE"].bool
}
var expVerify: Bool? {
expected["EXPECTEDVERIFY"].bool
}
var expUnprefix: Bool {
expected["EXPECTEDUNPREFIX"].bool ?? true
var expUnprefix: Bool? {
expected["EXPECTEDUNPREFIX"].bool
}
var expValidJson: Bool {
expected["EXPECTEDVALIDJSON"].bool ?? true
var expValidJson: Bool? {
expected["EXPECTEDVALIDJSON"].bool
}
var expCompression: Bool {
expected["EXPECTEDCOMPRESSION"].bool ?? true
var expCompression: Bool? {
expected["EXPECTEDCOMPRESSION"].bool
}
var expB45decode: Bool {
expected["EXPECTEDB45DECODE"].bool ?? true
var expB45decode: Bool? {
expected["EXPECTEDB45DECODE"].bool
}
var expPicturedecode: Bool {
expected["EXPECTEDPICTUREDECODE"].bool ?? true
var expPicturedecode: Bool? {
expected["EXPECTEDPICTUREDECODE"].bool
}
var expExpired: Bool? {
expected["EXPECTEDEXPIRATIONCHECK"].bool
Expand Down

0 comments on commit 0792980

Please sign in to comment.