Add buttons for hiding watched videos

Fix #448
This commit is contained in:
Arkadiusz Fal 2023-05-23 18:48:39 +02:00
parent 1e2d6cf72f
commit eed9330c0c
14 changed files with 100 additions and 22 deletions

View File

@ -102,6 +102,7 @@ struct ChannelPlaylistView: View {
ToolbarItem(placement: playlistButtonsPlacement) { ToolbarItem(placement: playlistButtonsPlacement) {
HStack { HStack {
ListingStyleButtons(listingStyle: $channelPlaylistListingStyle) ListingStyleButtons(listingStyle: $channelPlaylistListingStyle)
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
ShareButton(contentItem: contentItem) ShareButton(contentItem: contentItem)
@ -131,6 +132,7 @@ struct ChannelPlaylistView: View {
ListingStyleButtons(listingStyle: $channelPlaylistListingStyle) ListingStyleButtons(listingStyle: $channelPlaylistListingStyle)
Section { Section {
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
} }

View File

@ -137,6 +137,7 @@ struct ChannelVideosView: View {
} }
ListingStyleButtons(listingStyle: $channelPlaylistListingStyle) ListingStyleButtons(listingStyle: $channelPlaylistListingStyle)
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
contentTypePicker contentTypePicker
} }
@ -281,6 +282,7 @@ struct ChannelVideosView: View {
ListingStyleButtons(listingStyle: $channelPlaylistListingStyle) ListingStyleButtons(listingStyle: $channelPlaylistListingStyle)
Section { Section {
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
} }
} }

View File

@ -256,6 +256,7 @@ extension Defaults.Keys {
static let channelPlaylistListingStyle = Key<ListingStyle>("channelPlaylistListingStyle", default: .cells) static let channelPlaylistListingStyle = Key<ListingStyle>("channelPlaylistListingStyle", default: .cells)
static let searchListingStyle = Key<ListingStyle>("searchListingStyle", default: .cells) static let searchListingStyle = Key<ListingStyle>("searchListingStyle", default: .cells)
static let hideShorts = Key<Bool>("hideShorts", default: false) static let hideShorts = Key<Bool>("hideShorts", default: false)
static let hideWatched = Key<Bool>("hideWatched", default: false)
static let showInspector = Key<ShowInspectorSetting>("showInspector", default: .onlyLocal) static let showInspector = Key<ShowInspectorSetting>("showInspector", default: .onlyLocal)
} }

View File

@ -7,22 +7,17 @@ struct PlayerQueueRow: View {
let item: PlayerQueueItem let item: PlayerQueueItem
var history = false var history = false
var autoplay = false var autoplay = false
var watch: Watch?
private var player = PlayerModel.shared private var player = PlayerModel.shared
@Default(.closePiPOnNavigation) var closePiPOnNavigation @Default(.closePiPOnNavigation) var closePiPOnNavigation
@FetchRequest private var watchRequest: FetchedResults<Watch> init(item: PlayerQueueItem, history: Bool = false, autoplay: Bool = false, watch: Watch? = nil) {
init(item: PlayerQueueItem, history: Bool = false, autoplay: Bool = false) {
self.item = item self.item = item
self.history = history self.history = history
self.autoplay = autoplay self.autoplay = autoplay
_watchRequest = FetchRequest<Watch>( self.watch = watch
entity: Watch.entity(),
sortDescriptors: [],
predicate: NSPredicate(format: "videoID = %@", item.videoID)
)
} }
var body: some View { var body: some View {
@ -80,10 +75,6 @@ struct PlayerQueueRow: View {
#endif #endif
} }
private var watch: Watch? {
watchRequest.first
}
private var watchStoppedAt: CMTime? { private var watchStoppedAt: CMTime? {
guard let seconds = watch?.stoppedAt else { guard let seconds = watch?.stoppedAt else {
return nil return nil

View File

@ -145,6 +145,9 @@ struct PlaylistsView: View {
ToolbarItem { ToolbarItem {
ListingStyleButtons(listingStyle: $playlistListingStyle) ListingStyleButtons(listingStyle: $playlistListingStyle)
} }
ToolbarItem {
HideWatchedButtons()
}
ToolbarItem { ToolbarItem {
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
} }
@ -216,6 +219,7 @@ struct PlaylistsView: View {
ListingStyleButtons(listingStyle: $playlistListingStyle) ListingStyleButtons(listingStyle: $playlistListingStyle)
Section { Section {
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
} }
@ -386,6 +390,7 @@ struct PlaylistsView: View {
Spacer() Spacer()
ListingStyleButtons(listingStyle: $playlistListingStyle) ListingStyleButtons(listingStyle: $playlistListingStyle)
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
#else #else
Spacer() Spacer()

View File

@ -76,6 +76,7 @@ struct SearchView: View {
#if os(macOS) #if os(macOS)
ToolbarItemGroup(placement: toolbarPlacement) { ToolbarItemGroup(placement: toolbarPlacement) {
ListingStyleButtons(listingStyle: $searchListingStyle) ListingStyleButtons(listingStyle: $searchListingStyle)
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
FavoriteButton(item: favoriteItem) FavoriteButton(item: favoriteItem)
.id(favoriteItem?.id) .id(favoriteItem?.id)
@ -214,6 +215,7 @@ struct SearchView: View {
ListingStyleButtons(listingStyle: $searchListingStyle) ListingStyleButtons(listingStyle: $searchListingStyle)
Section { Section {
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
} }
@ -571,6 +573,7 @@ struct SearchView: View {
Spacer() Spacer()
ListingStyleButtons(listingStyle: $searchListingStyle) ListingStyleButtons(listingStyle: $searchListingStyle)
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
} }
.labelStyle(.iconOnly) .labelStyle(.iconOnly)

View File

@ -54,6 +54,7 @@ struct FeedView: View {
#if os(tvOS) #if os(tvOS)
SubscriptionsPageButton() SubscriptionsPageButton()
ListingStyleButtons(listingStyle: $subscriptionsListingStyle) ListingStyleButtons(listingStyle: $subscriptionsListingStyle)
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
#endif #endif

View File

@ -46,6 +46,7 @@ struct SubscriptionsView: View {
.toolbar { .toolbar {
ToolbarItemGroup { ToolbarItemGroup {
ListingStyleButtons(listingStyle: $subscriptionsListingStyle) ListingStyleButtons(listingStyle: $subscriptionsListingStyle)
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
toggleWatchedButton toggleWatchedButton
.id(feed.watchedId) .id(feed.watchedId)
@ -73,6 +74,7 @@ struct SubscriptionsView: View {
} }
Section { Section {
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
} }

View File

@ -123,6 +123,10 @@ struct TrendingView: View {
ListingStyleButtons(listingStyle: $trendingListingStyle) ListingStyleButtons(listingStyle: $trendingListingStyle)
} }
ToolbarItem {
HideWatchedButtons()
}
ToolbarItem { ToolbarItem {
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
} }
@ -174,6 +178,7 @@ struct TrendingView: View {
ListingStyleButtons(listingStyle: $trendingListingStyle) ListingStyleButtons(listingStyle: $trendingListingStyle)
Section { Section {
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
} }
@ -247,6 +252,7 @@ struct TrendingView: View {
Spacer() Spacer()
ListingStyleButtons(listingStyle: $trendingListingStyle) ListingStyleButtons(listingStyle: $trendingListingStyle)
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
Button { Button {

View File

@ -6,6 +6,7 @@ import SwiftUI
struct VideoCell: View { struct VideoCell: View {
var id: String? var id: String?
private var video: Video private var video: Video
private var watch: Watch?
@Environment(\.horizontalCells) private var horizontalCells @Environment(\.horizontalCells) private var horizontalCells
@Environment(\.inChannelView) private var inChannelView @Environment(\.inChannelView) private var inChannelView
@ -27,12 +28,10 @@ struct VideoCell: View {
private var navigation: NavigationModel { .shared } private var navigation: NavigationModel { .shared }
private var player: PlayerModel { .shared } private var player: PlayerModel { .shared }
@FetchRequest private var watchRequest: FetchedResults<Watch> init(id: String? = nil, video: Video, watch: Watch? = nil) {
init(id: String? = nil, video: Video) {
self.id = id self.id = id
self.video = video self.video = video
_watchRequest = video.watchFetchRequest self.watch = watch
} }
var body: some View { var body: some View {
@ -107,10 +106,6 @@ struct VideoCell: View {
watchedVideoPlayNowBehavior == .continue watchedVideoPlayNowBehavior == .continue
} }
private var watch: Watch? {
watchRequest.first
}
private var finished: Bool { private var finished: Bool {
watch?.finished ?? false watch?.finished ?? false
} }

View File

@ -1,3 +1,4 @@
import Defaults
import Foundation import Foundation
import SwiftUI import SwiftUI
@ -6,6 +7,18 @@ struct ContentItemView: View {
@Environment(\.listingStyle) private var listingStyle @Environment(\.listingStyle) private var listingStyle
@Environment(\.noListingDividers) private var noListingDividers @Environment(\.noListingDividers) private var noListingDividers
@Environment(\.hideShorts) private var hideShorts @Environment(\.hideShorts) private var hideShorts
@Default(.hideWatched) private var hideWatched
@FetchRequest private var watchRequest: FetchedResults<Watch>
init(item: ContentItem) {
self.item = item
if item.contentType == .video, let video = item.video {
_watchRequest = video.watchFetchRequest
} else {
_watchRequest = Video.fixture.watchFetchRequest
}
}
@ViewBuilder var body: some View { @ViewBuilder var body: some View {
if itemVisible { if itemVisible {
@ -26,6 +39,10 @@ struct ContentItemView: View {
} }
var itemVisible: Bool { var itemVisible: Bool {
if hideWatched, watch?.finished ?? false {
return false
}
guard hideShorts, item.contentType == .video, let video = item.video else { guard hideShorts, item.contentType == .video, let video = item.video else {
return true return true
} }
@ -35,10 +52,10 @@ struct ContentItemView: View {
@ViewBuilder func videoItem(_ video: Video) -> some View { @ViewBuilder func videoItem(_ video: Video) -> some View {
if listingStyle == .cells { if listingStyle == .cells {
VideoCell(video: video) VideoCell(video: video, watch: watch)
} else { } else {
let item = PlayerQueueItem(video) let item = PlayerQueueItem(video)
PlayerQueueRow(item: item) PlayerQueueRow(item: item, watch: watch)
.contextMenu { .contextMenu {
VideoContextMenuView(video: video) VideoContextMenuView(video: video)
} }
@ -99,4 +116,8 @@ struct ContentItemView: View {
#endif #endif
} }
} }
private var watch: Watch? {
watchRequest.first
}
} }

View File

@ -0,0 +1,35 @@
import Defaults
import SwiftUI
struct HideWatchedButtons: View {
@Default(.hideWatched) private var hideWatched
var body: some View {
Button {
hideWatched.toggle()
} label: {
Group {
if hideWatched {
Label("Watched: hidden", systemImage: "clock")
.help("Watched: hidden")
} else {
Label("Watched: visible", systemImage: "clock.fill")
.help("Watched: visible")
}
}
#if os(tvOS)
.font(.caption)
.imageScale(.small)
#endif
}
.transaction { t in t.disablesAnimations = true }
}
}
struct HideWatchedButtons_Previews: PreviewProvider {
static var previews: some View {
VStack {
HideWatchedButtons()
}
}
}

View File

@ -71,6 +71,10 @@ struct PopularView: View {
ListingStyleButtons(listingStyle: $popularListingStyle) ListingStyleButtons(listingStyle: $popularListingStyle)
} }
ToolbarItem {
HideWatchedButtons()
}
ToolbarItem { ToolbarItem {
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
} }
@ -90,6 +94,7 @@ struct PopularView: View {
ListingStyleButtons(listingStyle: $popularListingStyle) ListingStyleButtons(listingStyle: $popularListingStyle)
Section { Section {
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
} }
@ -129,6 +134,7 @@ struct PopularView: View {
HStack { HStack {
Spacer() Spacer()
ListingStyleButtons(listingStyle: $popularListingStyle) ListingStyleButtons(listingStyle: $popularListingStyle)
HideWatchedButtons()
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
Button { Button {

View File

@ -550,6 +550,9 @@
3774127627387EA200423605 /* Siesta in Frameworks */ = {isa = PBXBuildFile; productRef = 3774127527387EA200423605 /* Siesta */; }; 3774127627387EA200423605 /* Siesta in Frameworks */ = {isa = PBXBuildFile; productRef = 3774127527387EA200423605 /* Siesta */; };
3774127A27387EBC00423605 /* Defaults in Frameworks */ = {isa = PBXBuildFile; productRef = 3774127927387EBC00423605 /* Defaults */; }; 3774127A27387EBC00423605 /* Defaults in Frameworks */ = {isa = PBXBuildFile; productRef = 3774127927387EBC00423605 /* Defaults */; };
3774127C27387EC800423605 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 3774127B27387EC800423605 /* Alamofire */; }; 3774127C27387EC800423605 /* Alamofire in Frameworks */ = {isa = PBXBuildFile; productRef = 3774127B27387EC800423605 /* Alamofire */; };
37758C0B2A1D1C8B001FD900 /* HideWatchedButtons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37758C0A2A1D1C8B001FD900 /* HideWatchedButtons.swift */; };
37758C0C2A1D1C8B001FD900 /* HideWatchedButtons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37758C0A2A1D1C8B001FD900 /* HideWatchedButtons.swift */; };
37758C0D2A1D1C8B001FD900 /* HideWatchedButtons.swift in Sources */ = {isa = PBXBuildFile; fileRef = 37758C0A2A1D1C8B001FD900 /* HideWatchedButtons.swift */; };
3776924E294630110055EC18 /* ChannelAvatarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3776924D294630110055EC18 /* ChannelAvatarView.swift */; }; 3776924E294630110055EC18 /* ChannelAvatarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3776924D294630110055EC18 /* ChannelAvatarView.swift */; };
3776924F294630110055EC18 /* ChannelAvatarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3776924D294630110055EC18 /* ChannelAvatarView.swift */; }; 3776924F294630110055EC18 /* ChannelAvatarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3776924D294630110055EC18 /* ChannelAvatarView.swift */; };
37769250294630110055EC18 /* ChannelAvatarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3776924D294630110055EC18 /* ChannelAvatarView.swift */; }; 37769250294630110055EC18 /* ChannelAvatarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3776924D294630110055EC18 /* ChannelAvatarView.swift */; };
@ -1327,6 +1330,7 @@
37732FEF2703A26300F04329 /* AccountValidationStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountValidationStatus.swift; sourceTree = "<group>"; }; 37732FEF2703A26300F04329 /* AccountValidationStatus.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountValidationStatus.swift; sourceTree = "<group>"; };
37732FF32703D32400F04329 /* Sidebar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sidebar.swift; sourceTree = "<group>"; }; 37732FF32703D32400F04329 /* Sidebar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Sidebar.swift; sourceTree = "<group>"; };
37737785276F9858000521C1 /* Windows.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Windows.swift; sourceTree = "<group>"; }; 37737785276F9858000521C1 /* Windows.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Windows.swift; sourceTree = "<group>"; };
37758C0A2A1D1C8B001FD900 /* HideWatchedButtons.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HideWatchedButtons.swift; sourceTree = "<group>"; };
3776924D294630110055EC18 /* ChannelAvatarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelAvatarView.swift; sourceTree = "<group>"; }; 3776924D294630110055EC18 /* ChannelAvatarView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelAvatarView.swift; sourceTree = "<group>"; };
3776925129463C310055EC18 /* PlaylistsCacheModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaylistsCacheModel.swift; sourceTree = "<group>"; }; 3776925129463C310055EC18 /* PlaylistsCacheModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlaylistsCacheModel.swift; sourceTree = "<group>"; };
377692552946476F0055EC18 /* ChannelPlaylistsCacheModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelPlaylistsCacheModel.swift; sourceTree = "<group>"; }; 377692552946476F0055EC18 /* ChannelPlaylistsCacheModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelPlaylistsCacheModel.swift; sourceTree = "<group>"; };
@ -1876,6 +1880,7 @@
3748186D26A769D60084E870 /* DetailBadge.swift */, 3748186D26A769D60084E870 /* DetailBadge.swift */,
37599F37272B4D740087F250 /* FavoriteButton.swift */, 37599F37272B4D740087F250 /* FavoriteButton.swift */,
379EF9DF29AA585F009FE6C6 /* HideShortsButtons.swift */, 379EF9DF29AA585F009FE6C6 /* HideShortsButtons.swift */,
37758C0A2A1D1C8B001FD900 /* HideWatchedButtons.swift */,
37152EE926EFEB95004FB96D /* LazyView.swift */, 37152EE926EFEB95004FB96D /* LazyView.swift */,
371CC7732946963000979C1A /* ListingStyleButtons.swift */, 371CC7732946963000979C1A /* ListingStyleButtons.swift */,
37030FF627B0347C00ECDDAA /* MPVPlayerView.swift */, 37030FF627B0347C00ECDDAA /* MPVPlayerView.swift */,
@ -3131,6 +3136,7 @@
37F64FE426FE70A60081B69E /* RedrawOnModifier.swift in Sources */, 37F64FE426FE70A60081B69E /* RedrawOnModifier.swift in Sources */,
37EBD8C427AF0DA800F1C24B /* PlayerBackend.swift in Sources */, 37EBD8C427AF0DA800F1C24B /* PlayerBackend.swift in Sources */,
376A33E02720CAD6000C1D6B /* VideosApp.swift in Sources */, 376A33E02720CAD6000C1D6B /* VideosApp.swift in Sources */,
37758C0B2A1D1C8B001FD900 /* HideWatchedButtons.swift in Sources */,
374AB3DB28BCAF7E00DF56FB /* SeekType.swift in Sources */, 374AB3DB28BCAF7E00DF56FB /* SeekType.swift in Sources */,
37192D5728B179D60012EEDD /* ChaptersView.swift in Sources */, 37192D5728B179D60012EEDD /* ChaptersView.swift in Sources */,
37D836BC294927E700005E5E /* ChannelsCacheModel.swift in Sources */, 37D836BC294927E700005E5E /* ChannelsCacheModel.swift in Sources */,
@ -3443,6 +3449,7 @@
37F7AB5528A951B200FB46B5 /* Power.swift in Sources */, 37F7AB5528A951B200FB46B5 /* Power.swift in Sources */,
372CFD16285F2E2A00B0B54B /* ControlsBar.swift in Sources */, 372CFD16285F2E2A00B0B54B /* ControlsBar.swift in Sources */,
37FFC441272734C3009FFD26 /* Throttle.swift in Sources */, 37FFC441272734C3009FFD26 /* Throttle.swift in Sources */,
37758C0C2A1D1C8B001FD900 /* HideWatchedButtons.swift in Sources */,
37169AA72729E2CC0011DE61 /* AccountsBridge.swift in Sources */, 37169AA72729E2CC0011DE61 /* AccountsBridge.swift in Sources */,
379EF9E129AA585F009FE6C6 /* HideShortsButtons.swift in Sources */, 379EF9E129AA585F009FE6C6 /* HideShortsButtons.swift in Sources */,
37BA793C26DB8EE4002A0235 /* PlaylistVideosView.swift in Sources */, 37BA793C26DB8EE4002A0235 /* PlaylistVideosView.swift in Sources */,
@ -3832,6 +3839,7 @@
372D85DF283842EC00FF3C7D /* PiPDelegate.swift in Sources */, 372D85DF283842EC00FF3C7D /* PiPDelegate.swift in Sources */,
378E9C3A294552A700B2D696 /* ThumbnailView.swift in Sources */, 378E9C3A294552A700B2D696 /* ThumbnailView.swift in Sources */,
372D85E0283842EE00FF3C7D /* PlayerLayerView.swift in Sources */, 372D85E0283842EE00FF3C7D /* PlayerLayerView.swift in Sources */,
37758C0D2A1D1C8B001FD900 /* HideWatchedButtons.swift in Sources */,
37CEE4C32677B697005A1EFE /* Stream.swift in Sources */, 37CEE4C32677B697005A1EFE /* Stream.swift in Sources */,
37F64FE626FE70A60081B69E /* RedrawOnModifier.swift in Sources */, 37F64FE626FE70A60081B69E /* RedrawOnModifier.swift in Sources */,
37DCD3192A191A180059A470 /* AVPlayerViewController+FullScreen.swift in Sources */, 37DCD3192A191A180059A470 /* AVPlayerViewController+FullScreen.swift in Sources */,