@@ -15,55 +15,59 @@ import UIKit
15
15
/// package, override `bundle` to return `.module`. If your assets are categorized within their asset catalog by
16
16
/// a namespace, then override `namespace` to return the proper string prefix.
17
17
public 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.
19
19
static var bundle : Bundle { get }
20
20
21
- /// Optional namespace for the color assets (default is `nil`)
21
+ /// Optional namespace for the color assets.
22
22
static var namespace : String ? { get }
23
23
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.
25
25
static var fallbackColor : UIColor { get }
26
26
27
27
/// Loads the named color.
28
- ///
29
- /// Default implementation uses `UIColor(named:in:compatibleWith:)` passing in the associated `namespace`
30
- /// (prepended to `rawValue`) and `bundle`.
31
28
/// - Returns: The named color or else `nil` if the named asset cannot be loaded
32
29
func loadColor( ) -> UIColor ?
33
30
34
31
/// A color asset for this name value.
35
- ///
36
- /// Default implementation calls `loadColor` and nil-coalesces to `fallbackColor`.
37
32
var color : UIColor { get }
38
33
}
39
34
40
35
extension Colorable {
41
- /// The bundle containing the color assets for this enum (default is `.main`)
36
+ /// Returns the `.main` bundle.
42
37
public static var bundle : Bundle { . main }
43
38
44
- /// Optional namespace for the color assets (default is `nil`)
39
+ /// Returns `nil` to indicate no namespace.
45
40
public static var namespace : String ? { nil }
46
41
47
- /// Fallback color to use in case a color asset cannot be loaded (default is `.systemPink`)
42
+ /// Returns `.systemPink` color.
48
43
public static var fallbackColor : UIColor { . systemPink }
49
44
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`
53
46
/// (prepended to `rawValue`) and `bundle`.
54
- /// - Returns: The named color or else `nil` if the named asset cannot be loaded
55
47
public func loadColor( ) -> UIColor ? {
48
+ UIColor ( named: calculateName ( ) , in: Self . bundle, compatibleWith: nil )
49
+ }
50
+
51
+ internal func calculateName( ) -> String {
56
52
let name : String
57
53
if let validNamespace = Self . namespace {
58
54
name = " \( validNamespace) / \( rawValue) "
59
55
} else {
60
56
name = rawValue
61
57
}
62
- return UIColor ( named : name, in : Self . bundle , compatibleWith : nil )
58
+ return name
63
59
}
64
-
65
- /// A color asset for this name value .
60
+
61
+ /// Returns `loadColor()` nil-coalesced to `fallbackColor` .
66
62
///
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
+ }
69
73
}
0 commit comments