From b59a360dea7d2adbf11629d22f541b3af34baa2b Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 16 Mar 2023 15:36:59 -0500 Subject: [PATCH] updated to use keypaths Signed-off-by: Matt Bruce --- VDS/Classes/ColorConfiguration.swift | 35 ++++++++++++++++------------ VDS/Components/Badge/Badge.swift | 6 ++--- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/VDS/Classes/ColorConfiguration.swift b/VDS/Classes/ColorConfiguration.swift index 952d6e66..9c7c25bd 100644 --- a/VDS/Classes/ColorConfiguration.swift +++ b/VDS/Classes/ColorConfiguration.swift @@ -60,12 +60,22 @@ extension KeyColorConfigurable { public func setSurfaceColors(_ lightColor: UIColor, _ darkColor: UIColor, forKey key: KeyType) { keyColors.append(.init(key: key, surfaceConfig: .init(lightColor, darkColor))) } - + public func reset() { keyColors.removeAll() } } +extension KeyColorConfigurable where ObjectType: Surfaceable { + public func getColor(for object: ObjectType, with key: KeyType) -> UIColor { + if let keyColor = keyColors.first(where: {$0.key == key }) { + return keyColor.surfaceConfig.getColor(object) + } else { + return .clear //default + } + } +} + public class ControlColorConfiguration: KeyColorConfigurable { public typealias KeyType = UIControl.State @@ -119,32 +129,27 @@ public class ViewColorConfiguration: KeyColorConfigurable { } /// Generic Class that allows reflection for a object's specific property (equatable) to be defined as the lookup for a SurfaceConfiguration -public class KeyedColorConfiguration : KeyColorConfigurable { +public class KeyedColorConfiguration : KeyColorConfigurable { //Type of Class that at least implements Surfaceable public typealias ObjectType = ObjectType - /// property that will be used for reflection, the type of this property must implement Equatable - public var keyName: String - + //where the value exist for the object + public var keyPath: KeyPath? + /// Array of structs where the KeyValue is registered against a SurfaceColorConfiguration public var keyColors: [KeyColorConfiguration] = [] - public required init() { - self.keyName = "" - } + public required init() {} //this must be used - public convenience init(keyName: String) { + public required convenience init(keyPath: KeyPath) { self.init() - self.keyName = keyName + self.keyPath = keyPath } public func getKeyValue(_ object: ObjectType) -> KeyType? { - guard !keyName.isEmpty else { fatalError("keyName must not be empty, make sure you initialize this class using init(keyName: String) method") } - - let mirror = Mirror(reflecting: object) - guard let result = mirror.children.first(where: {$0.label == keyName })?.value as? KeyType, !mirror.children.isEmpty else { return nil } - return result + guard let keyPath else { fatalError("keyPath must not be empty, make sure you initialize this class using init(keyPath: \\Object.property) method") } + return object[keyPath: keyPath] } public func getColor(_ object: ObjectType) -> UIColor { diff --git a/VDS/Components/Badge/Badge.swift b/VDS/Components/Badge/Badge.swift index 10a0ed59..da4cdbca 100644 --- a/VDS/Components/Badge/Badge.swift +++ b/VDS/Components/Badge/Badge.swift @@ -81,8 +81,8 @@ public class Badge: View { //-------------------------------------------------- // MARK: - Configuration //-------------------------------------------------- - private var backgroundColorConfig: KeyedColorConfiguration = { - let config = KeyedColorConfiguration(keyName: "fillColor") + private var backgroundColorConfig: AnyColorable = { + let config = KeyedColorConfiguration(keyPath: \.fillColor) config.setSurfaceColors(VDSColor.backgroundBrandhighlight, VDSColor.backgroundBrandhighlight, forKey: .red) config.setSurfaceColors(VDSColor.paletteYellow62, VDSColor.paletteYellow62, forKey: .yellow) config.setSurfaceColors(VDSColor.paletteGreen26, VDSColor.paletteGreen34, forKey: .green) @@ -90,7 +90,7 @@ public class Badge: View { config.setSurfaceColors(VDSColor.paletteBlue35, VDSColor.paletteBlue45, forKey: .blue) config.setSurfaceColors(VDSColor.paletteBlack, VDSColor.paletteBlack, forKey: .black) config.setSurfaceColors(VDSColor.paletteWhite, VDSColor.paletteWhite, forKey: .white) - return config + return config.eraseToAnyColorable() }() private var textColorConfig = ViewColorConfiguration()