diff --git a/Model/FeedModel.swift b/Model/FeedModel.swift index db7e2cf2..f9b5324f 100644 --- a/Model/FeedModel.swift +++ b/Model/FeedModel.swift @@ -11,6 +11,7 @@ final class FeedModel: ObservableObject, CacheModel { @Published var isLoading = false @Published var videos = [Video]() @Published private var page = 1 + @Published var watchedUUID = UUID() private var feedCount = UnwatchedFeedCountModel.shared private var cacheModel = FeedCacheModel.shared @@ -115,7 +116,7 @@ final class FeedModel: ObservableObject, CacheModel { } func calculateUnwatchedFeed() { - guard let account = accounts.current, accounts.signedIn, Defaults[.showUnwatchedFeedBadges] else { return } + guard let account = accounts.current, accounts.signedIn else { return } let feed = cacheModel.retrieveFeed(account: account) backgroundContext.perform { [weak self] in guard let self else { return } @@ -132,20 +133,15 @@ final class FeedModel: ObservableObject, CacheModel { let byChannel = Dictionary(grouping: unwatched) { $0.channel.id }.mapValues(\.count) self.feedCount.unwatchedByChannel[account] = byChannel + self.watchedUUID = UUID() } } } func markAllFeedAsWatched() { - guard let account = accounts.current, accounts.signedIn else { return } - let mark = { [weak self] in - self?.backgroundContext.perform { [weak self] in - guard let self else { return } - self.videos.forEach { Watch.markAsWatched(videoID: $0.videoID, account: account, duration: $0.length, context: self.backgroundContext) } - - self.calculateUnwatchedFeed() - } + guard let self else { return } + self.markVideos(self.videos, watched: true, watchedAt: Date(timeIntervalSince1970: 0)) } if videos.isEmpty { @@ -211,14 +207,14 @@ final class FeedModel: ObservableObject, CacheModel { } } - func markVideos(_ videos: [Video], watched: Bool) { + func markVideos(_ videos: [Video], watched: Bool, watchedAt: Date? = nil) { guard accounts.signedIn, let account = accounts.current else { return } backgroundContext.perform { [weak self] in guard let self else { return } if watched { - videos.forEach { Watch.markAsWatched(videoID: $0.videoID, account: account, duration: $0.length, context: self.backgroundContext) } + videos.forEach { Watch.markAsWatched(videoID: $0.videoID, account: account, duration: $0.length, watchedAt: watchedAt, context: self.backgroundContext) } } else { let watches = self.watchFetchRequestResult(videos, context: self.backgroundContext) watches.forEach { self.backgroundContext.delete($0) } @@ -264,6 +260,10 @@ final class FeedModel: ObservableObject, CacheModel { return (feedCount.unwatched[account] ?? 0) > 0 } + var watchedId: String { + watchedUUID.uuidString + } + var feedTime: Date? { if let account = accounts.current { return cacheModel.getFeedTime(account: account) diff --git a/Model/Watch.swift b/Model/Watch.swift index 27ca8edc..539ad071 100644 --- a/Model/Watch.swift +++ b/Model/Watch.swift @@ -14,7 +14,7 @@ extension Watch { NSFetchRequest(entityName: "Watch") } - @nonobjc class func markAsWatched(videoID: String, account: Account, duration: Double, context: NSManagedObjectContext) { + @nonobjc class func markAsWatched(videoID: String, account: Account, duration: Double, watchedAt: Date? = nil, context: NSManagedObjectContext) { let watchFetchRequest = Watch.fetchRequest() watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", videoID as String) @@ -36,7 +36,7 @@ extension Watch { watch.videoDuration = duration watch.stoppedAt = duration - watch.watchedAt = Date() + watch.watchedAt = watchedAt ?? .init() try? context.save() } diff --git a/Shared/Subscriptions/SubscriptionsView.swift b/Shared/Subscriptions/SubscriptionsView.swift index 1bd77d8f..7a5165b7 100644 --- a/Shared/Subscriptions/SubscriptionsView.swift +++ b/Shared/Subscriptions/SubscriptionsView.swift @@ -44,20 +44,13 @@ struct SubscriptionsView: View { #endif #if os(macOS) .toolbar { - ToolbarItem { + ToolbarItemGroup { ListingStyleButtons(listingStyle: $subscriptionsListingStyle) - } - - ToolbarItem { HideShortsButtons(hide: $hideShorts) - } - - ToolbarItem { toggleWatchedButton - } - - ToolbarItem { + .id(feed.watchedId) playUnwatchedButton + .id(feed.watchedId) } } #endif