-
Notifications
You must be signed in to change notification settings - Fork 7
[Issue-47] Add SystemImage protocol #48
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.