codeandtheory / YCoreUI

Core components for iOS and tvOS to accelerate building user interfaces in code.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add `Colorable` protocol

mpospese opened this issue · comments

Intro

It's a common pattern to have a string-based enum to hold the names of color assets in an asset catalog. In each case you need to add some sort of computed property or func to return the color loaded from the asset catalog. It would be nice to have a protocol that we could simply have our enum conform to and skip having to write that implementation again and again. (This is especially true for cases where you might wish to organize your colors into multiple enums, which would require writing that color loading property/func once per enum.)

So let's declare a public protocol named Colorable.
(It is patterned off the protocol Localizable that is already part of YCoreUI. Colorable will do for color assets what Localizable does for string assets).

Tasks

  • declare a public protocol Colorable
    • declare a static get var bundle of type Bundle
    • declare a static get var namespace of type String?
    • declare a static get var fallbackColor of type UIColor
    • declare a loadColor func that takes no parameters and returns UIColor?
    • declare a get var color of type UIColor
  • add default implementations to all 4
  • bundle defaults to Bundle.main
  • namespace defaults to nil
  • fallbackColor defaults to UIColor.systemPink (we want something obvious to flag errors but also something unlikely to be actually used, so that we can unit tests that our colors load without fallback)
  • loadColor attempts to load the named color using (optional namespace prepended to) rawValue and bundle (but returns nil if the color does not exist in the asset catalog).
  • color uses loadColor but nil-coalesces the result back to fallbackColor so that it always returns something.

Acceptance Criteria

  • Implement Colorable as described in Tasks above
  • SwiftLint analysis has 0 violations
  • fully unit test the new code by adding colors to an asset catalog in the test target only, declaring an enum that lists the names of the colors, and test that it loads. Also test expected failures (e.g. blank name or random name does not load, wrong bundle, etc)
  • fully document all public interfaces on the new code (as per Jazzy documentation coverage tests)