Skip to content

Commit

Permalink
[TEMPURA-2186] Custom icons support (#2132)
Browse files Browse the repository at this point in the history
* allow custom name and bundle for icons

* add test for custom icons

* Record snapshots

* update BPKIcon template

* Update BPKIcons.swift

* record snapshots

* Updated snapshots

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
pablo-skyscanner and github-actions[bot] authored Jan 9, 2025
1 parent d7679d1 commit def0b5f
Show file tree
Hide file tree
Showing 15 changed files with 57 additions and 5 deletions.
7 changes: 7 additions & 0 deletions Backpack-SwiftUI/Icons/Classes/BPKIcon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,16 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import Backpack_Common

public struct BPKIcon {
public let name: String
let bundle: Bundle

init (name: String, bundle: Bundle = BPKCommonBundle.iconsBundle) {
self.name = name
self.bundle = bundle
}

public enum Size {
case large, small
Expand Down
5 changes: 2 additions & 3 deletions Backpack-SwiftUI/Icons/Classes/BPKIconView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,10 @@ private extension BPKIcon.Size {
private extension Image {
init(icon: BPKIcon, size: BPKIcon.Size = .small, shouldEnableAccessibility: Bool) {
let iconName = "\(icon.name)-\(size.suffix)"
let bundle = BPKCommonBundle.iconsBundle
if shouldEnableAccessibility {
self.init(iconName, bundle: bundle)
self.init(iconName, bundle: icon.bundle)
} else {
self.init(decorative: iconName, bundle: bundle)
self.init(decorative: iconName, bundle: icon.bundle)
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion Backpack-SwiftUI/Icons/Classes/Generated/BPKIcons.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,12 @@ public extension BPKIcon {

public extension BPKIcon {
// swiftlint:disable function_body_length cyclomatic_complexity
static func named(_ iconName: String) -> BPKIcon? {
static func named(_ iconName: String, bundle: Bundle? = nil) -> BPKIcon? {

if let bundle {
return BPKIcon(name: iconName, bundle: bundle)
}

switch iconName {
case "accessibility": return .accessibility
case "account--add": return .accountAdd
Expand Down
10 changes: 10 additions & 0 deletions Backpack-SwiftUI/Tests/Icons/BPKIconViewTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,14 @@ class BPKIconViewTests: XCTestCase {
func testAutoMirrorSmallIcon() {
assertSnapshot(BPKIconView(.arrowLeft, size: .small))
}

func testCustomSmallIcon() {
let bundle = Bundle(for: BPKIconViewTests.self)
assertSnapshot(BPKIconView(BPKIcon(name: "custom-icon", bundle: TestsBundle.bundle!), size: .small))
}

func testCustomLargeIcon() {
let bundle = Bundle(for: BPKIconViewTests.self)
assertSnapshot(BPKIconView(BPKIcon(name: "custom-icon", bundle: TestsBundle.bundle!), size: .large))
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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" : "custom-icon-lg.svg",
"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" : "custom-icon-sm.svg",
"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.
7 changes: 6 additions & 1 deletion templates/swiftui/BPKIcons.njk
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ public extension BPKIcon {

public extension BPKIcon {
// swiftlint:disable function_body_length cyclomatic_complexity
static func named(_ iconName: String) -> BPKIcon? {
static func named(_ iconName: String, bundle: Bundle? = nil) -> BPKIcon? {

if let bundle {
return BPKIcon(name: iconName, bundle: bundle)
}

switch iconName {
{% for icon in icons -%}
case "{{icon.file}}": return .{{icon.name}}
Expand Down

0 comments on commit def0b5f

Please sign in to comment.