From 2e043ec545724b56df7117829f08344da4b00031 Mon Sep 17 00:00:00 2001 From: Matt Maddux Date: Thu, 20 Feb 2020 09:49:20 -0600 Subject: [PATCH] Better icon and style fallback and messaging MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Missing icons will now fall back to “question-circle” available in the FA free set, and an error message will be printed. If no style is specified and “regular” is not available, the first available style will be used instead, but silently. If a style is specified but unavailable, same behavior as above, but an error message will be printed to log. --- Sources/FASwiftUI/FASearchHolder.swift | 6 +---- Sources/FASwiftUI/FAText.swift | 34 +++++++++++++++++--------- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/Sources/FASwiftUI/FASearchHolder.swift b/Sources/FASwiftUI/FASearchHolder.swift index ab6ac6e..d0a151a 100644 --- a/Sources/FASwiftUI/FASearchHolder.swift +++ b/Sources/FASwiftUI/FASearchHolder.swift @@ -12,11 +12,7 @@ import SwiftUI class FASearchHolder: ObservableObject { - @Published var searchQuery: String = "" { - didSet { - print("Set: \(searchQuery)") - } - } + @Published var searchQuery: String = "" @Published var searchResults: [String: FAIcon]? private var keyboardVisible: Bool = false diff --git a/Sources/FASwiftUI/FAText.swift b/Sources/FASwiftUI/FAText.swift index aa3b12f..4afc4e4 100644 --- a/Sources/FASwiftUI/FAText.swift +++ b/Sources/FASwiftUI/FAText.swift @@ -11,23 +11,35 @@ import SwiftUI public struct FAText: View { var iconName: String - private var icon: FAIcon { - return FontAwesome.shared.icon(byName: iconName) ?? FontAwesome.shared.icon(byName: "question-square")! - } + private var icon: FAIcon var size: CGFloat - var style: FAStyle? + var style: FAStyle private var weight: Font.Weight { - if let style = style { - return style.weight - } else { - return .regular - } + return style.weight } public init(iconName: String, size: CGFloat, style: FAStyle? = nil) { - self.iconName = iconName.hasPrefix("fa-") ? String(iconName.dropFirst(3)) : iconName self.size = size - self.style = style + self.style = style ?? .regular + self.iconName = iconName.hasPrefix("fa-") ? String(iconName.dropFirst(3)) : iconName + + if let icon = FontAwesome.shared.icon(byName: self.iconName) { + self.icon = icon + } else { + self.icon = FontAwesome.shared.icon(byName: "question-circle")! + self.style = .regular + print("FASwiftUI: Icon \"\(iconName)\" not found. Check list at https://fontawesome.com/icons for set availability.") + } + + if !self.icon.styles.contains(self.style) { + let fallbackStyle = self.icon.styles.first! + if fallbackStyle != .brands && style != nil { + print("FASwiftUI: Style \"\(style ?? .regular)\" not available for icon \"\(iconName)\", using \"\(fallbackStyle)\". Check list at https://fontawesome.com/icons for set availability.") + } else if self.style != .regular && style != nil { + print("FASwiftUI: Icon \"\(iconName)\" is part of the brands set and doesn't support alternate styles. Check list at https://fontawesome.com/icons for set availability.") + } + self.style = fallbackStyle + } } public var body: some View {