Skip to content

Commit 51a763a

Browse files
PanchamiShenoyPanchamiShenoy
authored andcommitted
[CM-929] Added colorable protocol
1 parent b245874 commit 51a763a

File tree

9 files changed

+206
-1
lines changed

9 files changed

+206
-1
lines changed

Package.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ let package = Package(
2222
),
2323
.testTarget(
2424
name: "YCoreUITests",
25-
dependencies: ["YCoreUI"]
25+
dependencies: ["YCoreUI"],
26+
resources: [.process("Assets")]
2627
)
2728
]
2829
)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//
2+
// Colorable.swift
3+
//
4+
// Created by Y Media Labs on 25/10/22.
5+
//
6+
import UIKit
7+
8+
/// Protocol for string based enum that has names of color assets
9+
public protocol Colorable: RawRepresentable where RawValue == String {
10+
/// The bundle containing the localized strings for this enum
11+
static var bundle: Bundle { get }
12+
13+
/// Namespace for color asset
14+
static var namespace: String? { get }
15+
16+
/// Color to display error cases
17+
static var fallbackColor: UIColor { get }
18+
19+
/// Color to be displayed based on namespace
20+
/// - Returns: Color to be displayed
21+
func loadColor() -> UIColor?
22+
23+
/// Color to be displayed
24+
var color: UIColor { get }
25+
}
26+
27+
/// Default implementation the `Colorable`
28+
extension Colorable {
29+
/// The bundle containing the localized strings
30+
static var bundle: Bundle { .main }
31+
32+
/// Namespace for color asset
33+
static var namespace: String? { nil }
34+
35+
/// Color to display error cases
36+
static var fallbackColor: UIColor { .systemPink }
37+
38+
/// Color to be displayed based on namespace
39+
/// - Returns: Color to be displayed
40+
func loadColor() -> UIColor? {
41+
let name: String
42+
if let validNamespace = Self.namespace {
43+
name = "\(validNamespace)/\(rawValue)"
44+
} else {
45+
name = rawValue
46+
}
47+
return UIColor(named: name, in: Self.bundle, compatibleWith: nil)
48+
}
49+
50+
/// Color to be displayed
51+
var color: UIColor { loadColor() ?? Self.fallbackColor }
52+
53+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"info" : {
3+
"author" : "xcode",
4+
"version" : 1
5+
}
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"info" : {
3+
"author" : "xcode",
4+
"version" : 1
5+
},
6+
"properties" : {
7+
"provides-namespace" : true
8+
}
9+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"colors" : [
3+
{
4+
"color" : {
5+
"color-space" : "srgb",
6+
"components" : {
7+
"alpha" : "1.000",
8+
"blue" : "0.824",
9+
"green" : "0.835",
10+
"red" : "1.000"
11+
}
12+
},
13+
"idiom" : "universal"
14+
}
15+
],
16+
"info" : {
17+
"author" : "xcode",
18+
"version" : 1
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"colors" : [
3+
{
4+
"color" : {
5+
"color-space" : "srgb",
6+
"components" : {
7+
"alpha" : "1.000",
8+
"blue" : "0.945",
9+
"green" : "0.949",
10+
"red" : "1.000"
11+
}
12+
},
13+
"idiom" : "universal"
14+
}
15+
],
16+
"info" : {
17+
"author" : "xcode",
18+
"version" : 1
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"colors" : [
3+
{
4+
"color" : {
5+
"color-space" : "srgb",
6+
"components" : {
7+
"alpha" : "1.000",
8+
"blue" : "0.690",
9+
"green" : "0.784",
10+
"red" : "0.984"
11+
}
12+
},
13+
"idiom" : "universal"
14+
}
15+
],
16+
"info" : {
17+
"author" : "xcode",
18+
"version" : 1
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"colors" : [
3+
{
4+
"color" : {
5+
"color-space" : "srgb",
6+
"components" : {
7+
"alpha" : "1.000",
8+
"blue" : "0.780",
9+
"green" : "0.820",
10+
"red" : "0.949"
11+
}
12+
},
13+
"idiom" : "universal"
14+
}
15+
],
16+
"info" : {
17+
"author" : "xcode",
18+
"version" : 1
19+
}
20+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//
2+
// ColorableProtocolTest.swift
3+
//
4+
//
5+
// Created by Y Media Labs on 26/10/22.
6+
//
7+
8+
import XCTest
9+
@testable import YCoreUI
10+
11+
final class ColorableProtocolTest: XCTestCase {
12+
13+
func testColorsWithoutNamespace() {
14+
WarningColors.allCases.forEach {
15+
let color = $0.color
16+
XCTAssertNotEqual(color, .systemPink)
17+
}
18+
}
19+
20+
func testColorsWithNamespace() {
21+
ErrorColors.allCases.forEach {
22+
let color = $0.color
23+
XCTAssertNotEqual(color, .systemPink)
24+
}
25+
}
26+
27+
func testInvalidColor() {
28+
PrimaryColors.allCases.forEach {
29+
let color = $0.color
30+
XCTAssertEqual(color, .systemPink)
31+
}
32+
}
33+
34+
35+
enum ErrorColors: String, CaseIterable, Colorable {
36+
case error50
37+
case error100
38+
39+
public static var bundle: Bundle { .module }
40+
}
41+
42+
enum WarningColors: String, CaseIterable, Colorable {
43+
case warning50
44+
case warning100
45+
46+
public static var bundle: Bundle { .module }
47+
}
48+
49+
enum PrimaryColors: String, CaseIterable, Colorable {
50+
case primary50
51+
case primary100
52+
53+
public static var bundle: Bundle { .module }
54+
}
55+
56+
}

0 commit comments

Comments
 (0)