1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Shared/Trending/TrendingCountry.swift

89 lines
2.6 KiB
Swift
Raw Normal View History

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()
@Binding var selectedCountry: Country
2021-09-25 13:48:22 +05:30
@StateObject private var store = Store(Country.allCases)
@State private var query = ""
@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
var body: some View {
VStack {
2021-11-28 20:07:55 +05:30
#if !os(tvOS)
HStack {
if #available(iOS 15.0, macOS 12.0, *) {
TextField("Country", text: $query, prompt: Text(Self.prompt))
} else {
TextField(Self.prompt, text: $query)
}
Button("Done") { selectCountryAndDismiss() }
.keyboardShortcut(.defaultAction)
.keyboardShortcut(.cancelAction)
}
.padding([.horizontal, .top])
#endif
2021-11-28 20:07:55 +05:30
countriesList
}
#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))
#endif
}
var countriesList: some View {
2021-11-28 20:07:55 +05:30
let list = 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
}
}
2021-11-28 20:07:55 +05:30
return Group {
#if os(macOS)
if #available(macOS 12.0, *) {
list
.listStyle(.inset(alternatesRowBackgrounds: true))
} else {
list
}
2021-11-28 20:07:55 +05:30
#else
list
#endif
}
#if os(macOS)
2021-11-08 21:59:35 +05:30
.padding(.bottom, 5)
#endif
}
func selectCountryAndDismiss(_ country: Country? = nil) {
if let selected = country ?? selection {
selectedCountry = selected
}
2021-11-28 20:07:55 +05:30
presentationMode.wrappedValue.dismiss()
}
}
2021-08-20 04:08:31 +05:30
struct TrendingCountry_Previews: PreviewProvider {
static var previews: some View {
2021-08-20 04:08:31 +05:30
TrendingCountry(selectedCountry: .constant(.pl))
}
}