Skip to content

Feature/cm 965 add image asset protocol #40

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 14 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
CM-965: Address review comments
  • Loading branch information
devkaranCT authored and Mark Pospesel committed Jan 3, 2023
commit d0c64a7ef70acc492fbd2cad76bbd8f51a9625ad
20 changes: 10 additions & 10 deletions Sources/YCoreUI/Protocols/ImageAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation
import UIKit

/// Any named image asset can be loaded from an asset catalog
/// Any named image asset can be loaded from an asset catalog.
///
/// All properties and functions have default implementations. At a minimum just have your string-based enum conform
/// to `ImageAsset` (and have an asset catalog with matching assets). If your enum and assets live inside a Swift
Expand All @@ -22,7 +22,8 @@ public protocol ImageAsset: RawRepresentable where RawValue == String {
/// Optional namespace for the image assets (default is `nil`).
static var namespace: String? { get }

/// Fallback image to use in case an image asset cannot be loaded (default is `.systemPink`)
/// 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 }

/// An image asset for this name value.
Expand All @@ -32,7 +33,7 @@ public protocol ImageAsset: RawRepresentable where RawValue == String {

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

Expand All @@ -43,23 +44,22 @@ extension ImageAsset {
/// Optional namespace for the image assets (default is `nil`)
public static var namespace: String? { nil }

/// fallback image to use in case an image asset cannot be loaded (default is `.systemPink`)
/// Fallback image to use in case an image asset cannot be loaded.
/// (default is 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
let rectangle = CGRect(x: 0, y: 0, width: 16, height: 16)
ctx.cgContext.setFillColor(UIColor.systemPink.cgColor)
ctx.cgContext.addRect(rectangle)
ctx.cgContext.drawPath(using: .fill)
UIColor.systemPink.setFill()
ctx.fill(CGRect(origin: .zero, size: renderer.format.bounds.size))
}
return image
}

/// Loads the named image
/// Loads the named image.
///
/// Default implementation uses `UIImage(named:in:compatibleWith:)` passing in the associated `namespace`
/// (prepended to `rawValue`) and `bundle`.
/// - Returns: The named image or else `nil` if the named asset cannot be loaded
/// - Returns: The named image or else `nil` if the named asset cannot be loaded.
public func loadImage() -> UIImage? {
let name: String
if let validNamespace = Self.namespace {
Expand Down
9 changes: 9 additions & 0 deletions Tests/YCoreUITests/Protocols/ImageAssetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ final class ImageAssetTests: XCTestCase {
func test_bundle() {
XCTAssertEqual(Flags.bundle, .module)
XCTAssertEqual(Icons.bundle, .module)
XCTAssertEqual(Missing.bundle, .main)
}

func test_namespace() {
Expand All @@ -39,9 +40,17 @@ final class ImageAssetTests: XCTestCase {
}
}

func test_missingImage() {
Missing.allCases.forEach {
XCTAssertNil($0.loadImage())
XCTAssertEqual($0.image, UIImage(systemName: "x.squareroot"))
}
}

func test_imageAsset_defaultValues() {
XCTAssertEqual(DefaultImageAssets.bundle, .main)
XCTAssertEqual(DefaultImageAssets.defaultCase.image.pngData(), DefaultImageAssets.fallbackImage.pngData())
XCTAssertNil(DefaultImageAssets.namespace)
}
}

Expand Down