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 3 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
3 changes: 2 additions & 1 deletion Sources/YCoreUI/Extensions/UIKit/UIColor+rgbComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit

// This relatively simple tuple (four float values representing the color channels)
// is already a released public api.
// swiftlint: disable large_tuple
// swiftlint:disable superfluous_disable_command large_tuple

/// Tuple representing Red, Green, Blue, and Alpha color channel components
public typealias RGBAComponents = (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat)
Expand All @@ -29,3 +29,4 @@ public extension UIColor {
return (red, green, blue, alpha)
}
}
// swiftlint: enable superfluous_disable_command large_tuple
16 changes: 13 additions & 3 deletions Sources/YCoreUI/Protocols/ImageAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ public protocol ImageAsset: RawRepresentable where RawValue == String {

/// Optional namespace for the image assets (default is `nil`).
static var namespace: String? { get }


/// Optional renderingMode to use for the image (default is `nil`)
static var renderingMode: UIImage.RenderingMode? { get }

/// Fallback image to use in case an image asset cannot be loaded.
/// (default is a 16 x 16 square filled with `.systemPink`)
static var fallbackImage: UIImage { get }
Expand All @@ -39,7 +42,10 @@ public protocol ImageAsset: RawRepresentable where RawValue == String {
extension ImageAsset {
/// The bundle containing the image assets for this enum (default is `.main`)
public static var bundle: Bundle { .main }


/// Optional renderingMode to use for the image (default is `nil`)
public static var renderingMode: UIImage.RenderingMode? { nil }

/// Optional namespace for the image assets (default is `nil`)
public static var namespace: String? { nil }

Expand All @@ -60,7 +66,11 @@ extension ImageAsset {
/// (prepended to `rawValue`) and `bundle`.
/// - Returns: The named image or else `nil` if the named asset cannot be loaded.
public func loadImage() -> UIImage? {
UIImage(named: calculateName(), in: Self.bundle, compatibleWith: nil)
let image = UIImage(named: calculateName(), in: Self.bundle, compatibleWith: nil)
guard let renderingMode = Self.renderingMode else {
return image
}
return image?.withRenderingMode(renderingMode)
}

internal func calculateName() -> String {
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 superfluous_disable_command large_tuple

final class CGFloatRoundedTests: XCTestCase {
typealias ScalingInputs = (
Expand Down Expand Up @@ -75,3 +75,4 @@ final class CGFloatRoundedTests: XCTestCase {
}
}
}
// swiftlint: enable large_tuple superfluous_disable_command
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 superfluous_disable_command 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 superfluous_disable_command
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 superfluous_disable_command 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 superfluous_disable_command
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import XCTest
@testable import YCoreUI

// swiftlint:disable superfluous_disable_command type_body_length

final class UIViewConstrainAnchorTests: XCTestCase {
func testXAxisConstraints() {
let (sut, relations) = makeSUT()
Expand Down Expand Up @@ -378,3 +380,4 @@ private extension MockLayoutContainer {
addSubview(view2)
}
}
// swiftlint:enable superfluous_disable_command type_body_length
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import XCTest

// swiftlint:disable superfluous_disable_command
final class UIViewConstrainEdgesTests: XCTestCase {
func testSimple() {
let (sut, insets) = makeSUT()
Expand Down Expand Up @@ -50,7 +51,7 @@ final class UIViewConstrainEdgesTests: XCTestCase {
XCTAssertEqual(trailing?.secondAttribute, .trailing)
XCTAssertEqual(trailing?.constant, -insets.trailing)
}

// swiftlint:disable function_body_length
func testPartialEdges() {
let (sut, _) = makeSUT()
let edges: [NSDirectionalRectEdge] = [
Expand Down Expand Up @@ -114,7 +115,8 @@ final class UIViewConstrainEdgesTests: XCTestCase {
button.removeFromSuperview()
}
}

// swiftlint:enable function_body_length

func testRelation() {
let (sut, _) = makeSUT()
let relations: [(input: NSLayoutConstraint.Relation, inverse: NSLayoutConstraint.Relation)] = [
Expand Down Expand Up @@ -176,3 +178,4 @@ private extension UIViewConstrainEdgesTests {
return (container, insets)
}
}
// swiftlint: enable superfluous_disable_command
15 changes: 15 additions & 0 deletions Tests/YCoreUITests/Protocols/ImageAssetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ final class ImageAssetTests: XCTestCase {
XCTAssertNotEqual($0.image.pngData(), DefaultImageAssets.fallbackImage.pngData())
}
}

func test_loadImageWithRenderingMode() {
RenderMode.allCases.forEach {
XCTAssertNotNil($0.loadImage())
}
}

func test_missingImage() {
YCoreUI.isLoggingEnabled = false
Expand Down Expand Up @@ -100,4 +106,13 @@ extension ImageAssetTests {
return image
}
}

enum RenderMode: String, CaseIterable, ImageAsset {
case unitedStates = "flag_us"
case india = "flag_in"
case switzerland = "flag_ch"

static var bundle: Bundle { .module }
static var renderingMode: UIImage.RenderingMode? { .alwaysOriginal }
}
}