-
Notifications
You must be signed in to change notification settings - Fork 7
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
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6c690a8
CM-965: Created "ImageAsset"
devkaranCT e7528d8
CM-965: Protocol 'ImageAsset' Added
devkaranCT 7f8e959
CM-965: Comment updated.
devkaranCT ce8e7b7
CM-965: 'ImageAsset' protocol extension added
devkaranCT e536512
CM-965: Default implementation for bundle and namespace updated.
devkaranCT cd77e0f
CM-965: 'loadImage()'. and image added in 'ImageAsset' extension
devkaranCT d743c97
CM-965: 'fallbackImage' updated
devkaranCT f1e62f6
CM-965: 'ImageAssetTests' created
devkaranCT f857ee4
CM-965: Test for 'addImageAsset'
devkaranCT 5f0d2f2
CM-965: Restructured folder directory in test target.
devkaranCT f097051
CM-965: Image asset default value test.
devkaranCT b935892
CM-965: Method renamed to snake case
devkaranCT 1994aaf
CM-965: Comment added for 'ImageAsset'
devkaranCT d0c64a7
CM-965: Address review comments
devkaranCT 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// | ||
// ImageAsset.swift | ||
// YCoreUI | ||
// | ||
// Created by Dev Karan on 19/12/22. | ||
// Copyright © 2022 Y Media Labs. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
/// 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 | ||
/// package, override `bundle` to return `.module`. If your assets are categorized within their asset catalog by | ||
/// a namespace, then override `namespace` to return the proper string prefix. | ||
public protocol ImageAsset: RawRepresentable where RawValue == String { | ||
/// The bundle containing the image assets for this enum (default is `.main`) | ||
static var bundle: Bundle { get } | ||
|
||
/// 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 a 16 x 16 square filled with `.systemPink`) | ||
static var fallbackImage: UIImage { get } | ||
|
||
/// An image asset for this name value. | ||
/// | ||
/// Default implementation calls `loadImage` and nil-coalesces to `fallbackImage`. | ||
var image: UIImage { get } | ||
|
||
/// Loads the image. | ||
/// | ||
/// - Returns: The named image or else `nil` if the named asset cannot be loaded. | ||
func loadImage() -> UIImage? | ||
} | ||
|
||
extension ImageAsset { | ||
/// The bundle containing the image assets for this enum (default is `.main`) | ||
public static var bundle: Bundle { .main } | ||
|
||
/// 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 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 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. | ||
public func loadImage() -> UIImage? { | ||
let name: String | ||
if let validNamespace = Self.namespace { | ||
name = "\(validNamespace)/\(rawValue)" | ||
} else { | ||
name = rawValue | ||
} | ||
return UIImage(named: name, in: Self.bundle, compatibleWith: nil) | ||
} | ||
|
||
/// An image asset for this name value. | ||
/// | ||
/// Default implementation calls `loadImage` and nil-coalesces to `fallbackImage`. | ||
public var image: UIImage { loadImage() ?? Self.fallbackImage } | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,6 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
Tests/YCoreUITests/Assets/Images.xcassets/Icons/Contents.json
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,9 @@ | ||
{ | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
}, | ||
"properties" : { | ||
"provides-namespace" : true | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
Tests/YCoreUITests/Assets/Images.xcassets/Icons/minus.imageset/Contents.json
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,12 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"filename" : "minus.png", | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+945 Bytes
Tests/YCoreUITests/Assets/Images.xcassets/Icons/minus.imageset/minus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions
12
Tests/YCoreUITests/Assets/Images.xcassets/Icons/plus.imageset/Contents.json
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,12 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"filename" : "plus.png", | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+956 Bytes
Tests/YCoreUITests/Assets/Images.xcassets/Icons/plus.imageset/plus.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions
12
Tests/YCoreUITests/Assets/Images.xcassets/flag_br.imageset/Contents.json
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,12 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"filename" : "flag_br.png", | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+973 Bytes
Tests/YCoreUITests/Assets/Images.xcassets/flag_br.imageset/flag_br.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions
12
Tests/YCoreUITests/Assets/Images.xcassets/flag_ch.imageset/Contents.json
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,12 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"filename" : "flag_ch.png", | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+958 Bytes
Tests/YCoreUITests/Assets/Images.xcassets/flag_ch.imageset/flag_ch.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions
12
Tests/YCoreUITests/Assets/Images.xcassets/flag_in.imageset/Contents.json
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,12 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"filename" : "flag_in.png", | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+962 Bytes
Tests/YCoreUITests/Assets/Images.xcassets/flag_in.imageset/flag_in.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions
12
Tests/YCoreUITests/Assets/Images.xcassets/flag_us.imageset/Contents.json
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,12 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"filename" : "flag_us.png", | ||
"idiom" : "universal" | ||
} | ||
], | ||
"info" : { | ||
"author" : "xcode", | ||
"version" : 1 | ||
} | ||
} |
Binary file added
BIN
+967 Bytes
Tests/YCoreUITests/Assets/Images.xcassets/flag_us.imageset/flag_us.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,88 @@ | ||
// | ||
// ImageAssetTests.swift | ||
// YCoreUI | ||
// | ||
// Created by Dev Karan on 21/12/22. | ||
// Copyright © 2022 Y Media Labs. All rights reserved. | ||
// | ||
|
||
import XCTest | ||
import YCoreUI | ||
|
||
final class ImageAssetTests: XCTestCase { | ||
func test_bundle() { | ||
XCTAssertEqual(Flags.bundle, .module) | ||
XCTAssertEqual(Icons.bundle, .module) | ||
XCTAssertEqual(Missing.bundle, .main) | ||
} | ||
|
||
func test_namespace() { | ||
XCTAssertNil(Flags.namespace) | ||
XCTAssertEqual(Icons.namespace, "Icons") | ||
XCTAssertNil(Missing.namespace) | ||
} | ||
|
||
func test_fallbackImage() { | ||
XCTAssertNotNil(Flags.fallbackImage) | ||
XCTAssertNotNil(Icons.fallbackImage) | ||
XCTAssertEqual(Missing.fallbackImage, UIImage(systemName: "x.squareroot")) | ||
} | ||
|
||
func test_loadImageWithNameSpace() { | ||
Icons.allCases.forEach { | ||
XCTAssertNotNil($0.loadImage()) | ||
} | ||
} | ||
|
||
func test_loadImageWithoutNameSpace() { | ||
Flags.allCases.forEach { | ||
XCTAssertNotNil($0.loadImage()) | ||
} | ||
} | ||
|
||
mpospese marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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()) | ||
mpospese marked this conversation as resolved.
Show resolved
Hide resolved
|
||
XCTAssertNil(DefaultImageAssets.namespace) | ||
} | ||
} | ||
|
||
enum DefaultImageAssets: String, CaseIterable, ImageAsset { | ||
case defaultCase | ||
} | ||
|
||
extension ImageAssetTests { | ||
enum Flags: String, CaseIterable, ImageAsset { | ||
case unitedStates = "flag_us" | ||
case india = "flag_in" | ||
case brazil = "flag_br" | ||
case switzerland = "flag_ch" | ||
|
||
static var bundle: Bundle { .module } | ||
} | ||
|
||
enum Icons: String, CaseIterable, ImageAsset { | ||
case plus | ||
case minus | ||
|
||
static var bundle: Bundle { .module } | ||
static var namespace: String? { "Icons" } | ||
} | ||
|
||
enum Missing: String, CaseIterable, ImageAsset { | ||
case notHere | ||
case gone | ||
|
||
static var fallbackImage: UIImage { | ||
let image: UIImage! = UIImage(systemName: "x.squareroot") | ||
return image | ||
} | ||
} | ||
} |
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.