Fix handling watch statuses

This commit is contained in:
Arkadiusz Fal 2023-04-22 17:59:28 +02:00
parent c8feeca41f
commit f852782f5e
3 changed files with 16 additions and 23 deletions

View File

@ -11,6 +11,7 @@ final class FeedModel: ObservableObject, CacheModel {
@Published var isLoading = false @Published var isLoading = false
@Published var videos = [Video]() @Published var videos = [Video]()
@Published private var page = 1 @Published private var page = 1
@Published var watchedUUID = UUID()
private var feedCount = UnwatchedFeedCountModel.shared private var feedCount = UnwatchedFeedCountModel.shared
private var cacheModel = FeedCacheModel.shared private var cacheModel = FeedCacheModel.shared
@ -115,7 +116,7 @@ final class FeedModel: ObservableObject, CacheModel {
} }
func calculateUnwatchedFeed() { 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) let feed = cacheModel.retrieveFeed(account: account)
backgroundContext.perform { [weak self] in backgroundContext.perform { [weak self] in
guard let self else { return } guard let self else { return }
@ -132,20 +133,15 @@ final class FeedModel: ObservableObject, CacheModel {
let byChannel = Dictionary(grouping: unwatched) { $0.channel.id }.mapValues(\.count) let byChannel = Dictionary(grouping: unwatched) { $0.channel.id }.mapValues(\.count)
self.feedCount.unwatchedByChannel[account] = byChannel self.feedCount.unwatchedByChannel[account] = byChannel
self.watchedUUID = UUID()
} }
} }
} }
func markAllFeedAsWatched() { func markAllFeedAsWatched() {
guard let account = accounts.current, accounts.signedIn else { return }
let mark = { [weak self] in let mark = { [weak self] in
self?.backgroundContext.perform { [weak self] in guard let self else { return }
guard let self else { return } self.markVideos(self.videos, watched: true, watchedAt: Date(timeIntervalSince1970: 0))
self.videos.forEach { Watch.markAsWatched(videoID: $0.videoID, account: account, duration: $0.length, context: self.backgroundContext) }
self.calculateUnwatchedFeed()
}
} }
if videos.isEmpty { 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 } guard accounts.signedIn, let account = accounts.current else { return }
backgroundContext.perform { [weak self] in backgroundContext.perform { [weak self] in
guard let self else { return } guard let self else { return }
if watched { 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 { } else {
let watches = self.watchFetchRequestResult(videos, context: self.backgroundContext) let watches = self.watchFetchRequestResult(videos, context: self.backgroundContext)
watches.forEach { self.backgroundContext.delete($0) } watches.forEach { self.backgroundContext.delete($0) }
@ -264,6 +260,10 @@ final class FeedModel: ObservableObject, CacheModel {
return (feedCount.unwatched[account] ?? 0) > 0 return (feedCount.unwatched[account] ?? 0) > 0
} }
var watchedId: String {
watchedUUID.uuidString
}
var feedTime: Date? { var feedTime: Date? {
if let account = accounts.current { if let account = accounts.current {
return cacheModel.getFeedTime(account: account) return cacheModel.getFeedTime(account: account)

View File

@ -14,7 +14,7 @@ extension Watch {
NSFetchRequest<Watch>(entityName: "Watch") NSFetchRequest<Watch>(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() let watchFetchRequest = Watch.fetchRequest()
watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", videoID as String) watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", videoID as String)
@ -36,7 +36,7 @@ extension Watch {
watch.videoDuration = duration watch.videoDuration = duration
watch.stoppedAt = duration watch.stoppedAt = duration
watch.watchedAt = Date() watch.watchedAt = watchedAt ?? .init()
try? context.save() try? context.save()
} }

View File

@ -44,20 +44,13 @@ struct SubscriptionsView: View {
#endif #endif
#if os(macOS) #if os(macOS)
.toolbar { .toolbar {
ToolbarItem { ToolbarItemGroup {
ListingStyleButtons(listingStyle: $subscriptionsListingStyle) ListingStyleButtons(listingStyle: $subscriptionsListingStyle)
}
ToolbarItem {
HideShortsButtons(hide: $hideShorts) HideShortsButtons(hide: $hideShorts)
}
ToolbarItem {
toggleWatchedButton toggleWatchedButton
} .id(feed.watchedId)
ToolbarItem {
playUnwatchedButton playUnwatchedButton
.id(feed.watchedId)
} }
} }
#endif #endif