2021-08-01 03:40:56 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
2021-08-20 04:08:31 +05:30
|
|
|
struct TrendingCountry: View {
|
2022-09-04 20:58:30 +05:30
|
|
|
static let prompt = "Country Name or Code".localized()
|
2021-09-27 03:49:50 +05:30
|
|
|
@Binding var selectedCountry: Country
|
2021-08-01 03:40:56 +05:30
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
@StateObject private var store = Store(Country.allCases)
|
2021-08-01 03:40:56 +05:30
|
|
|
|
|
|
|
@State private var query: String = ""
|
|
|
|
@State private var selection: Country?
|
|
|
|
|
2022-06-14 21:40:54 +05:30
|
|
|
@Environment(\.colorScheme) private var colorScheme
|
2021-11-28 20:07:55 +05:30
|
|
|
@Environment(\.presentationMode) private var presentationMode
|
2021-08-01 03:40:56 +05:30
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
2021-11-28 20:07:55 +05:30
|
|
|
#if !os(tvOS)
|
2021-08-01 03:40:56 +05:30
|
|
|
HStack {
|
2023-10-15 17:05:23 +05:30
|
|
|
if #available(iOS 15.0, macOS 12.0, *) {
|
|
|
|
TextField("Country", text: $query, prompt: Text(Self.prompt))
|
|
|
|
} else {
|
|
|
|
TextField(Self.prompt, text: $query)
|
|
|
|
}
|
2021-08-16 21:22:42 +05:30
|
|
|
|
2021-08-01 03:40:56 +05:30
|
|
|
Button("Done") { selectCountryAndDismiss() }
|
2021-08-16 21:22:42 +05:30
|
|
|
.keyboardShortcut(.defaultAction)
|
|
|
|
.keyboardShortcut(.cancelAction)
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
.padding([.horizontal, .top])
|
|
|
|
#endif
|
2021-11-28 20:07:55 +05:30
|
|
|
countriesList
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
#if os(tvOS)
|
2022-06-14 21:40:54 +05:30
|
|
|
.searchable(text: $query, placement: .automatic, prompt: Text(Self.prompt))
|
|
|
|
.background(Color.background(scheme: colorScheme))
|
2021-08-01 03:40:56 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var countriesList: some View {
|
2021-11-28 20:07:55 +05:30
|
|
|
let list = ScrollViewReader { _ in
|
2021-08-01 03:40:56 +05:30
|
|
|
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 20:07:55 +05:30
|
|
|
return Group {
|
|
|
|
#if os(macOS)
|
2023-10-15 17:05:23 +05:30
|
|
|
if #available(macOS 12.0, *) {
|
|
|
|
list
|
|
|
|
.listStyle(.inset(alternatesRowBackgrounds: true))
|
|
|
|
} else {
|
|
|
|
list
|
|
|
|
}
|
2021-11-28 20:07:55 +05:30
|
|
|
#else
|
|
|
|
list
|
|
|
|
#endif
|
|
|
|
}
|
2021-08-01 03:40:56 +05:30
|
|
|
#if os(macOS)
|
2021-11-08 21:59:35 +05:30
|
|
|
.padding(.bottom, 5)
|
2021-08-01 03:40:56 +05:30
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
func selectCountryAndDismiss(_ country: Country? = nil) {
|
2021-09-27 03:49:50 +05:30
|
|
|
if let selected = country ?? selection {
|
2021-08-01 03:40:56 +05:30
|
|
|
selectedCountry = selected
|
|
|
|
}
|
|
|
|
|
2021-11-28 20:07:55 +05:30
|
|
|
presentationMode.wrappedValue.dismiss()
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-20 04:08:31 +05:30
|
|
|
struct TrendingCountry_Previews: PreviewProvider {
|
2021-08-01 03:40:56 +05:30
|
|
|
static var previews: some View {
|
2021-08-20 04:08:31 +05:30
|
|
|
TrendingCountry(selectedCountry: .constant(.pl))
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
}
|