yattee/Shared/Trending/TrendingView.swift

172 lines
4.8 KiB
Swift
Raw Normal View History

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
@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-10-16 22:48:58 +00:00
@EnvironmentObject<AccountsModel> private var accounts
2021-06-26 09:39:35 +00:00
var popular: [ContentItem] {
ContentItem.array(of: store.collection)
}
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-10-20 22:21:50 +00:00
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 {
PlayerControlsView {
Section {
VStack(alignment: .center, spacing: 0) {
#if os(tvOS)
toolbar
HorizontalCells(items: popular)
.padding(.top, 40)
Spacer()
#else
VerticalCells(items: popular)
#endif
}
2021-06-17 10:02:39 +00:00
}
2021-07-11 20:52:49 +00:00
}
#if os(tvOS)
2021-09-25 08:18:22 +00:00
.fullScreenCover(isPresented: $presentingCountrySelection) {
2021-08-19 22:38:31 +00:00
TrendingCountry(selectedCountry: $country)
}
#else
2021-09-25 08:18:22 +00:00
.sheet(isPresented: $presentingCountrySelection) {
2021-08-19 22:38:31 +00:00
TrendingCountry(selectedCountry: $country)
#if os(macOS)
.frame(minWidth: 400, minHeight: 400)
#endif
}
2021-07-11 20:52:49 +00:00
.navigationTitle("Trending")
#endif
.toolbar {
#if os(macOS)
ToolbarItemGroup {
2021-10-20 22:21:50 +00:00
if accounts.app.supportsTrendingCategories {
categoryButton
}
countryButton
}
#elseif os(iOS)
2021-09-25 08:18:22 +00:00
ToolbarItemGroup(placement: .bottomBar) {
Group {
2021-10-20 22:21:50 +00:00
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())
}
} else {
Spacer()
2021-09-25 08:18:22 +00:00
}
HStack {
Text("Country")
.foregroundColor(.secondary)
countryButton
}
}
}
#endif
}
.onChange(of: resource) { _ in
2021-09-25 08:18:22 +00:00
resource.load()
}
2021-07-11 20:52:49 +00:00
.onAppear {
if videos.isEmpty {
resource.addObserver(store)
resource.loadIfNeeded()
} else {
store.replace(videos)
}
2021-06-17 10:02:39 +00:00
}
}
2021-09-25 08:18:22 +00:00
var toolbar: some View {
HStack {
2021-10-20 22:21:50 +00:00
if accounts.app.supportsTrendingCategories {
HStack {
Text("Category")
.foregroundColor(.secondary)
2021-09-25 08:18:22 +00:00
2021-10-20 22:21:50 +00:00
categoryButton
}
2021-09-25 08:18:22 +00:00
}
#if os(iOS)
Spacer()
#endif
HStack {
Text("Country")
.foregroundColor(.secondary)
countryButton
}
}
}
var categoryButton: some View {
#if os(tvOS)
Button(category.name) {
2021-09-25 08:18:22 +00:00
self.category = category.next()
}
.contextMenu {
ForEach(TrendingCategory.allCases) { category in
2021-09-25 08:18:22 +00:00
Button(category.name) { self.category = category }
}
2021-09-29 12:36:52 +00:00
Button("Cancel", role: .cancel) {}
}
2021-09-25 08:18:22 +00:00
#else
Picker("Category", selection: $category) {
ForEach(TrendingCategory.allCases) { category in
Text(category.name).tag(category)
}
}
#endif
}
var countryButton: some View {
Button(action: {
2021-09-25 08:18:22 +00:00
presentingCountrySelection.toggle()
2021-06-28 10:43:07 +00:00
resource.removeObservers(ownedBy: store)
}) {
Text("\(country.flag) \(country.id)")
}
}
2021-06-17 10:02:39 +00:00
}
struct TrendingView_Previews: PreviewProvider {
static var previews: some View {
2021-09-29 11:45:00 +00:00
TrendingView(Video.allFixtures)
.injectFixtureEnvironmentObjects()
}
}