yattee/Apple TV/TrendingView.swift

75 lines
1.9 KiB
Swift
Raw Normal View History

2021-06-28 10:43:07 +00:00
import Siesta
2021-06-17 10:02:39 +00:00
import SwiftUI
struct TrendingView: View {
2021-06-28 10:43:07 +00:00
@State private var category: TrendingCategory = .default
@State private var country: Country = .pl
@State private var selectingCountry = false
2021-06-17 10:02:39 +00:00
2021-06-28 10:43:07 +00:00
@ObservedObject private var store = Store<[Video]>()
2021-06-26 09:39:35 +00:00
2021-06-28 10:43:07 +00:00
var resource: Resource {
InvidiousAPI.shared.trending(category: category, country: country)
}
init() {
resource.addObserver(store)
}
2021-06-17 10:02:39 +00:00
var body: some View {
Section {
2021-07-07 23:01:54 +00:00
VStack(alignment: .center, spacing: 2) {
HStack {
categoryButton
countryFlag
countryButton
2021-06-17 10:02:39 +00:00
}
.scaleEffect(0.85)
2021-06-28 10:43:07 +00:00
VideosView(videos: store.collection)
2021-06-17 10:02:39 +00:00
}
2021-06-28 10:43:07 +00:00
}.onAppear {
resource.loadIfNeeded()
2021-06-17 10:02:39 +00:00
}
}
var categoryButton: some View {
2021-06-26 09:39:35 +00:00
Button(category.name) {
2021-06-28 10:43:07 +00:00
setCategory(category.next())
}
.contextMenu {
ForEach(TrendingCategory.allCases) { category in
2021-06-28 10:43:07 +00:00
Button(category.name) { setCategory(category) }
}
}
}
var countryFlag: some View {
2021-06-26 09:39:35 +00:00
Text(country.flag)
.font(.system(size: 60))
}
var countryButton: some View {
2021-06-26 09:39:35 +00:00
Button(country.rawValue) {
selectingCountry.toggle()
2021-06-28 10:43:07 +00:00
resource.removeObservers(ownedBy: store)
}
2021-06-28 10:43:07 +00:00
.fullScreenCover(isPresented: $selectingCountry, onDismiss: { setCountry(country) }) {
2021-06-26 09:39:35 +00:00
TrendingCountrySelectionView(selectedCountry: $country)
}
}
2021-06-28 10:43:07 +00:00
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 10:02:39 +00:00
}