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<ModelType> {
|
|
associatedtype ModelType
|
|
func getColor(_ viewModel: ModelType) -> UIColor
|
|
}
|
|
|
|
extension Colorable {
|
|
fileprivate func getColor(_ viewModel: Any) -> UIColor {
|
|
guard let model = viewModel as? ModelType else {
|
|
assertionFailure("Invalid ModelType, Expecting type \(ModelType.self), received \(viewModel) ")
|
|
return .black
|
|
}
|
|
return getColor(model)
|
|
}
|
|
|
|
public func eraseToAnyColorable() -> AnyColorable { AnyColorable(self) }
|
|
}
|
|
|
|
public struct GenericColorable<ModelType>: Colorable, Withable {
|
|
private var wrapped: any Colorable<ModelType>
|
|
|
|
public init<C: Colorable>(colorable: C) where C.ModelType == ModelType {
|
|
wrapped = colorable
|
|
}
|
|
|
|
public func getColor(_ viewModel: ModelType) -> UIColor {
|
|
wrapped.getColor(viewModel)
|
|
}
|
|
}
|
|
|
|
public struct AnyColorable: Colorable, Withable {
|
|
private let wrapped: any Colorable
|
|
|
|
public init(_ colorable: any Colorable) {
|
|
wrapped = colorable
|
|
}
|
|
|
|
public func getColor(_ viewModel: Any) -> UIColor {
|
|
wrapped.getColor(viewModel)
|
|
}
|
|
}
|