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
varbundle
of typeBundle
- declare a static
get
varnamespace
of typeString?
- declare a static
get
varfallbackColor
of typeUIColor
- declare a
loadColor
func that takes no parameters and returnsUIColor?
- declare a
get
varcolor
of typeUIColor
- declare a static
- add default implementations to all 4
bundle
defaults toBundle.main
namespace
defaults tonil
fallbackColor
defaults toUIColor.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 withoutfallback
)loadColor
attempts to load the named color using (optionalnamespace
prepended to)rawValue
andbundle
(but returnsnil
if the color does not exist in the asset catalog).color
usesloadColor
but nil-coalesces the result back tofallbackColor
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)