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