mirror of
https://github.com/yattee/yattee.git
synced 2025-08-05 02:04:07 +00:00
Minor fixes, split files into folders
This commit is contained in:
104
Shared/Trending/TrendingCountry.swift
Normal file
104
Shared/Trending/TrendingCountry.swift
Normal file
@@ -0,0 +1,104 @@
|
||||
import SwiftUI
|
||||
|
||||
struct TrendingCountry: View {
|
||||
static let prompt = "Country Name or Code"
|
||||
@Binding var selectedCountry: Country?
|
||||
|
||||
@ObservedObject private var store = Store(Country.allCases)
|
||||
|
||||
@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 {
|
||||
TextField("Country", text: $query, prompt: Text(TrendingCountry.prompt))
|
||||
.focused($countryIsFocused)
|
||||
|
||||
Button("Done") { selectCountryAndDismiss() }
|
||||
.keyboardShortcut(.defaultAction)
|
||||
.keyboardShortcut(.cancelAction)
|
||||
}
|
||||
.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)
|
||||
.searchable(text: $query, placement: searchPlacement, prompt: Text(TrendingCountry.prompt))
|
||||
#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) {
|
||||
let selected = country ?? selection
|
||||
|
||||
if selected != nil {
|
||||
selectedCountry = selected
|
||||
}
|
||||
|
||||
dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
struct TrendingCountry_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
TrendingCountry(selectedCountry: .constant(.pl))
|
||||
}
|
||||
}
|
141
Shared/Trending/TrendingView.swift
Normal file
141
Shared/Trending/TrendingView.swift
Normal file
@@ -0,0 +1,141 @@
|
||||
import Siesta
|
||||
import SwiftUI
|
||||
|
||||
struct TrendingView: View {
|
||||
@State private var category: TrendingCategory = .default
|
||||
@State private var country: Country! = .pl
|
||||
@State private var selectingCountry = false
|
||||
|
||||
@ObservedObject private var store = Store<[Video]>()
|
||||
|
||||
var resource: Resource {
|
||||
InvidiousAPI.shared.trending(category: category, country: country)
|
||||
}
|
||||
|
||||
init() {
|
||||
resource.addObserver(store)
|
||||
}
|
||||
|
||||
var toolbar: some View {
|
||||
HStack {
|
||||
HStack {
|
||||
Text("Category")
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
categoryButton
|
||||
}
|
||||
|
||||
#if os(iOS)
|
||||
Spacer()
|
||||
#endif
|
||||
|
||||
HStack {
|
||||
Text("Country")
|
||||
.foregroundColor(.secondary)
|
||||
|
||||
countryButton
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var body: some View {
|
||||
Section {
|
||||
VStack(alignment: .center, spacing: 2) {
|
||||
#if os(tvOS)
|
||||
toolbar
|
||||
.scaleEffect(0.85)
|
||||
#endif
|
||||
|
||||
VideosView(videos: store.collection)
|
||||
|
||||
#if os(iOS)
|
||||
toolbar
|
||||
.font(.system(size: 14))
|
||||
.padding(.horizontal)
|
||||
.padding(.vertical, 10)
|
||||
.overlay(Divider().offset(x: 0, y: -2), alignment: .topTrailing)
|
||||
.transaction { t in t.animation = .none }
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#if os(tvOS)
|
||||
.fullScreenCover(isPresented: $selectingCountry, onDismiss: { setCountry(country) }) {
|
||||
TrendingCountry(selectedCountry: $country)
|
||||
}
|
||||
#else
|
||||
.sheet(isPresented: $selectingCountry, onDismiss: { setCountry(country) }) {
|
||||
TrendingCountry(selectedCountry: $country)
|
||||
#if os(macOS)
|
||||
.frame(minWidth: 400, minHeight: 400)
|
||||
#endif
|
||||
}
|
||||
.navigationTitle("Trending")
|
||||
#endif
|
||||
#if os(macOS)
|
||||
.toolbar {
|
||||
ToolbarItemGroup {
|
||||
categoryButton
|
||||
countryButton
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
.onAppear {
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
var categoryButton: some View {
|
||||
#if os(tvOS)
|
||||
Button(category.name) {
|
||||
setCategory(category.next())
|
||||
}
|
||||
.contextMenu {
|
||||
ForEach(TrendingCategory.allCases) { category in
|
||||
Button(category.name) { setCategory(category) }
|
||||
}
|
||||
}
|
||||
#else
|
||||
Menu(category.name) {
|
||||
ForEach(TrendingCategory.allCases) { category in
|
||||
Button(action: { setCategory(category) }) {
|
||||
if category == self.category {
|
||||
Label(category.name, systemImage: "checkmark")
|
||||
} else {
|
||||
Text(category.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
var countryButton: some View {
|
||||
Button(action: {
|
||||
selectingCountry.toggle()
|
||||
resource.removeObservers(ownedBy: store)
|
||||
}) {
|
||||
Text("\(country.flag) \(country.id)")
|
||||
}
|
||||
}
|
||||
|
||||
fileprivate func setCategory(_ category: TrendingCategory) {
|
||||
resource.removeObservers(ownedBy: store)
|
||||
self.category = category
|
||||
resource.addObserver(store)
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
|
||||
fileprivate func setCountry(_ country: Country) {
|
||||
self.country = country
|
||||
resource.addObserver(store)
|
||||
resource.loadIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
struct TrendingView_Previews: PreviewProvider {
|
||||
static var previews: some View {
|
||||
TrendingView()
|
||||
.environmentObject(NavigationState())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user