// // ModelColorConfiguration.swift // VDS // // Created by Matt Bruce on 8/4/22. // import Foundation import UIKit public typealias ObjectColorable = Colorable & Initable & ObjectWithable public class ControlColorConfiguration: StateColorConfiguration, ObjectColorable { public typealias ObjectType = UIControl & Surfaceable required public override init() {} public override func getColor(_ surface: Surface, forState state: UIControl.State) -> UIColor { // find the exact match if let stateColor = stateColors.first(where: {$0.state == state }) { return stateColor.surfaceConfig.getColor(surface) } else if state.contains(.disabled), let stateColor = stateColors.first(where: {$0.state == .disabled }) { return stateColor.surfaceConfig.getColor(surface) } else if state.contains(.highlighted), let stateColor = stateColors.first(where: {$0.state == .highlighted }) { return stateColor.surfaceConfig.getColor(surface) } else { return .clear } } 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 } internal 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() /// config.lightColor = .black /// config.darkColor = .white /// /// let textColor = config.getColor(model) //returns .black 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 getColor(object.surface) } }