From b1cf47154ee10b3dafbea8c45e8dff3421c214b3 Mon Sep 17 00:00:00 2001 From: Matt Bruce Date: Thu, 8 Dec 2022 12:47:49 -0600 Subject: [PATCH] added configurations Signed-off-by: Matt Bruce --- VDS/Classes/ColorConfiguration.swift | 120 ++++++++++++++++++++++++++- 1 file changed, 116 insertions(+), 4 deletions(-) diff --git a/VDS/Classes/ColorConfiguration.swift b/VDS/Classes/ColorConfiguration.swift index cafac5f9..9a4f855b 100644 --- a/VDS/Classes/ColorConfiguration.swift +++ b/VDS/Classes/ColorConfiguration.swift @@ -10,26 +10,89 @@ import UIKit public typealias ObjectColorable = Colorable & Initable & ObjectWithable +public class ControlColorConfiguration: StateColorConfiguration, ObjectColorable { + public typealias ObjectType = UIControl & Surfaceable + + required public override init() {} + + public func getColor(_ object: any ObjectType) -> UIColor { + return getColor(object.surface, forState: object.state) + } +} + +public class ViewColorConfiguration: StateColorConfiguration, ObjectColorable { + public typealias ObjectType = Disabling & Surfaceable + + required public override init() {} + + public func setSurfaceColors(_ lightColor: UIColor, _ darkColor: UIColor, forDisabled disabled: Bool) { + setSurfaceColors(lightColor, darkColor, forState: disabled) + } + + public func getColor(_ object: any ObjectType) -> UIColor { + return getColor(object.surface, forState: object.disabled) + } +} + +public class StateColorConfiguration { + + struct StateColor { + var state: StateType + var surfaceConfig: SurfaceColorConfiguration + } + + private var stateColors: [StateColor] = [] + + public init() { } + + public func getColor(_ surface: Surface, forState state: StateType) -> UIColor { + if let stateColor = stateColors.first(where: {$0.state == state }) { + return stateColor.surfaceConfig.getColor(surface) + } else { + return .clear //default + } + } + + public func setSurfaceColors(_ lightColor: UIColor, _ darkColor: UIColor, forState state: StateType) { + stateColors.append(.init(state: state, surfaceConfig: .init(lightColor, darkColor))) + } + + public func reset() { + stateColors.removeAll() + } +} + + /// Meant to be used in a Object that implements the following interfaces for 2 possible color combinations /// - Surfaceable (var surface: Surface) /// /// let model = TestModel() /// model.surface = .light /// -/// let config = SurfaceColorConfiguration() -/// +/// let config = SurfaceColorConfiguration() /// config.lightColor = .black /// config.darkColor = .white /// /// let textColor = config.getColor(model) //returns .black -final public class SurfaceColorConfiguration: ObjectColorable { + +open class SurfaceColorConfiguration: ObjectColorable { public typealias ObjectType = Surfaceable public var lightColor: UIColor = .clear public var darkColor: UIColor = .clear required public init(){} + + public init(_ lightColor: UIColor, _ darkColor: UIColor) { + self.lightColor = lightColor + self.darkColor = darkColor + } + + public func getColor(_ surface: Surface) -> UIColor { + return surface == .light ? lightColor : darkColor + } + public func getColor(_ object: any ObjectType) -> UIColor { - return object.surface == .light ? lightColor : darkColor + return getColor(object.surface) } } @@ -189,3 +252,52 @@ final public class BinaryDisabledSurfaceColorConfiguration: BinaryDisabledSurfac getBinaryColor(object) } } + +public class ControlStateColorConfiguration: ObjectColorable { + public typealias ObjectType = UIControl & Surfaceable + public var disabled = SurfaceColorConfiguration() + public var normal = SurfaceColorConfiguration() + public var highlighted: SurfaceColorConfiguration? + public var selected: SurfaceColorConfiguration? + public var error: SurfaceColorConfiguration? + + required public init(){} + + public func setColorable(_ config: SurfaceColorConfiguration, for state: UIControl.State) { + switch state { + case .disabled: + disabled = config + + case .highlighted: + highlighted = config + + case .selected: + selected = config + + case .error: + error = config + + default: + normal = config + } + } + + public func getColor(_ object: any ObjectType) -> UIColor { + + if object.state == .disabled { + return disabled.getColor(object) + + } else if let highlighted, object.state == .highlighted { + return highlighted.getColor(object) + + } else if let selected, object.state == .selected { + return selected.getColor(object) + + } else if let error, object.state == .error { + return error.getColor(object) + + } else { + return normal.getColor(object) + } + } +}