2021-07-31 22:10:56 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
2021-08-19 22:38:31 +00:00
|
|
|
struct TrendingCountry: View {
|
2022-09-04 15:28:30 +00:00
|
|
|
static let prompt = "Country Name or Code".localized()
|
2021-09-26 22:19:50 +00:00
|
|
|
@Binding var selectedCountry: Country
|
2021-07-31 22:10:56 +00:00
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
@StateObject private var store = Store(Country.allCases)
|
2021-07-31 22:10:56 +00:00
|
|
|
|
|
|
|
@State private var query: String = ""
|
|
|
|
@State private var selection: Country?
|
|
|
|
|
2022-06-14 16:10:54 +00:00
|
|
|
@Environment(\.colorScheme) private var colorScheme
|
2021-11-28 14:37:55 +00:00
|
|
|
@Environment(\.presentationMode) private var presentationMode
|
2021-07-31 22:10:56 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
2021-11-28 14:37:55 +00:00
|
|
|
#if !os(tvOS)
|
2021-07-31 22:10:56 +00:00
|
|
|
HStack {
|
2021-11-28 14:37:55 +00:00
|
|
|
if #available(iOS 15.0, macOS 12.0, *) {
|
2022-05-20 19:53:17 +00:00
|
|
|
TextField("Country", text: $query, prompt: Text(Self.prompt))
|
2021-11-28 14:37:55 +00:00
|
|
|
} else {
|
2022-05-20 19:53:17 +00:00
|
|
|
TextField(Self.prompt, text: $query)
|
2021-11-28 14:37:55 +00:00
|
|
|
}
|
2021-08-16 15:52:42 +00:00
|
|
|
|
2021-07-31 22:10:56 +00:00
|
|
|
Button("Done") { selectCountryAndDismiss() }
|
2021-08-16 15:52:42 +00:00
|
|
|
.keyboardShortcut(.defaultAction)
|
|
|
|
.keyboardShortcut(.cancelAction)
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
|
|
|
.padding([.horizontal, .top])
|
|
|
|
#endif
|
2021-11-28 14:37:55 +00:00
|
|
|
countriesList
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
|
|
|
#if os(tvOS)
|
2022-06-14 16:10:54 +00:00
|
|
|
.searchable(text: $query, placement: .automatic, prompt: Text(Self.prompt))
|
|
|
|
.background(Color.background(scheme: colorScheme))
|
2021-07-31 22:10:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var countriesList: some View {
|
2021-11-28 14:37:55 +00:00
|
|
|
let list = ScrollViewReader { _ in
|
2021-07-31 22:10:56 +00:00
|
|
|
List(store.collection, selection: $selection) { country in
|
|
|
|
#if os(macOS)
|
|
|
|
Text(country.name)
|
|
|
|
.tag(country)
|
|
|
|
.id(country)
|
|
|
|
#else
|
|
|
|
Button(country.name) { selectCountryAndDismiss(country) }
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.onChange(of: query) { newQuery in
|
|
|
|
let results = Country.search(newQuery)
|
|
|
|
store.replace(results)
|
|
|
|
|
|
|
|
selection = results.first
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
return Group {
|
|
|
|
#if os(macOS)
|
|
|
|
if #available(macOS 12.0, *) {
|
|
|
|
list
|
|
|
|
.listStyle(.inset(alternatesRowBackgrounds: true))
|
|
|
|
} else {
|
|
|
|
list
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
list
|
|
|
|
#endif
|
|
|
|
}
|
2021-07-31 22:10:56 +00:00
|
|
|
#if os(macOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.padding(.bottom, 5)
|
2021-07-31 22:10:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectCountryAndDismiss(_ country: Country? = nil) {
|
2021-09-26 22:19:50 +00:00
|
|
|
if let selected = country ?? selection {
|
2021-07-31 22:10:56 +00:00
|
|
|
selectedCountry = selected
|
|
|
|
}
|
|
|
|
|
2021-11-28 14:37:55 +00:00
|
|
|
presentationMode.wrappedValue.dismiss()
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-19 22:38:31 +00:00
|
|
|
struct TrendingCountry_Previews: PreviewProvider {
|
2021-07-31 22:10:56 +00:00
|
|
|
static var previews: some View {
|
2021-08-19 22:38:31 +00:00
|
|
|
TrendingCountry(selectedCountry: .constant(.pl))
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
|
|
|
}
|