@@ -15,55 +15,59 @@ import UIKit
1515/// package, override `bundle` to return `.module`. If your assets are categorized within their asset catalog by
1616/// a namespace, then override `namespace` to return the proper string prefix.
1717public protocol Colorable : RawRepresentable where RawValue == String {
18- /// The bundle containing the color assets for this enum (default is `.main`)
18+ /// The bundle containing the color assets for this enum.
1919 static var bundle : Bundle { get }
2020
21- /// Optional namespace for the color assets (default is `nil`)
21+ /// Optional namespace for the color assets.
2222 static var namespace : String ? { get }
2323
24- /// Fallback color to use in case a color asset cannot be loaded (default is `.systemPink`)
24+ /// Fallback color to use in case a color asset cannot be loaded.
2525 static var fallbackColor : UIColor { get }
2626
2727 /// Loads the named color.
28- ///
29- /// Default implementation uses `UIColor(named:in:compatibleWith:)` passing in the associated `namespace`
30- /// (prepended to `rawValue`) and `bundle`.
3128 /// - Returns: The named color or else `nil` if the named asset cannot be loaded
3229 func loadColor( ) -> UIColor ?
3330
3431 /// A color asset for this name value.
35- ///
36- /// Default implementation calls `loadColor` and nil-coalesces to `fallbackColor`.
3732 var color : UIColor { get }
3833}
3934
4035extension Colorable {
41- /// The bundle containing the color assets for this enum (default is `.main`)
36+ /// Returns the `.main` bundle.
4237 public static var bundle : Bundle { . main }
4338
44- /// Optional namespace for the color assets (default is `nil`)
39+ /// Returns `nil` to indicate no namespace.
4540 public static var namespace : String ? { nil }
4641
47- /// Fallback color to use in case a color asset cannot be loaded (default is `.systemPink`)
42+ /// Returns `.systemPink` color.
4843 public static var fallbackColor : UIColor { . systemPink }
4944
50- /// Loads the named color.
51- ///
52- /// Default implementation uses `UIColor(named:in:compatibleWith:)` passing in the associated `namespace`
45+ /// Returns `UIColor(named:in:compatibleWith:)` passing in the associated `namespace`
5346 /// (prepended to `rawValue`) and `bundle`.
54- /// - Returns: The named color or else `nil` if the named asset cannot be loaded
5547 public func loadColor( ) -> UIColor ? {
48+ UIColor ( named: calculateName ( ) , in: Self . bundle, compatibleWith: nil )
49+ }
50+
51+ internal func calculateName( ) -> String {
5652 let name : String
5753 if let validNamespace = Self . namespace {
5854 name = " \( validNamespace) / \( rawValue) "
5955 } else {
6056 name = rawValue
6157 }
62- return UIColor ( named : name, in : Self . bundle , compatibleWith : nil )
58+ return name
6359 }
64-
65- /// A color asset for this name value .
60+
61+ /// Returns `loadColor()` nil-coalesced to `fallbackColor` .
6662 ///
67- /// Default implementation calls `loadColor` and nil-coalesces to `fallbackColor`.
68- public var color : UIColor { loadColor ( ) ?? Self . fallbackColor }
63+ /// Unless logging is disabled, a warning message will be logged to the console if the color asset fails to load.
64+ public var color : UIColor {
65+ guard let color = loadColor ( ) else {
66+ if YCoreUI . isLoggingEnabled {
67+ YCoreUI . colorLogger. warning ( " Color named \( calculateName ( ) ) failed to load from bundle. " )
68+ }
69+ return Self . fallbackColor
70+ }
71+ return color
72+ }
6973}
0 commit comments