added comments for ColorConfiguration

Signed-off-by: Matt Bruce <matt.bruce@verizon.com>
This commit is contained in:
Matt Bruce 2023-05-26 16:02:42 -05:00
parent 57135f8ac7
commit b0788a85dd

View File

@ -21,7 +21,9 @@ public typealias ObjectColorable = Colorable & Initable & ObjectWithable
/// config.darkColor = .white
///
/// let textColor = config.getColor(model) //returns .black
///
/// You can pass in a Surfaceable object and it will give you the color responding.
open class SurfaceColorConfiguration: ObjectColorable {
public typealias ObjectType = Surfaceable
public var lightColor: UIColor = .clear
@ -29,15 +31,27 @@ open class SurfaceColorConfiguration: ObjectColorable {
required public init(){}
/// Inititialization
/// - Parameters:
/// - lightColor: Color used for a light surface
/// - darkColor: Color used for a dark surface
public init(_ lightColor: UIColor, _ darkColor: UIColor) {
self.lightColor = lightColor
self.darkColor = darkColor
}
/// Will get the UIColor for the surface passed in
/// - Parameter surface: surface you currently have
/// - Returns: color corresponding to the surface you passed
public func getColor(_ surface: Surface) -> UIColor {
return surface == .light ? lightColor : darkColor
}
/// Colorable Generic method that will only take in any object that has a Surfacable implementation
/// - Parameter object: any Surfacable object type
/// - Returns: UIColor for the object type you passed in
public func getColor(_ object: any ObjectType) -> UIColor {
return getColor(object.surface)
}
@ -57,16 +71,30 @@ public protocol KeyColorConfigurable: ObjectColorable {
}
extension KeyColorConfigurable {
/// Generic Method to set a KeyColorConfiguration with the parameters given
/// - Parameters:
/// - lightColor: Color used for a light
/// - darkColor: Color used for a dark
/// - key: <#key description#>
public func setSurfaceColors(_ lightColor: UIColor, _ darkColor: UIColor, forKey key: KeyType) {
keyColors.append(.init(key: key, surfaceConfig: .init(lightColor, darkColor)))
}
/// Removes all keyColors
public func reset() {
keyColors.removeAll()
}
}
extension KeyColorConfigurable where ObjectType: Surfaceable {
/// Default Implementation for when the ObjectType is a Surfacable
/// - Parameters:
/// - object: Surfaceable ObjectType
/// - key: KeyPath to the property of the ObjectType
/// - Returns: UIColor corresponding the the key and surface
public func getColor(for object: ObjectType, with key: KeyType) -> UIColor {
if let keyColor = keyColors.first(where: {$0.key == key }) {
return keyColor.surfaceConfig.getColor(object)
@ -77,6 +105,8 @@ extension KeyColorConfigurable where ObjectType: Surfaceable {
}
/// ColorConfiguration for UIControls and will implement KeyColorConfigurale. This will then use the
/// register a SurfaceColorConfiguration for each state.
public class ControlColorConfiguration: KeyColorConfigurable {
public typealias KeyType = UIControl.State
public typealias ObjectType = Surfaceable & UIControl
@ -84,10 +114,19 @@ public class ControlColorConfiguration: KeyColorConfigurable {
private var lastKeyColor: KeyColorConfiguration<KeyType>?
public required init() { }
/// Helper method to set the SurfaceColorConfiguration for a state
/// - Parameters:
/// - lightColor: Color used for a light
/// - darkColor: Color used for a dark
/// - state: UIControlState you are keying off of.
public func setSurfaceColors(_ lightColor: UIColor, _ darkColor: UIColor, forState state: KeyType) {
setSurfaceColors(lightColor, darkColor, forKey: state)
}
/// Colorable implementation for getColor that where the object passed in will be of a UIControl as well as implementing Surfaceable.
/// - Parameter object: Object that is of UIControl and implements Surfacable
/// - Returns: UIColor corresponding to the UIControl.state and surface property of this object.
public func getColor(_ object: any ObjectType) -> UIColor {
let state = object.state
let surface = object.surface
@ -115,6 +154,7 @@ public class ControlColorConfiguration: KeyColorConfigurable {
}
}
///Meant to be used with any object that implements Surfaceable and Disabling. More than likely this is any View.
public class ViewColorConfiguration: KeyColorConfigurable {
public typealias KeyType = Bool
public typealias ObjectType = Surfaceable & Disabling
@ -122,10 +162,18 @@ public class ViewColorConfiguration: KeyColorConfigurable {
public required init() { }
/// Helper method to set the SurfaceColorConfiguration based on whether the object is disabled or not.
/// - Parameters:
/// - lightColor: Color used for a light
/// - darkColor: Color used for a dark
/// - disabled: True/False for disabled property
public func setSurfaceColors(_ lightColor: UIColor, _ darkColor: UIColor, forDisabled disabled: KeyType) {
setSurfaceColors(lightColor, darkColor, forKey: disabled)
}
/// Colorable implementation for getColor that where the object passed in will any object that implementing Surfaceable and Disabling.
/// - Parameter object: Object that implements Surfaceable and Disabling
/// - Returns: UIColor correspoding to either true/false for the disabled state and surface
public func getColor(_ object: ObjectType) -> UIColor {
if let keyColor = keyColors.first(where: {$0.key == object.disabled }) {
return keyColor.surfaceConfig.getColor(object)
@ -154,11 +202,17 @@ public class KeyedColorConfiguration<ObjectType: Surfaceable, KeyType: Equatable
self.keyPath = keyPath
}
/// Finds the KeyColorConfiguration for the Object passed in.
/// - Parameter object: Object should be of Surfaceable
/// - Returns: KeyType for the Object passed in
public func getKeyValue(_ object: ObjectType) -> KeyType? {
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]
}
/// Colorable implementation for getColor that where the object passed in will any object that implementing Surfaceable for the KeyPath registered.
/// - Parameter object: Object matching the type registered
/// - Returns: UIColor for the Object pertaining to the KeyPath it was registered
public func getColor(_ object: ObjectType) -> UIColor {
if let key = getKeyValue(object), let keyColor = keyColors.first(where: {$0.key == key }) {
return keyColor.surfaceConfig.getColor(object)