Merge pull request #672 from stonerl/favorites-view-tweaks

HomeView: Changes to Favourites and History Widget
This commit is contained in:
Arkadiusz Fal 2024-05-18 11:41:57 +02:00 committed by GitHub
commit f28cec79ba
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 30 additions and 24 deletions

View File

@ -5,6 +5,7 @@ import UniformTypeIdentifiers
struct FavoriteItemView: View { struct FavoriteItemView: View {
var item: FavoriteItem var item: FavoriteItem
@Binding var favoritesChanged: Bool
@Environment(\.navigationStyle) private var navigationStyle @Environment(\.navigationStyle) private var navigationStyle
@StateObject private var store = FavoriteResourceObserver() @StateObject private var store = FavoriteResourceObserver()
@ -25,8 +26,9 @@ struct FavoriteItemView: View {
@Default(.widgetsSettings) private var widgetsSettings @Default(.widgetsSettings) private var widgetsSettings
@Default(.visibleSections) private var visibleSections @Default(.visibleSections) private var visibleSections
init(item: FavoriteItem) { init(item: FavoriteItem, favoritesChanged: Binding<Bool>) {
self.item = item self.item = item
_favoritesChanged = favoritesChanged
} }
var body: some View { var body: some View {
@ -95,6 +97,7 @@ struct FavoriteItemView: View {
.onChange(of: player.currentVideo) { _ in reloadVisibleWatches() } .onChange(of: player.currentVideo) { _ in reloadVisibleWatches() }
.onChange(of: hideShorts) { _ in reloadVisibleWatches() } .onChange(of: hideShorts) { _ in reloadVisibleWatches() }
.onChange(of: hideWatched) { _ in reloadVisibleWatches() } .onChange(of: hideWatched) { _ in reloadVisibleWatches() }
.onChange(of: favoritesChanged) { _ in reloadVisibleWatches() }
} }
} }
.id(watchModel.historyToken) .id(watchModel.historyToken)
@ -491,14 +494,22 @@ struct FavoriteItemView: View {
} }
struct FavoriteItemView_Previews: PreviewProvider { struct FavoriteItemView_Previews: PreviewProvider {
static var previews: some View { struct PreviewWrapper: View {
@State private var favoritesChanged = false
var body: some View {
NavigationView { NavigationView {
VStack { VStack {
FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Search: resistance body upper band workout"))) FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Search: resistance body upper band workout")), favoritesChanged: $favoritesChanged)
.environment(\.navigationStyle, .tab) .environment(\.navigationStyle, .tab)
FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Marques"))) FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Marques")), favoritesChanged: $favoritesChanged)
.environment(\.navigationStyle, .sidebar) .environment(\.navigationStyle, .sidebar)
} }
} }
} }
}
static var previews: some View {
PreviewWrapper()
}
} }

View File

@ -1,19 +1,14 @@
import SwiftUI import SwiftUI
struct HistoryView: View { struct HistoryView: View {
var limit = 10 var limit: Int
@FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)]) @FetchRequest(sortDescriptors: [.init(key: "watchedAt", ascending: false)])
var watches: FetchedResults<Watch> var watches: FetchedResults<Watch>
@ObservedObject private var player = PlayerModel.shared @ObservedObject private var player = PlayerModel.shared
@State private var visibleWatches = [Watch]() @State private var visibleWatches = [Watch]()
init(limit: Int = 10) {
self.limit = limit
}
var body: some View { var body: some View {
LazyVStack { LazyVStack {
if visibleWatches.isEmpty { if visibleWatches.isEmpty {
@ -38,10 +33,14 @@ struct HistoryView: View {
func reloadVisibleWatches() { func reloadVisibleWatches() {
visibleWatches = Array(watches.filter { $0.videoID != player.currentVideo?.videoID }.prefix(limit)) visibleWatches = Array(watches.filter { $0.videoID != player.currentVideo?.videoID }.prefix(limit))
} }
init(limit: Int = 10) {
self.limit = limit
}
} }
struct HistoryView_Previews: PreviewProvider { struct HistoryView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
HistoryView() HistoryView(limit: 10)
} }
} }

View File

@ -97,11 +97,11 @@ struct HomeView: View {
VStack(alignment: .leading) { VStack(alignment: .leading) {
#if os(tvOS) #if os(tvOS)
ForEach(Defaults[.favorites]) { item in ForEach(Defaults[.favorites]) { item in
FavoriteItemView(item: item) FavoriteItemView(item: item, favoritesChanged: $favoritesChanged)
} }
#else #else
ForEach(favorites) { item in ForEach(favorites) { item in
FavoriteItemView(item: item) FavoriteItemView(item: item, favoritesChanged: $favoritesChanged)
#if os(macOS) #if os(macOS)
.workaroundForVerticalScrollingBug() .workaroundForVerticalScrollingBug()
#endif #endif

View File

@ -2,7 +2,7 @@ import SwiftUI
struct ListView: View { struct ListView: View {
var items: [ContentItem] var items: [ContentItem]
var limit: Int? = 10 var limit: Int?
var body: some View { var body: some View {
LazyVStack(alignment: .leading) { LazyVStack(alignment: .leading) {
@ -16,16 +16,12 @@ struct ListView: View {
} }
var limitedItems: [ContentItem] { var limitedItems: [ContentItem] {
if let limit, limit >= 0 { Array(items.prefix(limit ?? items.count))
return Array(items.prefix(limit))
}
return items
} }
} }
struct ListView_Previews: PreviewProvider { struct ListView_Previews: PreviewProvider {
static var previews: some View { static var previews: some View {
ListView(items: [.init(video: .fixture)]) ListView(items: [.init(video: .fixture)], limit: 10)
} }
} }