1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 14:50:32 +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 {
static let prompt = "Country Name or Code"
@Binding var selectedCountry: Country
2021-09-25 13:48:22 +05:30
@StateObject private var store = Store(Country.allCases)
@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
var body: some View {
VStack {
2021-11-28 20:07:55 +05:30
#if !os(tvOS)
HStack {
2021-11-28 20:07:55 +05:30
if #available(iOS 15.0, macOS 12.0, *) {
2022-05-21 01:23:17 +05:30
TextField("Country", text: $query, prompt: Text(Self.prompt))
2021-11-28 20:07:55 +05:30
} else {
2022-05-21 01:23:17 +05:30
TextField(Self.prompt, text: $query)
2021-11-28 20:07:55 +05:30
}
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
}
#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))
}
}