updated to use keypaths
Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
parent
66c769ba52
commit
b59a360dea
@ -60,12 +60,22 @@ extension KeyColorConfigurable {
|
|||||||
public func setSurfaceColors(_ lightColor: UIColor, _ darkColor: UIColor, forKey key: KeyType) {
|
public func setSurfaceColors(_ lightColor: UIColor, _ darkColor: UIColor, forKey key: KeyType) {
|
||||||
keyColors.append(.init(key: key, surfaceConfig: .init(lightColor, darkColor)))
|
keyColors.append(.init(key: key, surfaceConfig: .init(lightColor, darkColor)))
|
||||||
}
|
}
|
||||||
|
|
||||||
public func reset() {
|
public func reset() {
|
||||||
keyColors.removeAll()
|
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 class ControlColorConfiguration: KeyColorConfigurable {
|
||||||
public typealias KeyType = UIControl.State
|
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
|
/// 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
|
//Type of Class that at least implements Surfaceable
|
||||||
public typealias ObjectType = ObjectType
|
public typealias ObjectType = ObjectType
|
||||||
|
|
||||||
/// property that will be used for reflection, the type of this property must implement Equatable
|
//where the value exist for the object
|
||||||
public var keyName: String
|
public var keyPath: KeyPath<ObjectType, KeyType>?
|
||||||
|
|
||||||
/// Array of structs where the KeyValue is registered against a SurfaceColorConfiguration
|
/// Array of structs where the KeyValue is registered against a SurfaceColorConfiguration
|
||||||
public var keyColors: [KeyColorConfiguration<KeyType>] = []
|
public var keyColors: [KeyColorConfiguration<KeyType>] = []
|
||||||
|
|
||||||
public required init() {
|
public required init() {}
|
||||||
self.keyName = ""
|
|
||||||
}
|
|
||||||
|
|
||||||
//this must be used
|
//this must be used
|
||||||
public convenience init(keyName: String) {
|
public required convenience init(keyPath: KeyPath<ObjectType, KeyType>) {
|
||||||
self.init()
|
self.init()
|
||||||
self.keyName = keyName
|
self.keyPath = keyPath
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getKeyValue(_ object: ObjectType) -> KeyType? {
|
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") }
|
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]
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public func getColor(_ object: ObjectType) -> UIColor {
|
public func getColor(_ object: ObjectType) -> UIColor {
|
||||||
|
|||||||
@ -81,8 +81,8 @@ public class Badge: View {
|
|||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
// MARK: - Configuration
|
// MARK: - Configuration
|
||||||
//--------------------------------------------------
|
//--------------------------------------------------
|
||||||
private var backgroundColorConfig: KeyedColorConfiguration<FillColor, Badge> = {
|
private var backgroundColorConfig: AnyColorable = {
|
||||||
let config = KeyedColorConfiguration<FillColor, Badge>(keyName: "fillColor")
|
let config = KeyedColorConfiguration<Badge, FillColor>(keyPath: \.fillColor)
|
||||||
config.setSurfaceColors(VDSColor.backgroundBrandhighlight, VDSColor.backgroundBrandhighlight, forKey: .red)
|
config.setSurfaceColors(VDSColor.backgroundBrandhighlight, VDSColor.backgroundBrandhighlight, forKey: .red)
|
||||||
config.setSurfaceColors(VDSColor.paletteYellow62, VDSColor.paletteYellow62, forKey: .yellow)
|
config.setSurfaceColors(VDSColor.paletteYellow62, VDSColor.paletteYellow62, forKey: .yellow)
|
||||||
config.setSurfaceColors(VDSColor.paletteGreen26, VDSColor.paletteGreen34, forKey: .green)
|
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.paletteBlue35, VDSColor.paletteBlue45, forKey: .blue)
|
||||||
config.setSurfaceColors(VDSColor.paletteBlack, VDSColor.paletteBlack, forKey: .black)
|
config.setSurfaceColors(VDSColor.paletteBlack, VDSColor.paletteBlack, forKey: .black)
|
||||||
config.setSurfaceColors(VDSColor.paletteWhite, VDSColor.paletteWhite, forKey: .white)
|
config.setSurfaceColors(VDSColor.paletteWhite, VDSColor.paletteWhite, forKey: .white)
|
||||||
return config
|
return config.eraseToAnyColorable()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
private var textColorConfig = ViewColorConfiguration()
|
private var textColorConfig = ViewColorConfiguration()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user