updated to use keypaths

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-03-16 15:36:59 -05:00
parent 66c769ba52
commit b59a360dea
2 changed files with 23 additions and 18 deletions

View File

@ -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<KeyType: Equatable, ObjectType: Surfaceable> : KeyColorConfigurable {
public class KeyedColorConfiguration<ObjectType: Surfaceable, KeyType: Equatable> : 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<ObjectType, KeyType>?
/// Array of structs where the KeyValue is registered against a SurfaceColorConfiguration
public var keyColors: [KeyColorConfiguration<KeyType>] = []
public required init() {
self.keyName = ""
}
public required init() {}
//this must be used
public convenience init(keyName: String) {
public required convenience init(keyPath: KeyPath<ObjectType, KeyType>) {
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 {

View File

@ -81,8 +81,8 @@ public class Badge: View {
//--------------------------------------------------
// MARK: - Configuration
//--------------------------------------------------
private var backgroundColorConfig: KeyedColorConfiguration<FillColor, Badge> = {
let config = KeyedColorConfiguration<FillColor, Badge>(keyName: "fillColor")
private var backgroundColorConfig: AnyColorable = {
let config = KeyedColorConfiguration<Badge, FillColor>(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()