53 lines
1.8 KiB
Swift
53 lines
1.8 KiB
Swift
//
|
|
// Drink.swift
|
|
// meterios
|
|
//
|
|
// (c) 2025 Martin "maride" Dessauer
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct Drink: Identifiable, Encodable, Decodable {
|
|
public var id: Int
|
|
public var name: String
|
|
public var logoURL: String
|
|
private var price: String
|
|
private var bottle_size: String
|
|
public var caffeine: Int
|
|
public var active: Bool
|
|
|
|
// KeyMap is an enum covering the field names as used by SpaceMarket API
|
|
private enum KeyMap : String, CodingKey { case id, name, bottle_size, caffeine, price, active, logo_url }
|
|
|
|
// Init function to be used with JSONDecoder using SpaceMarket API's strange types
|
|
init(from decoder : Decoder) throws {
|
|
let container = try decoder.container(keyedBy: KeyMap.self)
|
|
self.id = try container.decode(Int.self, forKey: .id)
|
|
self.name = try container.decode(String.self, forKey: .name)
|
|
self.bottle_size = (try? container.decode(String.self, forKey: .bottle_size)) ?? ""
|
|
self.caffeine = (try? container.decode(Int.self, forKey: .caffeine)) ?? 0
|
|
self.price = (try? container.decode(String.self, forKey: .price)) ?? ""
|
|
self.active = (try? container.decode(Bool.self, forKey: .active)) ?? false
|
|
self.logoURL = (try? container.decode(String.self, forKey: .logo_url)) ?? ""
|
|
}
|
|
|
|
// Init function, as straight-forward as it gets
|
|
init(id: Int, name: String, logoURL: String = "", price: String = "", bottle_size: String = "", caffeine: Int = 0, active: Bool = true) {
|
|
self.id = id
|
|
self.name = name
|
|
self.logoURL = logoURL
|
|
self.price = price
|
|
self.bottle_size = bottle_size
|
|
self.caffeine = caffeine
|
|
self.active = active
|
|
}
|
|
|
|
func GetPrice() -> Float {
|
|
return Float(self.price) ?? 9999.99
|
|
}
|
|
|
|
func GetBottleSize() -> Float {
|
|
return Float(self.bottle_size) ?? 9999.99
|
|
}
|
|
}
|