2021-06-28 16:13:07 +05:30
|
|
|
import Siesta
|
2021-06-17 15:32:39 +05:30
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct TrendingView: View {
|
2021-09-25 13:48:22 +05:30
|
|
|
@StateObject private var store = Store<[Video]>()
|
|
|
|
|
2021-06-28 16:13:07 +05:30
|
|
|
@State private var category: TrendingCategory = .default
|
2021-08-01 03:40:56 +05:30
|
|
|
@State private var country: Country! = .pl
|
2021-09-25 13:48:22 +05:30
|
|
|
@State private var presentingCountrySelection = false
|
2021-06-17 15:32:39 +05:30
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
@EnvironmentObject<InvidiousAPI> private var api
|
2021-06-26 15:09:35 +05:30
|
|
|
|
2021-06-28 16:13:07 +05:30
|
|
|
var resource: Resource {
|
2021-09-25 13:48:22 +05:30
|
|
|
let resource = api.trending(category: category, country: country)
|
2021-06-28 16:13:07 +05:30
|
|
|
resource.addObserver(store)
|
2021-08-01 03:40:56 +05:30
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
return resource
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
|
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)
|
2021-08-01 03:40:56 +05:30
|
|
|
toolbar
|
|
|
|
.scaleEffect(0.85)
|
2021-07-12 02:22:49 +05:30
|
|
|
#endif
|
2021-06-17 15:32:39 +05:30
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
if store.collection.isEmpty {
|
|
|
|
Text("Loading")
|
|
|
|
}
|
2021-08-01 03:40:56 +05:30
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
VideosView(videos: store.collection)
|
2021-06-17 15:32:39 +05:30
|
|
|
}
|
2021-07-12 02:22:49 +05:30
|
|
|
}
|
2021-08-01 03:40:56 +05:30
|
|
|
#if os(tvOS)
|
2021-09-25 13:48:22 +05:30
|
|
|
.fullScreenCover(isPresented: $presentingCountrySelection) {
|
2021-08-20 04:08:31 +05:30
|
|
|
TrendingCountry(selectedCountry: $country)
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
#else
|
2021-09-25 13:48:22 +05:30
|
|
|
.sheet(isPresented: $presentingCountrySelection) {
|
2021-08-20 04:08:31 +05:30
|
|
|
TrendingCountry(selectedCountry: $country)
|
2021-08-01 03:40:56 +05:30
|
|
|
#if os(macOS)
|
|
|
|
.frame(minWidth: 400, minHeight: 400)
|
|
|
|
#endif
|
|
|
|
}
|
2021-07-12 02:22:49 +05:30
|
|
|
.navigationTitle("Trending")
|
|
|
|
#endif
|
2021-08-01 03:40:56 +05:30
|
|
|
#if os(macOS)
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItemGroup {
|
|
|
|
categoryButton
|
|
|
|
countryButton
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
#elseif os(iOS)
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItemGroup(placement: .bottomBar) {
|
|
|
|
Group {
|
|
|
|
HStack {
|
|
|
|
Text("Category")
|
|
|
|
.foregroundColor(.secondary)
|
2021-08-01 03:40:56 +05:30
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
categoryButton
|
2021-09-26 23:10:25 +05:30
|
|
|
// only way to disable Menu animation is to
|
|
|
|
// force redraw of the view when it changes
|
|
|
|
.id(UUID())
|
2021-09-25 13:48:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
Text("Country")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
countryButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
.onChange(of: resource) { resource in
|
|
|
|
resource.load()
|
|
|
|
}
|
2021-07-12 02:22:49 +05:30
|
|
|
.onAppear {
|
2021-09-25 13:48:22 +05:30
|
|
|
resource.addObserver(store)
|
2021-06-28 16:13:07 +05:30
|
|
|
resource.loadIfNeeded()
|
2021-06-17 15:32:39 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 13:48:22 +05:30
|
|
|
var toolbar: some View {
|
|
|
|
HStack {
|
|
|
|
HStack {
|
|
|
|
Text("Category")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
categoryButton
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
Spacer()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
Text("Country")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
countryButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-25 04:18:28 +05:30
|
|
|
var categoryButton: some View {
|
2021-08-01 03:40:56 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
Button(category.name) {
|
2021-09-25 13:48:22 +05:30
|
|
|
self.category = category.next()
|
2021-06-25 04:18:28 +05:30
|
|
|
}
|
2021-08-01 03:40:56 +05:30
|
|
|
.contextMenu {
|
|
|
|
ForEach(TrendingCategory.allCases) { category in
|
2021-09-25 13:48:22 +05:30
|
|
|
Button(category.name) { self.category = category }
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-08-01 03:40:56 +05:30
|
|
|
#else
|
2021-09-26 23:10:25 +05:30
|
|
|
Picker("Category", selection: $category) {
|
2021-08-01 03:40:56 +05:30
|
|
|
ForEach(TrendingCategory.allCases) { category in
|
2021-09-26 23:10:25 +05:30
|
|
|
Text(category.name).tag(category)
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-06-25 04:18:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var countryButton: some View {
|
2021-08-01 03:40:56 +05:30
|
|
|
Button(action: {
|
2021-09-25 13:48:22 +05:30
|
|
|
presentingCountrySelection.toggle()
|
2021-06-28 16:13:07 +05:30
|
|
|
resource.removeObservers(ownedBy: store)
|
2021-08-01 03:40:56 +05:30
|
|
|
}) {
|
|
|
|
Text("\(country.flag) \(country.id)")
|
2021-06-25 04:18:28 +05:30
|
|
|
}
|
|
|
|
}
|
2021-06-17 15:32:39 +05:30
|
|
|
}
|
2021-08-01 03:40:56 +05:30
|
|
|
|
|
|
|
struct TrendingView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
TrendingView()
|
2021-09-25 13:48:22 +05:30
|
|
|
.environmentObject(NavigationModel())
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
}
|