2021-06-28 10:43:07 +00:00
|
|
|
import Siesta
|
2021-06-17 10:02:39 +00:00
|
|
|
import SwiftUI
|
|
|
|
|
|
|
|
struct TrendingView: View {
|
2021-09-25 08:18:22 +00:00
|
|
|
@StateObject private var store = Store<[Video]>()
|
|
|
|
|
2021-06-28 10:43:07 +00:00
|
|
|
@State private var category: TrendingCategory = .default
|
2021-07-31 22:10:56 +00:00
|
|
|
@State private var country: Country! = .pl
|
2021-09-25 08:18:22 +00:00
|
|
|
@State private var presentingCountrySelection = false
|
2021-06-17 10:02:39 +00:00
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
@EnvironmentObject<InvidiousAPI> private var api
|
2021-06-26 09:39:35 +00:00
|
|
|
|
2021-06-28 10:43:07 +00:00
|
|
|
var resource: Resource {
|
2021-09-25 08:18:22 +00:00
|
|
|
let resource = api.trending(category: category, country: country)
|
2021-06-28 10:43:07 +00:00
|
|
|
resource.addObserver(store)
|
2021-07-31 22:10:56 +00:00
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
return resource
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2021-07-11 20:52:49 +00:00
|
|
|
#if os(tvOS)
|
2021-07-31 22:10:56 +00:00
|
|
|
toolbar
|
|
|
|
.scaleEffect(0.85)
|
2021-07-11 20:52:49 +00:00
|
|
|
#endif
|
2021-06-17 10:02:39 +00:00
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
if store.collection.isEmpty {
|
|
|
|
Text("Loading")
|
|
|
|
}
|
2021-07-31 22:10:56 +00:00
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
VideosView(videos: store.collection)
|
2021-06-17 10:02:39 +00:00
|
|
|
}
|
2021-07-11 20:52:49 +00:00
|
|
|
}
|
2021-07-31 22:10:56 +00:00
|
|
|
#if os(tvOS)
|
2021-09-25 08:18:22 +00:00
|
|
|
.fullScreenCover(isPresented: $presentingCountrySelection) {
|
2021-08-19 22:38:31 +00:00
|
|
|
TrendingCountry(selectedCountry: $country)
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
|
|
|
#else
|
2021-09-25 08:18:22 +00:00
|
|
|
.sheet(isPresented: $presentingCountrySelection) {
|
2021-08-19 22:38:31 +00:00
|
|
|
TrendingCountry(selectedCountry: $country)
|
2021-07-31 22:10:56 +00:00
|
|
|
#if os(macOS)
|
|
|
|
.frame(minWidth: 400, minHeight: 400)
|
|
|
|
#endif
|
|
|
|
}
|
2021-07-11 20:52:49 +00:00
|
|
|
.navigationTitle("Trending")
|
|
|
|
#endif
|
2021-07-31 22:10:56 +00:00
|
|
|
#if os(macOS)
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItemGroup {
|
|
|
|
categoryButton
|
|
|
|
countryButton
|
|
|
|
}
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
#elseif os(iOS)
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItemGroup(placement: .bottomBar) {
|
|
|
|
Group {
|
|
|
|
HStack {
|
|
|
|
Text("Category")
|
|
|
|
.foregroundColor(.secondary)
|
2021-07-31 22:10:56 +00:00
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
categoryButton
|
|
|
|
}
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
Text("Country")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
countryButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.transaction { t in t.animation = .none }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
.onChange(of: resource) { resource in
|
|
|
|
resource.load()
|
|
|
|
}
|
2021-07-11 20:52:49 +00:00
|
|
|
.onAppear {
|
2021-09-25 08:18:22 +00:00
|
|
|
resource.addObserver(store)
|
2021-06-28 10:43:07 +00:00
|
|
|
resource.loadIfNeeded()
|
2021-06-17 10:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-25 08:18:22 +00:00
|
|
|
var toolbar: some View {
|
|
|
|
HStack {
|
|
|
|
HStack {
|
|
|
|
Text("Category")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
categoryButton
|
|
|
|
}
|
|
|
|
|
|
|
|
#if os(iOS)
|
|
|
|
Spacer()
|
|
|
|
#endif
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
Text("Country")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
countryButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-24 22:48:28 +00:00
|
|
|
var categoryButton: some View {
|
2021-07-31 22:10:56 +00:00
|
|
|
#if os(tvOS)
|
|
|
|
Button(category.name) {
|
2021-09-25 08:18:22 +00:00
|
|
|
self.category = category.next()
|
2021-06-24 22:48:28 +00:00
|
|
|
}
|
2021-07-31 22:10:56 +00:00
|
|
|
.contextMenu {
|
|
|
|
ForEach(TrendingCategory.allCases) { category in
|
2021-09-25 08:18:22 +00:00
|
|
|
Button(category.name) { self.category = category }
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-25 08:18:22 +00:00
|
|
|
|
2021-07-31 22:10:56 +00:00
|
|
|
#else
|
|
|
|
Menu(category.name) {
|
|
|
|
ForEach(TrendingCategory.allCases) { category in
|
2021-09-25 08:18:22 +00:00
|
|
|
Button(action: { self.category = category }) {
|
2021-07-31 22:10:56 +00:00
|
|
|
if category == self.category {
|
|
|
|
Label(category.name, systemImage: "checkmark")
|
|
|
|
} else {
|
|
|
|
Text(category.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2021-06-24 22:48:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var countryButton: some View {
|
2021-07-31 22:10:56 +00:00
|
|
|
Button(action: {
|
2021-09-25 08:18:22 +00:00
|
|
|
presentingCountrySelection.toggle()
|
2021-06-28 10:43:07 +00:00
|
|
|
resource.removeObservers(ownedBy: store)
|
2021-07-31 22:10:56 +00:00
|
|
|
}) {
|
|
|
|
Text("\(country.flag) \(country.id)")
|
2021-06-24 22:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-17 10:02:39 +00:00
|
|
|
}
|
2021-07-31 22:10:56 +00:00
|
|
|
|
|
|
|
struct TrendingView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
TrendingView()
|
2021-09-25 08:18:22 +00:00
|
|
|
.environmentObject(NavigationModel())
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
|
|
|
}
|