vds_ios/VDS/PropertyWrappers/CodableColor.swift
Matt Bruce 4dcddd7fef added colorcodable
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
2022-08-26 14:19:48 -05:00

58 lines
1.4 KiB
Swift

//
// CodableColor.swift
// VDS
//
// Created by Matt Bruce on 8/26/22.
//
import Foundation
import UIKit
@propertyWrapper
public struct CodableColor {
public var wrappedValue: UIColor
public init(wrappedValue: UIColor) {
self.wrappedValue = wrappedValue
}
}
extension CodableColor: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let colorString = try container.decode(String.self)
wrappedValue = UIColor(hexString: colorString)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(wrappedValue.hexString)
}
}
@propertyWrapper
public struct OptionalCodableColor {
public var wrappedValue: UIColor?
public init(wrappedValue: UIColor?) {
self.wrappedValue = wrappedValue
}
}
extension OptionalCodableColor: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if container.decodeNil() {
wrappedValue = nil
} else {
let colorString = try container.decode(String.self)
wrappedValue = UIColor(hexString: colorString)
}
}
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(wrappedValue?.hexString)
}
}