Skip to content

Commit

Permalink
Add location and dates to pass output
Browse files Browse the repository at this point in the history
  • Loading branch information
Arclite committed Oct 17, 2024
1 parent 04a6347 commit 6bdbe0c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 49 deletions.
33 changes: 11 additions & 22 deletions Sources/App/Pass.swift → Sources/App/Pass/Pass.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ struct Pass: ResponseEncodable {
return encoder
}()


static let dateFormatStyle = Date.ISO8601FormatStyle.iso8601

let description: String
let formatVersion = 1
let organizationName = "Cocoatype, LLC"
Expand All @@ -22,33 +25,19 @@ struct Pass: ResponseEncodable {

let barcodes: [Pass.Barcode]
let storeCard = Pass.StoreCard()
let locations: [Pass.Location]
let relevantDate: String?

init(_ request: PassRequest) {
self.description = request.title
self.logoText = request.title
self.barcodes = [Pass.Barcode(request.barcode)]
}
}

extension Pass {
struct StoreCard: ResponseEncodable {}
}

extension Pass {
struct Barcode: ResponseEncodable {
let format: String
let message: String
let messageEncoding = "utf-8"
self.locations = request.locations.map(Pass.Location.init)

init(_ request: PassRequest.Barcode) {
switch request {
case .qr(let message):
self.format = "PKBarcodeFormatQR"
self.message = message
case .code128(let message):
self.format = "PKBarcodeFormatCode128"
self.message = message
}
}
self.relevantDate = try? request.dates
.compactMap(Self.dateFormatStyle.parse(_:))
.sorted()
.first
.map(Self.dateFormatStyle.format(_:))
}
}
20 changes: 20 additions & 0 deletions Sources/App/Pass/PassBarcode.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Hummingbird

extension Pass {
struct Barcode: ResponseEncodable {
let format: String
let message: String
let messageEncoding = "utf-8"

init(_ request: PassRequest.Barcode) {
switch request {
case .qr(let message):
self.format = "PKBarcodeFormatQR"
self.message = message
case .code128(let message):
self.format = "PKBarcodeFormatCode128"
self.message = message
}
}
}
}
15 changes: 15 additions & 0 deletions Sources/App/Pass/PassLocation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Hummingbird

extension Pass {
struct Location: ResponseEncodable {
let latitude: Double
let longitude: Double
let relevantText: String?

init(requestLocation: PassRequest.Location) {
self.latitude = requestLocation.latitude
self.longitude = requestLocation.longitude
self.relevantText = requestLocation.name
}
}
}
5 changes: 5 additions & 0 deletions Sources/App/Pass/PassStoreCard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Hummingbird

extension Pass {
struct StoreCard: ResponseEncodable {}
}
29 changes: 29 additions & 0 deletions Sources/App/Request/PassRequest.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Foundation

struct PassRequest: Decodable {
let title: String
let barcode: PassRequest.Barcode
let locations: [PassRequest.Location]
let dates: [String]
}

enum PassRequestDecodeError: Error {
case unknownFormat(String)
}

/* Barc Supports
* EAN-13
* UPC-A
* Code 39
* Codabar
*/

/* Both Support
* QR
* Code 128
*/

/* Apple Wallet Supports
* Aztec
* PDF417
*/
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import Foundation

struct PassRequest: Decodable {
let title: String
let barcode: Barcode

extension PassRequest {
enum Barcode: Decodable {
case qr(String)
case code128(String)
Expand All @@ -28,24 +23,3 @@ struct PassRequest: Decodable {
}
}
}

enum PassRequestDecodeError: Error {
case unknownFormat(String)
}

/* Barc Supports
* EAN-13
* UPC-A
* Code 39
* Codabar
*/

/* Both Support
* QR
* Code 128
*/

/* Apple Wallet Supports
* Aztec
* PDF417
*/
7 changes: 7 additions & 0 deletions Sources/App/Request/PassRequestLocation.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
extension PassRequest {
struct Location: Decodable {
let latitude: Double
let longitude: Double
let name: String?
}
}

0 comments on commit 6bdbe0c

Please sign in to comment.