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

283 lines
7.9 KiB
Swift
Raw Normal View History

import Defaults
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-09-29 17:15:00 +05:30
private var videos = [Video]()
2021-09-25 13:48:22 +05:30
@Default(.trendingCategory) private var category
@Default(.trendingCountry) private var country
2022-12-12 05:48:29 +05:30
@Default(.trendingListingStyle) private var trendingListingStyle
2021-09-25 13:48:22 +05:30
@State private var presentingCountrySelection = false
2021-06-17 15:32:39 +05:30
2021-11-02 03:26:18 +05:30
@State private var favoriteItem: FavoriteItem?
@ObservedObject private var accounts = AccountsModel.shared
2021-06-26 15:09:35 +05:30
2021-11-02 03:26:18 +05:30
var trending: [ContentItem] {
ContentItem.array(of: store.collection)
}
2022-12-16 14:05:10 +05:30
@State private var error: RequestError?
init(_ videos: [Video] = [Video]()) {
self.videos = videos
}
var resource: Resource {
let newResource: Resource
newResource = accounts.api.trending(country: country, category: category)
newResource.addObserver(store)
return newResource
}
2021-06-17 15:32:39 +05:30
var body: some View {
Section {
VerticalCells(items: trending) { if shouldDisplayHeader { header } }
.environment(\.listingStyle, trendingListingStyle)
}
.toolbar {
ToolbarItem {
RequestErrorButton(error: error)
2023-05-27 04:44:48 +05:30
}
#if os(macOS)
ToolbarItemGroup {
if let favoriteItem {
FavoriteButton(item: favoriteItem)
.id(favoriteItem.id)
}
categoryButton
countryButton
}
#endif
}
.onChange(of: resource) { _ in
resource.load()
.onFailure { self.error = $0 }
.onSuccess { _ in self.error = nil }
updateFavoriteItem()
}
.onAppear {
resource.loadIfNeeded()?
.onFailure { self.error = $0 }
.onSuccess { _ in self.error = nil }
updateFavoriteItem()
}
#if os(tvOS)
.fullScreenCover(isPresented: $presentingCountrySelection) {
TrendingCountry(selectedCountry: $country)
}
#else
.sheet(isPresented: $presentingCountrySelection) {
2021-11-08 21:59:35 +05:30
TrendingCountry(selectedCountry: $country)
#if os(macOS)
.frame(minWidth: 400, minHeight: 400)
#endif
}
2022-01-07 04:30:40 +05:30
.background(
Button("Refresh") {
resource.load()
2022-12-16 14:05:10 +05:30
.onFailure { self.error = $0 }
.onSuccess { _ in self.error = nil }
2022-01-07 04:30:40 +05:30
}
.keyboardShortcut("r")
2022-01-07 16:42:56 +05:30
.opacity(0)
2022-01-07 04:30:40 +05:30
)
2021-11-08 21:59:35 +05:30
.navigationTitle("Trending")
2021-07-12 02:22:49 +05:30
#endif
#if os(iOS)
2023-07-01 22:08:11 +05:30
2022-09-02 22:32:19 +05:30
.refreshable {
DispatchQueue.main.async {
resource.load()
2022-12-16 14:05:10 +05:30
.onFailure { self.error = $0 }
.onSuccess { _ in self.error = nil }
2022-09-02 22:32:19 +05:30
}
}
2022-12-10 06:49:36 +05:30
.navigationBarTitleDisplayMode(.inline)
.toolbar {
ToolbarItem(placement: .principal) {
trendingMenu
}
}
#endif
2022-12-12 05:48:29 +05:30
#if os(macOS)
.toolbar {
ToolbarItem {
ListingStyleButtons(listingStyle: $trendingListingStyle)
}
2023-02-25 21:12:18 +05:30
ToolbarItem {
HideWatchedButtons()
}
2023-02-25 21:12:18 +05:30
ToolbarItem {
2023-05-23 22:24:53 +05:30
HideShortsButtons()
2023-02-25 21:12:18 +05:30
}
2022-08-28 23:30:43 +05:30
}
2022-12-12 05:48:29 +05:30
#else
2023-05-08 01:15:18 +05:30
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
resource.loadIfNeeded()?
.onFailure { self.error = $0 }
2022-12-16 14:05:10 +05:30
.onSuccess { _ in self.error = nil }
2022-12-12 05:48:29 +05:30
}
2022-08-28 23:30:43 +05:30
#endif
2021-06-17 15:32:39 +05:30
}
2021-11-11 03:35:59 +05:30
#if os(tvOS)
private var toolbar: some View {
HStack {
if accounts.app.supportsTrendingCategories {
HStack {
Text("Category")
.foregroundColor(.secondary)
2021-09-25 13:48:22 +05:30
2021-11-11 03:35:59 +05:30
categoryButton
}
2021-10-21 03:51:50 +05:30
}
2021-09-25 13:48:22 +05:30
2021-11-11 03:35:59 +05:30
HStack {
Text("Country")
.foregroundColor(.secondary)
2021-11-02 03:26:18 +05:30
2021-11-11 03:35:59 +05:30
countryButton
2021-11-02 03:26:18 +05:30
}
2021-11-11 03:35:59 +05:30
2022-11-11 03:21:30 +05:30
if let favoriteItem {
FavoriteButton(item: favoriteItem)
.id(favoriteItem.id)
.labelStyle(.iconOnly)
}
2021-11-11 03:35:59 +05:30
}
2021-09-25 13:48:22 +05:30
}
2021-11-11 03:35:59 +05:30
#endif
2021-09-25 13:48:22 +05:30
2022-12-10 06:49:36 +05:30
#if os(iOS)
var trendingMenu: some View {
Menu {
countryButton
2022-12-12 03:45:56 +05:30
categoryButton
2022-12-12 03:45:56 +05:30
2022-12-12 05:48:29 +05:30
ListingStyleButtons(listingStyle: $trendingListingStyle)
2023-02-25 21:12:18 +05:30
Section {
HideWatchedButtons()
2023-05-23 22:24:53 +05:30
HideShortsButtons()
2023-02-25 21:12:18 +05:30
}
2022-12-12 03:45:56 +05:30
Section {
SettingsButtons()
}
2022-12-10 06:49:36 +05:30
} label: {
HStack(spacing: 12) {
Text("\(country.flag) \(country.name)")
.font(.headline)
.foregroundColor(.primary)
Image(systemName: "chevron.down.circle.fill")
.foregroundColor(.accentColor)
.imageScale(.small)
}
.frame(maxWidth: 320)
}
}
#endif
@ViewBuilder private var categoryButton: some View {
if accounts.app.supportsTrendingCategories {
#if os(tvOS)
Button(category.name) {
self.category = category.next()
}
.contextMenu {
ForEach(TrendingCategory.allCases) { category in
Button(category.controlLabel) { self.category = category }
}
2021-09-29 18:06:52 +05:30
Button("Cancel", role: .cancel) {}
}
2021-09-25 13:48:22 +05:30
#else
Picker(category.controlLabel, selection: $category) {
ForEach(TrendingCategory.allCases) { category in
Label(category.controlLabel, systemImage: category.systemImage).tag(category)
}
}
#endif
}
}
2021-11-02 03:26:18 +05:30
private var countryButton: some View {
Button(action: {
2021-09-25 13:48:22 +05:30
presentingCountrySelection.toggle()
resource.removeObservers(ownedBy: store)
}) {
2022-12-10 06:49:36 +05:30
#if os(iOS)
2022-12-16 04:22:00 +05:30
Label("Country", systemImage: "flag")
2022-12-10 06:49:36 +05:30
#else
Text("\(country.flag) \(country.id)")
#endif
}
}
2021-11-02 03:26:18 +05:30
private func updateFavoriteItem() {
favoriteItem = FavoriteItem(section: .trending(country.rawValue, category.rawValue))
}
var header: some View {
HStack {
Group {
categoryButton
countryButton
}
.font(.caption)
Spacer()
ListingStyleButtons(listingStyle: $trendingListingStyle)
HideWatchedButtons()
2023-05-23 22:24:53 +05:30
HideShortsButtons()
Button {
resource.load()
.onFailure { self.error = $0 }
.onSuccess { _ in self.error = nil }
} label: {
Label("Refresh", systemImage: "arrow.clockwise")
.labelStyle(.iconOnly)
.imageScale(.small)
.font(.caption)
}
}
.padding(.leading, 30)
.padding(.bottom, 15)
.padding(.trailing, 30)
}
var shouldDisplayHeader: Bool {
#if os(tvOS)
true
#else
false
#endif
}
2021-06-17 15:32:39 +05:30
}
struct TrendingView_Previews: PreviewProvider {
static var previews: some View {
2022-12-10 06:49:36 +05:30
NavigationView {
TrendingView(Video.allFixtures)
}
}
}