Switch to horizontal cells for trending on tvOS

This commit is contained in:
Arkadiusz Fal 2021-09-29 13:08:51 +02:00
parent d22868ed2d
commit 29e042a8bf

View File

@ -4,6 +4,7 @@ import SwiftUI
struct TrendingView: View { struct TrendingView: View {
@StateObject private var store = Store<[Video]>() @StateObject private var store = Store<[Video]>()
private var videos: [Video]
@Default(.trendingCategory) private var category @Default(.trendingCategory) private var category
@Default(.trendingCountry) private var country @Default(.trendingCountry) private var country
@ -12,6 +13,10 @@ struct TrendingView: View {
@EnvironmentObject<InvidiousAPI> private var api @EnvironmentObject<InvidiousAPI> private var api
init(_ videos: [Video] = [Video]()) {
self.videos = videos
}
var resource: Resource { var resource: Resource {
let resource = api.trending(category: category, country: country) let resource = api.trending(category: category, country: country)
resource.addObserver(store) resource.addObserver(store)
@ -21,17 +26,18 @@ struct TrendingView: View {
var body: some View { var body: some View {
Section { Section {
VStack(alignment: .center, spacing: 2) { VStack(alignment: .center, spacing: 0) {
#if os(tvOS) #if os(tvOS)
toolbar toolbar
.scaleEffect(0.85) VideosCellsHorizontal(videos: store.collection)
.padding(.top, 40)
#else
VideosCellsVertical(videos: store.collection)
#endif #endif
if store.collection.isEmpty { #if os(tvOS)
Text("Loading") Spacer()
} #endif
VideosCellsVertical(videos: store.collection)
} }
} }
#if os(tvOS) #if os(tvOS)
@ -47,15 +53,13 @@ struct TrendingView: View {
} }
.navigationTitle("Trending") .navigationTitle("Trending")
#endif #endif
#if os(macOS) .toolbar {
.toolbar { #if os(macOS)
ToolbarItemGroup { ToolbarItemGroup {
categoryButton categoryButton
countryButton countryButton
} }
} #elseif os(iOS)
#elseif os(iOS)
.toolbar {
ToolbarItemGroup(placement: .bottomBar) { ToolbarItemGroup(placement: .bottomBar) {
Group { Group {
HStack { HStack {
@ -76,14 +80,18 @@ struct TrendingView: View {
} }
} }
} }
} #endif
#endif }
.onChange(of: resource) { resource in .onChange(of: resource) { _ in
resource.load() resource.load()
} }
.onAppear { .onAppear {
resource.addObserver(store) if videos.isEmpty {
resource.loadIfNeeded() resource.addObserver(store)
resource.loadIfNeeded()
} else {
store.replace(videos)
}
} }
} }
@ -141,7 +149,9 @@ struct TrendingView: View {
struct TrendingView_Previews: PreviewProvider { struct TrendingView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
TrendingView() TrendingView(Video.allFixtures + Video.allFixtures + Video.allFixtures)
.environmentObject(InvidiousAPI())
.environmentObject(NavigationModel()) .environmentObject(NavigationModel())
.environmentObject(SubscriptionsModel())
} }
} }