This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension String { | |
var localized: String { | |
let local = NSLocalizedString(self, comment: "") | |
guard !local.isEmpty else { | |
return self | |
} | |
return local | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
infix operator <=> | |
func <=><T: Comparable>(lhs: T, rhs: T) -> ComparisonResult { | |
if lhs < rhs { return .orderedAscending} | |
if lhs > rhs { return .orderedDescending } | |
return .orderedSame | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Searchable { | |
var keyPaths: [KeyPath<Self, String>] { get } | |
} | |
extension Searchable { | |
func matches(_ searchString: String) -> Bool { | |
for path in keyPaths { | |
let value = self[keyPath: path] | |
if value.localizedCaseInsensitiveContains(searchString) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Searchable { | |
var keyPaths: [KeyPath<Self, String>] { get } | |
} | |
extension Searchable { | |
func matches(_ searchString: String) -> Bool { | |
for path in keyPaths { | |
let value = self[keyPath: path] | |
if value.localizedCaseInsensitiveContains(searchString) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Searchable { | |
var keyPaths: [KeyPath<Self, String>] { get } | |
} | |
extension Searchable { | |
func matches(_ searchString: String) -> Bool { | |
for path in keyPaths { | |
let value = self[keyPath: path] | |
if value.localizedCaseInsensitiveContains(searchString) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func mirror(_ value: Any, using function: (Any) -> Void) { | |
let mirror = Mirror(reflecting: value) | |
for child in mirror.children { | |
function(child.value) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func ~=(lhs: Any, rhs: String) -> Bool { | |
let mirror = Mirror(reflecting: lhs) | |
for child in mirror.children { | |
guard let value = child.value as? String else { continue } | |
if value.localizedCaseInsensitiveContains(rhs) { | |
return true | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Bundle { | |
func decode<T: Decodable>(_ file: String) -> T { | |
guard let url = self.url(forResource: file, withExtension: nil) else { | |
fatalError("Failed to locate \(file) in bundle.") | |
} | |
guard let data = try? Data(contentsOf: url) else { | |
fatalError("Failed to load \(file) from bundle.") | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Environment(\.accessibilityReduceMotion) var reduceMotion | |
func withOptionalAnimation<Result>(_ animation: Animation? = .default, _ body: () throws -> Result) rethrows -> Result { | |
if UIAccessibility.isReduceMotionEnabled { | |
return try body() | |
} else { | |
return try withAnimation(animation, body) | |
} | |
} |