2021-09-27 03:49:50 +05:30
|
|
|
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
|
|
|
|
2021-09-27 03:49:50 +05:30
|
|
|
@Default(.trendingCategory) private var category
|
|
|
|
@Default(.trendingCountry) private var country
|
|
|
|
|
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?
|
|
|
|
|
2021-10-17 04:18:58 +05:30
|
|
|
@EnvironmentObject<AccountsModel> private var accounts
|
2021-06-26 15:09:35 +05:30
|
|
|
|
2021-11-02 03:26:18 +05:30
|
|
|
var trending: [ContentItem] {
|
2021-10-22 04:59:10 +05:30
|
|
|
ContentItem.array(of: store.collection)
|
|
|
|
}
|
|
|
|
|
2021-09-29 16:38:51 +05:30
|
|
|
init(_ videos: [Video] = [Video]()) {
|
|
|
|
self.videos = videos
|
|
|
|
}
|
|
|
|
|
2021-06-28 16:13:07 +05:30
|
|
|
var resource: Resource {
|
2021-10-21 03:51:50 +05:30
|
|
|
let newResource: Resource
|
2021-08-01 03:40:56 +05:30
|
|
|
|
2021-10-21 03:51:50 +05:30
|
|
|
newResource = accounts.api.trending(country: country, category: category)
|
|
|
|
newResource.addObserver(store)
|
|
|
|
|
|
|
|
return newResource
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
|
2021-06-17 15:32:39 +05:30
|
|
|
var body: some View {
|
2021-10-06 01:50:09 +05:30
|
|
|
PlayerControlsView {
|
|
|
|
Section {
|
|
|
|
VStack(alignment: .center, spacing: 0) {
|
|
|
|
#if os(tvOS)
|
|
|
|
toolbar
|
2021-11-02 03:26:18 +05:30
|
|
|
HorizontalCells(items: trending)
|
2021-10-06 01:50:09 +05:30
|
|
|
.padding(.top, 40)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
#else
|
2021-11-02 03:26:18 +05:30
|
|
|
VerticalCells(items: trending)
|
2021-10-06 01:50:09 +05:30
|
|
|
#endif
|
|
|
|
}
|
2021-06-17 15:32:39 +05:30
|
|
|
}
|
2021-07-12 02:22:49 +05:30
|
|
|
}
|
2022-01-05 21:55:57 +05:30
|
|
|
|
|
|
|
.toolbar {
|
|
|
|
#if os(macOS)
|
|
|
|
ToolbarItemGroup {
|
|
|
|
if let favoriteItem = favoriteItem {
|
|
|
|
FavoriteButton(item: favoriteItem)
|
|
|
|
.id(favoriteItem.id)
|
|
|
|
}
|
|
|
|
|
|
|
|
if accounts.app.supportsTrendingCategories {
|
|
|
|
categoryButton
|
|
|
|
}
|
|
|
|
countryButton
|
|
|
|
}
|
|
|
|
#elseif os(iOS)
|
|
|
|
ToolbarItemGroup(placement: .bottomBar) {
|
|
|
|
Group {
|
|
|
|
if accounts.app.supportsTrendingCategories {
|
|
|
|
HStack {
|
|
|
|
Text("Category")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
categoryButton
|
|
|
|
// only way to disable Menu animation is to
|
|
|
|
// force redraw of the view when it changes
|
|
|
|
.id(UUID())
|
|
|
|
}
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
|
|
|
|
if let favoriteItem = favoriteItem {
|
|
|
|
FavoriteButton(item: favoriteItem)
|
|
|
|
.id(favoriteItem.id)
|
|
|
|
|
|
|
|
Spacer()
|
|
|
|
}
|
|
|
|
|
|
|
|
HStack {
|
|
|
|
Text("Country")
|
|
|
|
.foregroundColor(.secondary)
|
|
|
|
|
|
|
|
countryButton
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
.onChange(of: resource) { _ in
|
|
|
|
resource.load()
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
.onAppear {
|
|
|
|
if videos.isEmpty {
|
|
|
|
resource.addObserver(store)
|
|
|
|
resource.loadIfNeeded()
|
|
|
|
} else {
|
|
|
|
store.replace(videos)
|
|
|
|
}
|
|
|
|
|
|
|
|
updateFavoriteItem()
|
|
|
|
}
|
|
|
|
|
2021-08-01 03:40:56 +05:30
|
|
|
#if os(tvOS)
|
2021-11-08 21:59:35 +05:30
|
|
|
.fullScreenCover(isPresented: $presentingCountrySelection) {
|
|
|
|
TrendingCountry(selectedCountry: $country)
|
|
|
|
}
|
2021-08-01 03:40:56 +05:30
|
|
|
#else
|
2021-11-08 21:59:35 +05:30
|
|
|
.sheet(isPresented: $presentingCountrySelection) {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
.keyboardShortcut("r")
|
|
|
|
)
|
2021-11-08 21:59:35 +05:30
|
|
|
.navigationTitle("Trending")
|
2021-07-12 02:22:49 +05:30
|
|
|
#endif
|
2022-01-05 21:55:57 +05:30
|
|
|
#if os(iOS)
|
|
|
|
.refreshControl { refreshControl in
|
|
|
|
resource.load().onCompletion { _ in
|
|
|
|
refreshControl.endRefreshing()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.navigationBarTitleDisplayMode(RefreshControl.navigationBarTitleDisplayMode)
|
|
|
|
#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
|
|
|
#if os(iOS)
|
|
|
|
Spacer()
|
|
|
|
#endif
|
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
|
|
|
|
|
|
|
#if os(tvOS)
|
|
|
|
if let favoriteItem = favoriteItem {
|
|
|
|
FavoriteButton(item: favoriteItem)
|
|
|
|
.id(favoriteItem.id)
|
|
|
|
.labelStyle(.iconOnly)
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
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
|
|
|
|
2021-11-02 03:26:18 +05:30
|
|
|
private var categoryButton: some View {
|
2021-08-01 03:40:56 +05:30
|
|
|
#if os(tvOS)
|
|
|
|
Button(category.name) {
|
2021-09-25 13:48:22 +05:30
|
|
|
self.category = category.next()
|
2021-06-25 04:18:28 +05:30
|
|
|
}
|
2021-08-01 03:40:56 +05:30
|
|
|
.contextMenu {
|
|
|
|
ForEach(TrendingCategory.allCases) { category in
|
2021-11-02 03:26:18 +05:30
|
|
|
Button(category.controlLabel) { self.category = category }
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
2021-09-29 18:06:52 +05:30
|
|
|
|
|
|
|
Button("Cancel", role: .cancel) {}
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
2021-09-25 13:48:22 +05:30
|
|
|
|
2021-08-01 03:40:56 +05:30
|
|
|
#else
|
2021-11-28 20:07:55 +05:30
|
|
|
Picker(category.controlLabel, selection: $category) {
|
2021-08-01 03:40:56 +05:30
|
|
|
ForEach(TrendingCategory.allCases) { category in
|
2021-11-02 03:26:18 +05:30
|
|
|
Text(category.controlLabel).tag(category)
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
}
|
2021-11-28 20:07:55 +05:30
|
|
|
.pickerStyle(.menu)
|
2021-08-01 03:40:56 +05:30
|
|
|
#endif
|
2021-06-25 04:18:28 +05:30
|
|
|
}
|
|
|
|
|
2021-11-02 03:26:18 +05:30
|
|
|
private var countryButton: some View {
|
2021-08-01 03:40:56 +05:30
|
|
|
Button(action: {
|
2021-09-25 13:48:22 +05:30
|
|
|
presentingCountrySelection.toggle()
|
2021-06-28 16:13:07 +05:30
|
|
|
resource.removeObservers(ownedBy: store)
|
2021-08-01 03:40:56 +05:30
|
|
|
}) {
|
|
|
|
Text("\(country.flag) \(country.id)")
|
2021-06-25 04:18:28 +05:30
|
|
|
}
|
|
|
|
}
|
2021-11-02 03:26:18 +05:30
|
|
|
|
|
|
|
private func updateFavoriteItem() {
|
|
|
|
favoriteItem = FavoriteItem(section: .trending(country.rawValue, category.rawValue))
|
|
|
|
}
|
2021-06-17 15:32:39 +05:30
|
|
|
}
|
2021-08-01 03:40:56 +05:30
|
|
|
|
|
|
|
struct TrendingView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2021-09-29 17:15:00 +05:30
|
|
|
TrendingView(Video.allFixtures)
|
|
|
|
.injectFixtureEnvironmentObjects()
|
2021-08-01 03:40:56 +05:30
|
|
|
}
|
|
|
|
}
|