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
|
|
|
|
|
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?
|
|
|
|
|
2021-10-16 22:48:58 +00:00
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
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)
|
|
|
|
}
|
|
|
|
|
2021-09-29 11:08:51 +00:00
|
|
|
init(_ videos: [Video] = [Video]()) {
|
|
|
|
self.videos = videos
|
|
|
|
}
|
|
|
|
|
2021-06-28 10:43:07 +00:00
|
|
|
var resource: Resource {
|
2021-10-20 22:21:50 +00:00
|
|
|
let newResource: Resource
|
2021-07-31 22:10:56 +00:00
|
|
|
|
2021-10-20 22:21:50 +00:00
|
|
|
newResource = accounts.api.trending(country: country, category: category)
|
|
|
|
newResource.addObserver(store)
|
|
|
|
|
|
|
|
return newResource
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
|
|
|
|
2021-06-17 10:02:39 +00:00
|
|
|
var body: some View {
|
2022-02-16 20:23:11 +00:00
|
|
|
BrowserPlayerControls(toolbar: {
|
2022-02-04 17:38:29 +00:00
|
|
|
HStack {
|
|
|
|
if accounts.app.supportsTrendingCategories {
|
2022-11-11 20:28:48 +00:00
|
|
|
categoryButton
|
|
|
|
.layoutPriority(1)
|
|
|
|
// only way to disable Menu animation is to
|
|
|
|
// force redraw of the view when it changes
|
|
|
|
.id(UUID())
|
2022-02-04 17:38:29 +00:00
|
|
|
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
|
2022-09-28 14:27:01 +00:00
|
|
|
if let favoriteItem {
|
2022-02-04 17:38:29 +00:00
|
|
|
FavoriteButton(item: favoriteItem, labelPadding: true)
|
|
|
|
.id(favoriteItem.id)
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
|
2022-11-11 20:28:48 +00:00
|
|
|
countryButton
|
2022-02-04 17:38:29 +00:00
|
|
|
}
|
|
|
|
.padding(.horizontal)
|
|
|
|
}) {
|
2021-10-05 20:20:09 +00:00
|
|
|
Section {
|
|
|
|
VStack(alignment: .center, spacing: 0) {
|
|
|
|
#if os(tvOS)
|
|
|
|
toolbar
|
2021-11-01 21:56:18 +00:00
|
|
|
HorizontalCells(items: trending)
|
2021-10-05 20:20:09 +00:00
|
|
|
.padding(.top, 40)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
#else
|
2021-11-01 21:56:18 +00:00
|
|
|
VerticalCells(items: trending)
|
2022-02-04 17:38:29 +00:00
|
|
|
.environment(\.scrollViewBottomPadding, 70)
|
2021-10-05 20:20:09 +00:00
|
|
|
#endif
|
|
|
|
}
|
2021-06-17 10:02:39 +00:00
|
|
|
}
|
2021-07-11 20:52:49 +00:00
|
|
|
}
|
2022-01-05 16:25:57 +00:00
|
|
|
|
|
|
|
.toolbar {
|
|
|
|
#if os(macOS)
|
|
|
|
ToolbarItemGroup {
|
2022-09-28 14:27:01 +00:00
|
|
|
if let favoriteItem {
|
2022-01-05 16:25:57 +00:00
|
|
|
FavoriteButton(item: favoriteItem)
|
|
|
|
.id(favoriteItem.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if accounts.app.supportsTrendingCategories {
|
|
|
|
categoryButton
|
|
|
|
}
|
|
|
|
countryButton
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.onChange(of: resource) { _ in
|
|
|
|
resource.load()
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
.onAppear {
|
|
|
|
if videos.isEmpty {
|
|
|
|
resource.addObserver(store)
|
|
|
|
resource.loadIfNeeded()
|
|
|
|
} else {
|
|
|
|
store.replace(videos)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
|
2021-07-31 22:10:56 +00:00
|
|
|
#if os(tvOS)
|
2021-11-08 16:29:35 +00:00
|
|
|
.fullScreenCover(isPresented: $presentingCountrySelection) {
|
|
|
|
TrendingCountry(selectedCountry: $country)
|
|
|
|
}
|
2021-07-31 22:10:56 +00:00
|
|
|
#else
|
2021-11-08 16:29:35 +00:00
|
|
|
.sheet(isPresented: $presentingCountrySelection) {
|
|
|
|
TrendingCountry(selectedCountry: $country)
|
|
|
|
#if os(macOS)
|
|
|
|
.frame(minWidth: 400, minHeight: 400)
|
|
|
|
#endif
|
|
|
|
}
|
2022-01-06 23:00:40 +00:00
|
|
|
.background(
|
|
|
|
Button("Refresh") {
|
|
|
|
resource.load()
|
|
|
|
}
|
|
|
|
.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)
|
|
|
|
.refreshControl { refreshControl in
|
|
|
|
resource.load().onCompletion { _ in
|
|
|
|
refreshControl.endRefreshing()
|
|
|
|
}
|
|
|
|
}
|
2022-09-02 17:02:19 +00:00
|
|
|
.backport
|
|
|
|
.refreshable {
|
2022-09-04 15:24:07 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
resource.load().onFailure { error in
|
|
|
|
NavigationModel.shared.presentAlert(title: "Could not refresh Trending", message: error.userMessage)
|
|
|
|
}
|
2022-09-02 17:02:19 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-05 16:25:57 +00:00
|
|
|
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode)
|
|
|
|
#endif
|
2022-08-28 18:00:43 +00:00
|
|
|
#if !os(macOS)
|
|
|
|
.onReceive(NotificationCenter.default.publisher(for: UIApplication.willEnterForegroundNotification)) { _ in
|
|
|
|
resource.loadIfNeeded()
|
|
|
|
}
|
|
|
|
#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
|
|
|
|
2021-11-01 21:56:18 +00:00
|
|
|
private 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-11-01 21:56:18 +00:00
|
|
|
Button(category.controlLabel) { self.category = category }
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
2021-09-29 12:36:52 +00:00
|
|
|
|
|
|
|
Button("Cancel", role: .cancel) {}
|
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
|
2021-11-28 14:37:55 +00:00
|
|
|
Picker(category.controlLabel, selection: $category) {
|
2021-07-31 22:10:56 +00:00
|
|
|
ForEach(TrendingCategory.allCases) { category in
|
2021-11-01 21:56:18 +00:00
|
|
|
Text(category.controlLabel).tag(category)
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-28 14:37:55 +00:00
|
|
|
.pickerStyle(.menu)
|
2021-07-31 22:10:56 +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()
|
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-11-01 21:56:18 +00:00
|
|
|
|
|
|
|
private func updateFavoriteItem() {
|
|
|
|
favoriteItem = FavoriteItem(section: .trending(country.rawValue, category.rawValue))
|
|
|
|
}
|
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 {
|
2021-09-29 11:45:00 +00:00
|
|
|
TrendingView(Video.allFixtures)
|
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
|
|
|
}
|