Skip to content

Commit 38f2a28

Browse files
SahilSainiYMLMark Pospesel
and
Mark Pospesel
authored
[ISSUE] Add logger to colorable on fallBack (#62)
* [ISSUE] added logger to colorable on failBack * [CM-1324] Update documentation comments --------- Co-authored-by: Mark Pospesel <mark.pospesel@ymedialabs.com>
1 parent ec33ac3 commit 38f2a28

File tree

2 files changed

+60
-20
lines changed

2 files changed

+60
-20
lines changed

Sources/YCoreUI/Protocols/Colorable.swift

+24-20
Original file line numberDiff line numberDiff line change
@@ -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.
1717
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.
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

4035
extension 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
}

Tests/YCoreUITests/Protocols/ColorableTests.swift

+36
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,42 @@ final class ColorableTests: XCTestCase {
4141
}
4242

4343
func testMissingColor() {
44+
YCoreUI.isLoggingEnabled = false
45+
4446
PrimaryColors.allCases.forEach {
4547
XCTAssertNil($0.loadColor())
4648
XCTAssertEqual($0.color, PrimaryColors.fallbackColor)
4749
}
50+
51+
YCoreUI.isLoggingEnabled = true
52+
}
53+
54+
func test_calculateName_deliversCorrectName() {
55+
PrimaryColors.allCases.forEach {
56+
XCTAssertEqual($0.calculateName(), $0.rawValue)
57+
}
58+
59+
ErrorColors.allCases.forEach {
60+
XCTAssertEqual($0.calculateName(), "Error/\($0.rawValue)")
61+
}
62+
63+
WarningColors.allCases.forEach {
64+
XCTAssertEqual($0.calculateName(), $0.rawValue)
65+
}
66+
}
67+
68+
func test_colorable_deliversCorrectColor() {
69+
ErrorColors.allCases.forEach {
70+
XCTAssertNotEqual($0.color, ErrorColors.fallbackColor)
71+
}
72+
73+
WarningColors.allCases.forEach {
74+
XCTAssertNotEqual($0.color, WarningColors.fallbackColor)
75+
}
76+
}
77+
78+
func test_colorable_deliversDefaultFallbackColor() {
79+
XCTAssertEqual(DefaultColors.defaultCase.color, DefaultColors.fallbackColor)
4880
}
4981
}
5082

@@ -70,4 +102,8 @@ private extension ColorableTests {
70102

71103
static var fallbackColor: UIColor { .systemPurple }
72104
}
105+
106+
enum DefaultColors: String, CaseIterable, Colorable {
107+
case defaultCase
108+
}
73109
}

0 commit comments

Comments
 (0)