diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index cebd999c..05bb03d6 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -37,6 +37,7 @@ EA3362452892F9130071C351 /* Labelable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA3362442892F9130071C351 /* Labelable.swift */; }; EA33624728931B050071C351 /* Initable.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA33624628931B050071C351 /* Initable.swift */; }; EA3C3B4C2894823E000CA526 /* AnyProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA3C3B4B2894823E000CA526 /* AnyProxy.swift */; }; + EA84F6B128B94A2500D67ABC /* CodableColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA84F6B028B94A2500D67ABC /* CodableColor.swift */; }; EA89200228AECF2A006B9984 /* UIButton+Publisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA89200128AECF2A006B9984 /* UIButton+Publisher.swift */; }; EA89200428AECF4B006B9984 /* UITextField+Publisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA89200328AECF4B006B9984 /* UITextField+Publisher.swift */; }; EA89200628B526D6006B9984 /* CheckboxGroup.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA89200528B526D6006B9984 /* CheckboxGroup.swift */; }; @@ -127,6 +128,7 @@ EA3362442892F9130071C351 /* Labelable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Labelable.swift; sourceTree = ""; }; EA33624628931B050071C351 /* Initable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Initable.swift; sourceTree = ""; }; EA3C3B4B2894823E000CA526 /* AnyProxy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyProxy.swift; sourceTree = ""; }; + EA84F6B028B94A2500D67ABC /* CodableColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CodableColor.swift; sourceTree = ""; }; EA89200128AECF2A006B9984 /* UIButton+Publisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIButton+Publisher.swift"; sourceTree = ""; }; EA89200328AECF4B006B9984 /* UITextField+Publisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITextField+Publisher.swift"; sourceTree = ""; }; EA89200528B526D6006B9984 /* CheckboxGroup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CheckboxGroup.swift; sourceTree = ""; }; @@ -385,8 +387,9 @@ isa = PBXGroup; children = ( EA3C3B4B2894823E000CA526 /* AnyProxy.swift */, - EAB1D2A028A598FE00DAE764 /* UsesAutoLayout.swift */, + EA84F6B028B94A2500D67ABC /* CodableColor.swift */, EAB1D2A228A5994800DAE764 /* Debuggable.swift */, + EAB1D2A028A598FE00DAE764 /* UsesAutoLayout.swift */, ); path = PropertyWrappers; sourceTree = ""; @@ -569,6 +572,7 @@ EAF7F11828A1475A00B287F5 /* RadioButtonModel.swift in Sources */, EA89200D28B530FD006B9984 /* RadioBoxModel.swift in Sources */, EA89201328B568D8006B9984 /* RadioBox.swift in Sources */, + EA84F6B128B94A2500D67ABC /* CodableColor.swift in Sources */, EA3362402892EF6C0071C351 /* Label.swift in Sources */, EAF7F0B3289B1ADC00B287F5 /* LabelAttributeAction.swift in Sources */, EA33622E2891EA3C0071C351 /* DispatchQueue+Once.swift in Sources */, diff --git a/VDS/PropertyWrappers/CodableColor.swift b/VDS/PropertyWrappers/CodableColor.swift new file mode 100644 index 00000000..3b046acc --- /dev/null +++ b/VDS/PropertyWrappers/CodableColor.swift @@ -0,0 +1,57 @@ +// +// 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) + } +}