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

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