HomeView: Changes to Favourites and History Widget

The History Widget in the Home View was hard-coded to 10 items. Now it uses the limit set in the settings.

The items weren't immediate updated when the limit was changed.

List Views had a hard-coded limit of 10 items. Now they use the limit supplied in the parameter.
This commit is contained in:
Toni Förster 2024-05-18 00:36:40 +02:00
parent 1fe8a32fb8
commit 38c4ddbe43
No known key found for this signature in database
GPG Key ID: 292F3E5086C83FC7
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 {
@ -92,6 +94,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)
@ -486,14 +489,22 @@ struct FavoriteItemView: View {
} }
struct FavoriteItemView_Previews: PreviewProvider { struct FavoriteItemView_Previews: PreviewProvider {
static var previews: some View { struct PreviewWrapper: View {
NavigationView { @State private var favoritesChanged = false
VStack {
FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Search: resistance body upper band workout"))) var body: some View {
.environment(\.navigationStyle, .tab) NavigationView {
FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Marques"))) VStack {
.environment(\.navigationStyle, .sidebar) FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Search: resistance body upper band workout")), favoritesChanged: $favoritesChanged)
.environment(\.navigationStyle, .tab)
FavoriteItemView(item: .init(section: .channel("peerTube", "a", "Marques")), favoritesChanged: $favoritesChanged)
.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)
} }
} }