Skip to content
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

[TEMPURA-2186] Custom icons support #2132

Merged
merged 9 commits into from
Jan 9, 2025
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
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
Loading