Skip to content

Commit

Permalink
[ISSUE] Add logger to colorable on fallBack (#62)
Browse files Browse the repository at this point in the history
* [ISSUE] added logger to colorable on failBack

* [CM-1324] Update documentation comments

---------

Co-authored-by: Mark Pospesel <mark.pospesel@ymedialabs.com>
  • Loading branch information
SahilSainiYML and Mark Pospesel committed Apr 11, 2023
1 parent ec33ac3 commit 38f2a28
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 20 deletions.
44 changes: 24 additions & 20 deletions Sources/YCoreUI/Protocols/Colorable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,55 +15,59 @@ import UIKit
/// package, override `bundle` to return `.module`. If your assets are categorized within their asset catalog by
/// a namespace, then override `namespace` to return the proper string prefix.
public protocol Colorable: RawRepresentable where RawValue == String {
/// The bundle containing the color assets for this enum (default is `.main`)
/// The bundle containing the color assets for this enum.
static var bundle: Bundle { get }

/// Optional namespace for the color assets (default is `nil`)
/// Optional namespace for the color assets.
static var namespace: String? { get }

/// Fallback color to use in case a color asset cannot be loaded (default is `.systemPink`)
/// Fallback color to use in case a color asset cannot be loaded.
static var fallbackColor: UIColor { get }

/// Loads the named color.
///
/// Default implementation uses `UIColor(named:in:compatibleWith:)` passing in the associated `namespace`
/// (prepended to `rawValue`) and `bundle`.
/// - Returns: The named color or else `nil` if the named asset cannot be loaded
func loadColor() -> UIColor?

/// A color asset for this name value.
///
/// Default implementation calls `loadColor` and nil-coalesces to `fallbackColor`.
var color: UIColor { get }
}

extension Colorable {
/// The bundle containing the color assets for this enum (default is `.main`)
/// Returns the `.main` bundle.
public static var bundle: Bundle { .main }

/// Optional namespace for the color assets (default is `nil`)
/// Returns `nil` to indicate no namespace.
public static var namespace: String? { nil }

/// Fallback color to use in case a color asset cannot be loaded (default is `.systemPink`)
/// Returns `.systemPink` color.
public static var fallbackColor: UIColor { .systemPink }

/// Loads the named color.
///
/// Default implementation uses `UIColor(named:in:compatibleWith:)` passing in the associated `namespace`
/// Returns `UIColor(named:in:compatibleWith:)` passing in the associated `namespace`
/// (prepended to `rawValue`) and `bundle`.
/// - Returns: The named color or else `nil` if the named asset cannot be loaded
public func loadColor() -> UIColor? {
UIColor(named: calculateName(), in: Self.bundle, compatibleWith: nil)
}

internal func calculateName() -> String {
let name: String
if let validNamespace = Self.namespace {
name = "\(validNamespace)/\(rawValue)"
} else {
name = rawValue
}
return UIColor(named: name, in: Self.bundle, compatibleWith: nil)
return name
}
/// A color asset for this name value.

/// Returns `loadColor()` nil-coalesced to `fallbackColor`.
///
/// Default implementation calls `loadColor` and nil-coalesces to `fallbackColor`.
public var color: UIColor { loadColor() ?? Self.fallbackColor }
/// Unless logging is disabled, a warning message will be logged to the console if the color asset fails to load.
public var color: UIColor {
guard let color = loadColor() else {
if YCoreUI.isLoggingEnabled {
YCoreUI.colorLogger.warning("Color named \(calculateName()) failed to load from bundle.")
}
return Self.fallbackColor
}
return color
}
}
36 changes: 36 additions & 0 deletions Tests/YCoreUITests/Protocols/ColorableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,42 @@ final class ColorableTests: XCTestCase {
}

func testMissingColor() {
YCoreUI.isLoggingEnabled = false

PrimaryColors.allCases.forEach {
XCTAssertNil($0.loadColor())
XCTAssertEqual($0.color, PrimaryColors.fallbackColor)
}

YCoreUI.isLoggingEnabled = true
}

func test_calculateName_deliversCorrectName() {
PrimaryColors.allCases.forEach {
XCTAssertEqual($0.calculateName(), $0.rawValue)
}

ErrorColors.allCases.forEach {
XCTAssertEqual($0.calculateName(), "Error/\($0.rawValue)")
}

WarningColors.allCases.forEach {
XCTAssertEqual($0.calculateName(), $0.rawValue)
}
}

func test_colorable_deliversCorrectColor() {
ErrorColors.allCases.forEach {
XCTAssertNotEqual($0.color, ErrorColors.fallbackColor)
}

WarningColors.allCases.forEach {
XCTAssertNotEqual($0.color, WarningColors.fallbackColor)
}
}

func test_colorable_deliversDefaultFallbackColor() {
XCTAssertEqual(DefaultColors.defaultCase.color, DefaultColors.fallbackColor)
}
}

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

static var fallbackColor: UIColor { .systemPurple }
}

enum DefaultColors: String, CaseIterable, Colorable {
case defaultCase
}
}

0 comments on commit 38f2a28

Please sign in to comment.