1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 13:50:32 +05:30
yattee/Shared/TrendingView.swift

90 lines
2.3 KiB
Swift
Raw Normal View History

2021-06-28 16:13:07 +05:30
import Siesta
2021-06-17 15:32:39 +05:30
import SwiftUI
struct TrendingView: View {
2021-06-28 16:13:07 +05:30
@State private var category: TrendingCategory = .default
@State private var country: Country = .pl
@State private var selectingCountry = false
2021-06-17 15:32:39 +05:30
2021-06-28 16:13:07 +05:30
@ObservedObject private var store = Store<[Video]>()
2021-06-26 15:09:35 +05:30
2021-06-28 16:13:07 +05:30
var resource: Resource {
InvidiousAPI.shared.trending(category: category, country: country)
}
init() {
resource.addObserver(store)
}
2021-06-17 15:32:39 +05:30
var body: some View {
Section {
2021-07-08 04:31:54 +05:30
VStack(alignment: .center, spacing: 2) {
2021-07-12 02:22:49 +05:30
#if os(tvOS)
HStack {
Text("Category")
.foregroundColor(.secondary)
2021-07-12 02:22:49 +05:30
categoryButton
2021-07-12 02:22:49 +05:30
Text("Country")
.foregroundColor(.secondary)
2021-07-12 02:22:49 +05:30
countryFlag
countryButton
}
.scaleEffect(0.85)
#endif
2021-06-17 15:32:39 +05:30
2021-06-28 16:13:07 +05:30
VideosView(videos: store.collection)
2021-06-17 15:32:39 +05:30
}
2021-07-12 02:22:49 +05:30
}
#if !os(tvOS)
.navigationTitle("Trending")
#endif
.onAppear {
2021-06-28 16:13:07 +05:30
resource.loadIfNeeded()
2021-06-17 15:32:39 +05:30
}
}
var categoryButton: some View {
2021-06-26 15:09:35 +05:30
Button(category.name) {
2021-06-28 16:13:07 +05:30
setCategory(category.next())
}
.contextMenu {
ForEach(TrendingCategory.allCases) { category in
2021-06-28 16:13:07 +05:30
Button(category.name) { setCategory(category) }
}
}
}
var countryFlag: some View {
2021-06-26 15:09:35 +05:30
Text(country.flag)
.font(.system(size: 60))
}
var countryButton: some View {
2021-06-26 15:09:35 +05:30
Button(country.rawValue) {
selectingCountry.toggle()
2021-06-28 16:13:07 +05:30
resource.removeObservers(ownedBy: store)
}
2021-07-12 02:22:49 +05:30
#if os(tvOS)
.fullScreenCover(isPresented: $selectingCountry, onDismiss: { setCountry(country) }) {
TrendingCountrySelectionView(selectedCountry: $country)
}
#endif
}
2021-06-28 16:13:07 +05:30
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()
}
2021-06-17 15:32:39 +05:30
}