51 lines
1.2 KiB
Swift
51 lines
1.2 KiB
Swift
//
|
|
// Colorable.swift
|
|
// VDS
|
|
//
|
|
// Created by Matt Bruce on 10/10/22.
|
|
//
|
|
|
|
import Foundation
|
|
import UIKit
|
|
|
|
public protocol Colorable<ObjectType> {
|
|
associatedtype ObjectType
|
|
func getColor(_ object: ObjectType) -> UIColor
|
|
}
|
|
|
|
extension Colorable {
|
|
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)
|
|
}
|
|
|
|
public func eraseToAnyColorable() -> AnyColorable { AnyColorable(self) }
|
|
}
|
|
|
|
public struct GenericColorable<ObjectType>: Colorable, Withable {
|
|
private var wrapped: any Colorable<ObjectType>
|
|
|
|
public init<C: Colorable>(colorable: C) where C.ObjectType == ObjectType {
|
|
wrapped = colorable
|
|
}
|
|
|
|
public func getColor(_ object: ObjectType) -> UIColor {
|
|
wrapped.getColor(object)
|
|
}
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|