58 lines
1.4 KiB
Swift
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)
|
|
}
|
|
}
|