Last active
September 18, 2024 03:01
-
-
Save shaps80/890f374f67be14c561896f19da4e2a55 to your computer and use it in GitHub Desktop.
This file contains 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
import SwiftUI | |
public extension Picker where SelectionValue: CaseIterable, SelectionValue.AllCases: RandomAccessCollection, SelectionValue: RawRepresentable, SelectionValue.RawValue == String { | |
init(_ title: some StringProtocol, selection: Binding<SelectionValue>) where Label == Text, Content == ForEach<SelectionValue.AllCases, SelectionValue, Text> { | |
self.init(title, selection: selection) { | |
ForEach(SelectionValue.allCases, id: \.self) { element in | |
Text(element.rawValue) | |
} | |
} | |
} | |
init(_ titleKey: LocalizedStringKey, selection: Binding<SelectionValue>) where Label == Text, Content == ForEach<SelectionValue.AllCases, SelectionValue, Text> { | |
self.init(titleKey, selection: selection) { | |
ForEach(SelectionValue.allCases, id: \.self) { element in | |
Text(element.rawValue) | |
} | |
} | |
} | |
init(_ title: some StringProtocol, systemImage: String, selection: Binding<SelectionValue>) where Label == SwiftUI.Label<Text, Image>, Content == ForEach<SelectionValue.AllCases, SelectionValue, Text> { | |
self.init(title, systemImage: systemImage, selection: selection) { | |
ForEach(SelectionValue.allCases, id: \.self) { element in | |
Text(element.rawValue) | |
} | |
} | |
} | |
init(_ titleKey: LocalizedStringKey, systemImage: String, selection: Binding<SelectionValue>) where Label == SwiftUI.Label<Text, Image>, Content == ForEach<SelectionValue.AllCases, SelectionValue, Text> { | |
self.init(titleKey, systemImage: systemImage, selection: selection) { | |
ForEach(SelectionValue.allCases, id: \.self) { element in | |
Text(element.rawValue) | |
} | |
} | |
} | |
init(selection: Binding<SelectionValue>, @ViewBuilder label: () -> Label) where Content == ForEach<SelectionValue.AllCases, SelectionValue, Text> { | |
self.init(selection: selection, content: { | |
ForEach(SelectionValue.allCases, id: \.self) { element in | |
Text(element.rawValue) | |
} | |
}, label: label) | |
} | |
} | |
public extension Picker where SelectionValue: CaseIterable, SelectionValue.AllCases: RandomAccessCollection, SelectionValue.AllCases.Element: Hashable { | |
typealias Element = SelectionValue.AllCases.Element | |
init<C: View>(_ title: some StringProtocol, selection: Binding<SelectionValue>, @ViewBuilder content: @escaping (Element) -> C) where Label == Text, Content == ForEach<SelectionValue.AllCases, SelectionValue, C> { | |
self.init(title, selection: selection) { | |
ForEach(SelectionValue.allCases, id: \.self, content: content) | |
} | |
} | |
init<C: View>(_ titleKey: LocalizedStringKey, selection: Binding<SelectionValue>, @ViewBuilder content: @escaping (Element) -> C) where Label == Text, Content == ForEach<SelectionValue.AllCases, SelectionValue, C> { | |
self.init(titleKey, selection: selection) { | |
ForEach(SelectionValue.allCases, id: \.self, content: content) | |
} | |
} | |
init<C: View>(_ title: some StringProtocol, systemImage: String, selection: Binding<SelectionValue>, @ViewBuilder content: @escaping (Element) -> C) where Label == SwiftUI.Label<Text, Image>, Content == ForEach<SelectionValue.AllCases, SelectionValue, C> { | |
self.init(title, systemImage: systemImage, selection: selection) { | |
ForEach(SelectionValue.allCases, id: \.self, content: content) | |
} | |
} | |
init<C: View>(_ titleKey: LocalizedStringKey, systemImage: String, selection: Binding<SelectionValue>, @ViewBuilder content: @escaping (Element) -> C) where Label == SwiftUI.Label<Text, Image>, Content == ForEach<SelectionValue.AllCases, SelectionValue, C> { | |
self.init(titleKey, systemImage: systemImage, selection: selection) { | |
ForEach(SelectionValue.allCases, id: \.self, content: content) | |
} | |
} | |
init<C: View>(selection: Binding<SelectionValue>, @ViewBuilder content: @escaping (Element) -> C, @ViewBuilder label: () -> Label) where Content == ForEach<SelectionValue.AllCases, SelectionValue, C> { | |
self.init(selection: selection, content: { | |
ForEach(SelectionValue.allCases, id: \.self, content: content) | |
}, label: label) | |
} | |
} | |
public extension Picker where SelectionValue: CaseIterable, SelectionValue.AllCases: RandomAccessCollection, SelectionValue.AllCases.Element: Hashable { | |
init(_ title: some StringProtocol, selection: Binding<SelectionValue>, value: KeyPath<SelectionValue.AllCases.Element, String>) where Label == Text, Content == ForEach<SelectionValue.AllCases, SelectionValue, Text> { | |
self.init(title, selection: selection) { | |
ForEach(SelectionValue.allCases, id: \.self) { | |
Text($0[keyPath: value]) | |
} | |
} | |
} | |
init(_ titleKey: LocalizedStringKey, selection: Binding<SelectionValue>, value: KeyPath<SelectionValue.AllCases.Element, String>) where Label == Text, Content == ForEach<SelectionValue.AllCases, SelectionValue, Text> { | |
self.init(titleKey, selection: selection) { | |
ForEach(SelectionValue.allCases, id: \.self) { | |
Text($0[keyPath: value]) | |
} | |
} | |
} | |
} |
@shaps80 is it possible if Orientation name is LocalizedStringKey ?
It's does no support now, I've test it :-(
extension Orientation {
var name: LocalizedStringKey {
switch self {
case .portrait:
return "Portrait"
case .landscapeLeft:
return "Landscape Left"
case .landscapeRight:
return "Landscape Right"
}
}
}
Should be, pretty sure I included those inits as they're extremely useful and generally expected in SwiftUI. 👍
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: