Skip to content

Commit

Permalink
[CM-1317] add rendering mode support (#60)
Browse files Browse the repository at this point in the history
* [ISSUE] add rendering mode support

* [UPDATE] added lint rule as in new swiftLint we get warnings even for disabled rules for file

[UPDATE] add disable rule

* [UPDATE] reenabled function length rule

[UPDATE] added     // swiftlint:disable superfluous_disable_command

* [UPDATE] review comments resolved

* [UPDATE] rendering default is .automatic

* Update UIView+constrainEdgesTests.swift

* Update UIView+constrainEdgesTests.swift

* [CM-1317] Update documentation and unit tests

---------

Co-authored-by: Mark Pospesel <mark.pospesel@ymedialabs.com>
  • Loading branch information
SahilSainiYML and Mark Pospesel committed Apr 11, 2023
1 parent 962c919 commit ec33ac3
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 38 deletions.
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 }
}
}

0 comments on commit ec33ac3

Please sign in to comment.