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 all 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
77 changes: 77 additions & 0 deletions Sources/YCoreUI/Protocols/ImageAsset.swift
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 }
}
6 changes: 6 additions & 0 deletions Tests/YCoreUITests/Assets/Images.xcassets/Contents.json
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 Tests/YCoreUITests/Assets/Images.xcassets/Icons/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"info" : {
"author" : "xcode",
"version" : 1
},
"properties" : {
"provides-namespace" : true
}
}
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
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
88 changes: 88 additions & 0 deletions Tests/YCoreUITests/Protocols/ImageAssetTests.swift
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())
}
}

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)
}
}

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
}
}
}