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

142 lines
3.9 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
2021-06-28 16:13:07 +05:30
@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 toolbar: some View {
HStack {
HStack {
Text("Category")
.foregroundColor(.secondary)
categoryButton
}
#if os(iOS)
Spacer()
#endif
HStack {
Text("Country")
.foregroundColor(.secondary)
countryButton
}
}
}
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)
toolbar
.scaleEffect(0.85)
2021-07-12 02:22:49 +05:30
#endif
2021-06-17 15:32:39 +05:30
2021-06-28 16:13:07 +05:30
VideosView(videos: store.collection)
#if os(iOS)
toolbar
.font(.system(size: 14))
.padding(.horizontal)
.padding(.vertical, 10)
.overlay(Divider().offset(x: 0, y: -2), alignment: .topTrailing)
2021-08-20 04:08:31 +05:30
.transaction { t in t.animation = .none }
#endif
2021-06-17 15:32:39 +05:30
}
2021-07-12 02:22:49 +05:30
}
#if os(tvOS)
.fullScreenCover(isPresented: $selectingCountry, onDismiss: { setCountry(country) }) {
2021-08-20 04:08:31 +05:30
TrendingCountry(selectedCountry: $country)
}
#else
.sheet(isPresented: $selectingCountry, onDismiss: { setCountry(country) }) {
2021-08-20 04:08:31 +05:30
TrendingCountry(selectedCountry: $country)
#if os(macOS)
.frame(minWidth: 400, minHeight: 400)
#endif
}
2021-07-12 02:22:49 +05:30
.navigationTitle("Trending")
#endif
#if os(macOS)
.toolbar {
ToolbarItemGroup {
categoryButton
countryButton
}
}
#endif
2021-07-12 02:22:49 +05:30
.onAppear {
2021-06-28 16:13:07 +05:30
resource.loadIfNeeded()
2021-06-17 15:32:39 +05:30
}
}
var categoryButton: some View {
#if os(tvOS)
Button(category.name) {
setCategory(category.next())
}
.contextMenu {
ForEach(TrendingCategory.allCases) { category in
Button(category.name) { setCategory(category) }
}
}
#else
Menu(category.name) {
ForEach(TrendingCategory.allCases) { category in
Button(action: { setCategory(category) }) {
if category == self.category {
Label(category.name, systemImage: "checkmark")
} else {
Text(category.name)
}
}
}
}
#endif
}
var countryButton: some View {
Button(action: {
selectingCountry.toggle()
2021-06-28 16:13:07 +05:30
resource.removeObservers(ownedBy: store)
}) {
Text("\(country.flag) \(country.id)")
}
}
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
}
struct TrendingView_Previews: PreviewProvider {
static var previews: some View {
TrendingView()
.environmentObject(NavigationState())
}
}