2021-07-31 22:10:56 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
2021-08-19 22:38:31 +00:00
|
|
|
struct TrendingCountry: View {
|
2021-07-31 22:10:56 +00:00
|
|
|
static let prompt = "Country Name or Code"
|
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?
|
|
|
|
|
|
|
|
@FocusState var countryIsFocused
|
|
|
|
@Environment(\.dismiss) private var dismiss
|
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
VStack {
|
|
|
|
#if os(macOS)
|
|
|
|
HStack {
|
2021-08-19 22:38:31 +00:00
|
|
|
TextField("Country", text: $query, prompt: Text(TrendingCountry.prompt))
|
2021-07-31 22:10:56 +00:00
|
|
|
.focused($countryIsFocused)
|
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])
|
|
|
|
|
|
|
|
countriesList
|
|
|
|
#else
|
|
|
|
NavigationView {
|
|
|
|
countriesList
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItemGroup(placement: .navigationBarLeading) {
|
|
|
|
Button("Done") { selectCountryAndDismiss() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#if os(iOS)
|
|
|
|
.navigationBarTitle("Trending Country", displayMode: .automatic)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.onAppear {
|
|
|
|
countryIsFocused = true
|
|
|
|
}
|
|
|
|
.onSubmit { selectCountryAndDismiss() }
|
|
|
|
#if !os(macOS)
|
2021-08-19 22:38:31 +00:00
|
|
|
.searchable(text: $query, placement: searchPlacement, prompt: Text(TrendingCountry.prompt))
|
2021-07-31 22:10:56 +00:00
|
|
|
#endif
|
|
|
|
#if os(tvOS)
|
|
|
|
.background(.thinMaterial)
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var countriesList: some View {
|
|
|
|
ScrollViewReader { _ in
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(macOS)
|
|
|
|
.listStyle(.inset(alternatesRowBackgrounds: true))
|
|
|
|
.padding(.bottom, 5)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
var searchPlacement: SearchFieldPlacement {
|
|
|
|
#if os(iOS)
|
|
|
|
.navigationBarDrawer(displayMode: .always)
|
|
|
|
#else
|
|
|
|
.automatic
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
|
|
|
dismiss()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|