This repository was archived by the owner on Jan 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathSale.swift
More file actions
57 lines (48 loc) · 2.28 KB
/
Sale.swift
File metadata and controls
57 lines (48 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import UIKit
import SwiftyJSON
@objcMembers final class Sale: NSObject, JSONAbleType {
dynamic let id: String
dynamic let isAuction: Bool
dynamic let startDate: Date
dynamic let endDate: Date?
dynamic let name: String
dynamic var artworkCount: Int
dynamic let auctionState: String
dynamic let bypassCreditCardRequirement: Bool
dynamic var buyersPremium: BuyersPremium?
init(id: String, name: String, isAuction: Bool, startDate: Date, endDate: Date?, artworkCount: Int, state: String, bypassCreditCardRequirement: Bool) {
self.id = id
self.name = name
self.isAuction = isAuction
self.startDate = startDate
self.endDate = endDate
self.artworkCount = artworkCount
self.auctionState = state
self.bypassCreditCardRequirement = bypassCreditCardRequirement
}
static func fromJSON(_ json:[String: Any]) -> Sale {
let json = JSON(json)
let id = json["id"].stringValue
let isAuction = json["is_auction"].boolValue
let startDate = KioskDateFormatter.fromString(json["start_at"].stringValue)!
let endDate = KioskDateFormatter.fromString(json["end_at"].stringValue)
let name = json["name"].stringValue
let artworkCount = json["eligible_sale_artworks_count"].intValue
let state = json["auction_state"].stringValue
let bypassCreditCardRequirement = json["trusted_client_bypasses_card_requirement"].boolValue
let sale = Sale(id: id, name: name, isAuction: isAuction, startDate: startDate, endDate: endDate, artworkCount: artworkCount, state: state, bypassCreditCardRequirement: bypassCreditCardRequirement)
if let buyersPremiumDict = json["buyers_premium"].object as? [String: AnyObject] {
sale.buyersPremium = BuyersPremium.fromJSON(buyersPremiumDict)
}
return sale
}
func isActive(_ systemTime:SystemTime) -> Bool {
let now = systemTime.date()
guard let endDate = endDate else {
// Live sales don't have end dates, and the kiosk isn't compatible with live sales.
// So we'll say any live sale is "not active"
return false
}
return (now as NSDate).earlierDate(startDate) == startDate && (now as NSDate).laterDate(endDate) == endDate
}
}