Skip to content

[CM-1317] add rendering mode support #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ public extension UIColor {
return (red, green, blue, alpha)
}
}
// swiftlint: enable large_tuple
56 changes: 25 additions & 31 deletions Sources/YCoreUI/Protocols/SystemImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,46 +14,29 @@ import UIKit
/// to `SystemImage`. The raw value of the enum should match a sytem image name (e.g. `checkmark.seal`).
public protocol SystemImage: RawRepresentable where RawValue == String {
/// Fallback image to use in case a system image cannot be loaded.
/// (default is a 16 x 16 square filled with `.systemPink`)
static var fallbackImage: UIImage { get }

/// A system image for this name value.
///
/// Default implementation calls `loadImage` and nil-coalesces to `fallbackImage`.
var image: UIImage { get }

/// Image will scale according to the specified text style.
///
/// Default implementation is `.body`.
/// Return `nil` to not have the system image scale (not recommended).
static var textStyle: UIFont.TextStyle? { get }

/// Image configuration to be used in `loadImage()`.
///
/// Default implementation is `UIImage.SymbolConfiguration(textStyle: textStyle)`.
/// Returns `nil` when `textStyle` is `nil`.
/// Image configuration to be used to load the system image.
static var configuration: UIImage.Configuration? { get }

/// Rendering mode to use for the system image.
static var renderingMode: UIImage.RenderingMode { get }

/// Loads the named system image.
/// - Returns: The named system image or else `nil` if the system image cannot be loaded.
func loadImage() -> UIImage?
}

extension SystemImage {
/// Image will scale according to the specified text style.
public static var textStyle: UIFont.TextStyle? { .body }

/// Image configuration to be used in `loadImage()`.
///
/// Returns `nil` when `textStyle` is `nil`.
public static var configuration: UIImage.Configuration? {
guard let textStyle = textStyle else {
return nil
}
return UIImage.SymbolConfiguration(textStyle: textStyle)
}

/// Fallback image to use in case a system image cannot be loaded.
/// (default is a 16 x 16 square filled with `.systemPink`)
/// Returns a 16 x 16 square filled with `.systemPink`.
public static var fallbackImage: UIImage {
let renderer = UIGraphicsImageRenderer(size: CGSize(width: 16, height: 16))
let image = renderer.image { ctx in
Expand All @@ -63,17 +46,28 @@ extension SystemImage {
return image
}

/// Loads the named system image.
///
/// Default implementation uses `UIImage(systemName:)` passing in the associated `rawValue`.
/// - Returns: The named system image or else `nil` if the system image cannot be loaded.
/// Returns `.body` text style.
public static var textStyle: UIFont.TextStyle? { .body }

/// Returns `UIImage.SymbolConfiguration(textStyle:)`
/// passing in the specified `textStyle` or else returns `nil` if `textStyle` is `nil`.
public static var configuration: UIImage.Configuration? {
guard let textStyle = textStyle else {
return nil
}
return UIImage.SymbolConfiguration(textStyle: textStyle)
}

/// Returns `.automatic` rendering mode.
public static var renderingMode: UIImage.RenderingMode { .automatic }

/// Returns `UIImage(systemName:)` passing in the associated `rawValue` and `configuration`
/// and combined with the specified `renderingMode`.
public func loadImage() -> UIImage? {
UIImage(systemName: rawValue, withConfiguration: Self.configuration)
UIImage(systemName: rawValue, withConfiguration: Self.configuration)?.withRenderingMode(Self.renderingMode)
}

/// A system image for this name value.
///
/// Default implementation calls `loadImage` and nil-coalesces to `fallbackImage`.
/// Returns `loadImage()` nil-coalesced to `fallbackImage`.
public var image: UIImage {
guard let image = loadImage() else {
if YCoreUI.isLoggingEnabled {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import XCTest
@testable import YCoreUI

// Large tuples help us build unit test expectations concisely
// swiftlint:disable large_tuple
// swiftlint: disable large_tuple

final class CGFloatRoundedTests: XCTestCase {
typealias ScalingInputs = (
Expand Down Expand Up @@ -75,3 +75,4 @@ final class CGFloatRoundedTests: XCTestCase {
}
}
}
// swiftlint: enable large_tuple
3 changes: 2 additions & 1 deletion Tests/YCoreUITests/Extensions/UIKit/UIColor+WCAGTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import XCTest
@testable import YCoreUI

// Large tuples help us build unit test expectations concisely
// swiftlint:disable large_tuple
// swiftlint: disable large_tuple

final class UIColorWCAGTests: XCTestCase {
typealias ColorInputs = (foreground: UIColor, background: UIColor, context: WCAGContext)
Expand Down Expand Up @@ -183,3 +183,4 @@ final class UIColorWCAGTests: XCTestCase {
XCTAssertEqual(round(ratio1 * 100) / 100, round(ratio2 * 100) / 100)
}
}
// swiftlint: enable large_tuple
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import XCTest
@testable import YCoreUI

// Large tuples help us build unit test expectations concisely
// swiftlint:disable large_tuple
// swiftlint: disable large_tuple

final class UIColorRgbValueTests: XCTestCase {
typealias ColorTest = (color: UIColor, prefix: String?, isUppercase: Bool, output: String)
Expand Down Expand Up @@ -75,3 +75,4 @@ final class UIColorRgbValueTests: XCTestCase {
YCoreUI.isLoggingEnabled = true
}
}
// swiftlint: enable large_tuple
26 changes: 22 additions & 4 deletions Tests/YCoreUITests/Protocols/SystemImageTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@ final class SystemImageTests: XCTestCase {

YCoreUI.isLoggingEnabled = true
}


func test_systemImage_deliversDefaultFallback() {
XCTAssertEqual(DefaultSymbols.defaultCase.image.pngData(), DefaultSymbols.fallbackImage.pngData())
}

func test_defaultImageScaling() {
XCTAssertEqual(Symbols.textStyle, .body)
XCTAssertEqual(Symbols.configuration, UIImage.SymbolConfiguration(textStyle: .body))
Expand All @@ -47,9 +51,15 @@ final class SystemImageTests: XCTestCase {
XCTAssertEqual(SymbolCustomScaling.textStyle, .title1)
XCTAssertEqual(SymbolCustomScaling.configuration, UIImage.SymbolConfiguration(textStyle: .title1))
}

func test_systemImage_deliversDefaultFallback() {
XCTAssertEqual(DefaultSymbols.defaultCase.image.pngData(), DefaultSymbols.fallbackImage.pngData())

func test_defaultRenderingMode() {
XCTAssertEqual(Symbols.renderingMode, .automatic)
}

func test_customRenderingMode() {
SymbolCustomRenderingMode.allCases.forEach {
XCTAssertEqual($0.image.renderingMode, .alwaysOriginal)
}
}
}

Expand Down Expand Up @@ -88,4 +98,12 @@ extension SystemImageTests {

static var textStyle: UIFont.TextStyle? { .title1 }
}

enum SymbolCustomRenderingMode: String, CaseIterable, SystemImage {
case plus
case minus
case trash

static var renderingMode: UIImage.RenderingMode { .alwaysOriginal }
}
}