added codable for VDS

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2024-08-20 13:59:19 -05:00
parent 94ace3d49a
commit 478f2b6083

View File

@ -57,6 +57,40 @@ extension VDS.Button.Size: RawRepresentableCodable {
public static var defaultValue: VDS.Button.Size? { nil }
}
extension VDS.CalendarBase.CalendarIndicatorModel: Codable {
enum CodingKeys: String, CodingKey {
case label
case date
case dateFormat
}
public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let label = try container.decode(String.self, forKey: .label)
let formatter = Self.formatter()
formatter.dateFormat = try container.decodeIfPresent(String.self, forKey: .dateFormat) ?? "MMM d, y"
let foundDate = try container.decode(String.self, forKey: .date)
let date = formatter.date(from: foundDate)!
self = .init(label: label, date: date)
}
public func encode(to encoder: any Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(label, forKey: .label)
try container.encode(Self.formatter().string(from: date), forKey: .date)
}
static func formatter() -> DateFormatter {
let formatter = DateFormatter()
formatter.dateStyle = .medium
formatter.timeZone = NSTimeZone.system
formatter.locale = .current
formatter.formatterBehavior = .default
return formatter
}
}
//--------------------------------------------------
// MARK: - Decodable Defaults
//--------------------------------------------------