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) {
|
|
|
|
HStack {
|
2021-07-09 20:23:53 +05:30
|
|
|
Text("Category")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
2021-06-25 04:18:28 +05:30
|
|
|
categoryButton
|
2021-07-09 20:23:53 +05:30
|
|
|
|
|
|
|
Text("Country")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
2021-06-25 04:18:28 +05:30
|
|
|
countryFlag
|
|
|
|
countryButton
|
2021-06-17 15:32:39 +05:30
|
|
|
}
|
|
|
|
.scaleEffect(0.85)
|
|
|
|
|
2021-06-28 16:13:07 +05:30
|
|
|
VideosView(videos: store.collection)
|
2021-06-17 15:32:39 +05:30
|
|
|
}
|
2021-06-28 16:13:07 +05:30
|
|
|
}.onAppear {
|
|
|
|
resource.loadIfNeeded()
|
2021-06-17 15:32:39 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 04:18:28 +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())
|
2021-06-25 04:18:28 +05:30
|
|
|
}
|
|
|
|
.contextMenu {
|
|
|
|
ForEach(TrendingCategory.allCases) { category in
|
2021-06-28 16:13:07 +05:30
|
|
|
Button(category.name) { setCategory(category) }
|
2021-06-25 04:18:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var countryFlag: some View {
|
2021-06-26 15:09:35 +05:30
|
|
|
Text(country.flag)
|
2021-06-25 04:18:28 +05:30
|
|
|
.font(.system(size: 60))
|
|
|
|
}
|
|
|
|
|
|
|
|
var countryButton: some View {
|
2021-06-26 15:09:35 +05:30
|
|
|
Button(country.rawValue) {
|
2021-06-25 04:18:28 +05:30
|
|
|
selectingCountry.toggle()
|
2021-06-28 16:13:07 +05:30
|
|
|
resource.removeObservers(ownedBy: store)
|
2021-06-25 04:18:28 +05:30
|
|
|
}
|
2021-06-28 16:13:07 +05:30
|
|
|
.fullScreenCover(isPresented: $selectingCountry, onDismiss: { setCountry(country) }) {
|
2021-06-26 15:09:35 +05:30
|
|
|
TrendingCountrySelectionView(selectedCountry: $country)
|
2021-06-25 04:18:28 +05:30
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|