Skip to content

Commit

Permalink
Return non-optional Icon from .sfSymbol
Browse files Browse the repository at this point in the history
  • Loading branch information
bcylin committed May 30, 2020
1 parent 0b9e015 commit 1b671d1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
16 changes: 10 additions & 6 deletions Source/Model/Icon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public struct Icon: Equatable {
/// The image for the highlighted state.
public let highlightedImage: UIImage?

/// Icon with an image of the given name for the normal state.
// MARK: -

/// Returns `Icon` with an image of the given name for the normal state.
/// The "-highlighted" suffix is appended to the name for the highlighted image.
///
/// - Parameters:
Expand All @@ -49,27 +51,29 @@ public struct Icon: Equatable {
)
}

/// Icon with an image for the normal state.
/// Returns `Icon` with an image for the normal state. The image for the highlighted state is nil.
/// A method to provide backward compatiblility with the previous enum `case image(UIImage)`.
public static func image(_ image: UIImage) -> Self {
return Icon(image: image, highlightedImage: nil)
}

/// Icon with images for the normal and highlighted states.
/// Returns `Icon` with images for the normal and highlighted states.
/// A method to provide backward compatiblility with the previous enum `case images(normal: UIImage, highlighted: UIImage)`.
public static func images(normal: UIImage, highlighted: UIImage) -> Self {
return Icon(image: normal, highlightedImage: highlighted)
}

/// Returns `Icon.image` with the specified SF Symbol.
/// Returns `Icon` with the specified SF Symbol as the image for the normal state.
/// The image for the highlighted state is nil.
///
/// - Parameters:
/// - name: The name of the system symbol image.
/// - configuration: The configuration to specify traits and other details that define the variant of image.
@available(iOS 13.0, tvOS 13.0, *)
public static func sfSymbol(_ name: String, withConfiguration configuration: UIImage.Configuration? = nil) -> Self? {
public static func sfSymbol(_ name: String, withConfiguration configuration: UIImage.Configuration? = nil) -> Self {
// Make sure the image scales with the Dynamic Type settings.
let fallback = UIImage.SymbolConfiguration(textStyle: .body)
return UIImage(systemName: name, withConfiguration: configuration ?? fallback).map { .image($0) }
return Icon(image: UIImage(systemName: name, withConfiguration: configuration ?? fallback), highlightedImage: nil)
}

}
18 changes: 9 additions & 9 deletions Tests/Model/IconTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ internal final class IconTests: XCTestCase {
let name = "gear"

// When
let icon: Icon? = .sfSymbol(name)
let icon: Icon = .sfSymbol(name)

// Then
let expectedImage = UIImage(systemName: name, withConfiguration: UIImage.SymbolConfiguration(textStyle: .body))
XCTAssertEqual(icon?.image, expectedImage)
XCTAssertNil(icon?.highlightedImage)
XCTAssertEqual(icon.image, expectedImage)
XCTAssertNil(icon.highlightedImage)
}

@available(iOS 13.0, tvOS 13.0, *)
Expand All @@ -101,22 +101,22 @@ internal final class IconTests: XCTestCase {
let configuration = UIImage.SymbolConfiguration(scale: .large)

// When
let icon: Icon? = .sfSymbol(name, withConfiguration: configuration)
let icon: Icon = .sfSymbol(name, withConfiguration: configuration)

// Then
let expectedImage = UIImage(systemName: name, withConfiguration: configuration)
XCTAssertEqual(icon?.image, expectedImage)
XCTAssertNil(icon?.highlightedImage)
XCTAssertEqual(icon.image, expectedImage)
XCTAssertNil(icon.highlightedImage)
}

@available(iOS 13.0, tvOS 13.0, *)
func testSFSymbol_notFound() {
// When
let icon: Icon? = .sfSymbol("not-found")
let icon: Icon = .sfSymbol("not-found")

// Then
XCTAssertNil(icon?.image)
XCTAssertNil(icon?.highlightedImage)
XCTAssertNil(icon.image)
XCTAssertNil(icon.highlightedImage)
}

// MARK: - Equatable
Expand Down

0 comments on commit 1b671d1

Please sign in to comment.