// // Colorable.swift // VDS // // Created by Matt Bruce on 10/10/22. // import Foundation import UIKit /// Protocol used to be implemented on a object that can return a color for a Generic ObjectType. public protocol Colorable { associatedtype ObjectType /// Method used to get the current color needed for the Generic ObjectType passed into the method. func getColor(_ object: ObjectType) -> UIColor } extension Colorable { /// Helper method used where you don't know the ObjectType to return the color. /// - Parameter object: Any object, but this objectType will be checked against the registered Generic ObjectType for the Colorable /// - Returns: UIColor for the rule of this Colorable implementation for the object passed into the function fileprivate func getColor(_ object: Any) -> UIColor { guard let model = object as? ObjectType else { assertionFailure("Invalid ObjectType, Expecting type \(ObjectType.self), received \(object) ") return .black } return getColor(model) } /// TypeErasure for the Colorable to AnyColorable /// - Returns: AnyColorable public func eraseToAnyColorable() -> AnyColorable { AnyColorable(self) } } /// TypeErased Struct of a Colorable Type. This type can be used anywhere as long as the ObjectType being passed into this method matches the wrapped Colorable ObjectType. public struct AnyColorable: Colorable, Withable { private let wrapped: any Colorable public init(_ colorable: any Colorable) { wrapped = colorable } public func getColor(_ object: Any) -> UIColor { wrapped.getColor(object) } }