-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// SystemImage.swift | ||
// YCoreUI | ||
// | ||
// Created by Mark Pospesel on 3/8/23. | ||
// Copyright © 2023 Y Media Labs. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
|
||
/// Any string corresponding to a system image (SF Symbols). | ||
/// | ||
/// All properties and functions have default implementations. At a minimum just have your string-based enum conform | ||
/// 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 } | ||
|
||
/// 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 { | ||
/// Fallback image to use in case a system image 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 | ||
UIColor.systemPink.setFill() | ||
ctx.fill(CGRect(origin: .zero, size: renderer.format.bounds.size)) | ||
} | ||
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. | ||
public func loadImage() -> UIImage? { | ||
UIImage(systemName: rawValue) | ||
} | ||
|
||
/// A system image for this name value. | ||
/// | ||
/// Default implementation calls `loadImage` and nil-coalesces to `fallbackImage`. | ||
public var image: UIImage { loadImage() ?? Self.fallbackImage } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// SystemImageTests.swift | ||
// YCoreUI | ||
// | ||
// Created by Mark Pospesel on 3/8/23. | ||
// Copyright © 2023 Y Media Labs. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import YCoreUI | ||
|
||
final class SystemImageTests: XCTestCase { | ||
func test_fallbackImage_deliversImage() { | ||
XCTAssertNotNil(Symbols.fallbackImage) | ||
XCTAssertEqual(MissingSymbols.fallbackImage, UIImage(systemName: "x.squareroot")) | ||
} | ||
|
||
func test_loadImage_deliversImage() { | ||
Symbols.allCases.forEach { | ||
XCTAssertNotNil($0.loadImage()) | ||
} | ||
} | ||
|
||
func test_missingImage_deliversCustomFallback() { | ||
MissingSymbols.allCases.forEach { | ||
XCTAssertNil($0.loadImage()) | ||
XCTAssertEqual($0.image, UIImage(systemName: "x.squareroot")) | ||
} | ||
} | ||
|
||
func test_systemImage_deliversDefaultFallback() { | ||
XCTAssertEqual(DefaultSymbols.defaultCase.image.pngData(), DefaultSymbols.fallbackImage.pngData()) | ||
} | ||
} | ||
|
||
extension SystemImageTests { | ||
enum Symbols: String, CaseIterable, SystemImage { | ||
case checked = "checkmark.square" | ||
case unchecked = "square" | ||
case warning = "exclamationmark.triangle.fill" | ||
case error = "exclamationmark.octagon" | ||
} | ||
|
||
enum MissingSymbols: String, CaseIterable, SystemImage { | ||
case notHere | ||
case gone | ||
|
||
static var fallbackImage: UIImage { | ||
let image: UIImage! = UIImage(systemName: "x.squareroot") | ||
return image | ||
} | ||
} | ||
|
||
enum DefaultSymbols: String, CaseIterable, SystemImage { | ||
case defaultCase | ||
} | ||
} |