mirror of
https://github.com/yattee/yattee.git
synced 2024-12-15 06:40:32 +05:30
Add "Mark as watched" video context menu item (#193)
This commit is contained in:
parent
23075751c3
commit
5bac96ac26
@ -14,6 +14,32 @@ extension Watch {
|
|||||||
NSFetchRequest<Watch>(entityName: "Watch")
|
NSFetchRequest<Watch>(entityName: "Watch")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@nonobjc class func markAsWatched(videoID: String, duration: Double, context: NSManagedObjectContext) {
|
||||||
|
let watchFetchRequest = Watch.fetchRequest()
|
||||||
|
watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", videoID as String)
|
||||||
|
|
||||||
|
let results = try? context.fetch(watchFetchRequest)
|
||||||
|
|
||||||
|
context.perform {
|
||||||
|
let watch: Watch?
|
||||||
|
|
||||||
|
if results?.isEmpty ?? true {
|
||||||
|
watch = Watch(context: context)
|
||||||
|
watch?.videoID = videoID
|
||||||
|
} else {
|
||||||
|
watch = results?.first
|
||||||
|
}
|
||||||
|
|
||||||
|
guard let watch = watch else { return }
|
||||||
|
|
||||||
|
watch.videoDuration = duration
|
||||||
|
watch.stoppedAt = duration
|
||||||
|
watch.watchedAt = Date()
|
||||||
|
|
||||||
|
try? context.save()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@NSManaged var videoID: String
|
@NSManaged var videoID: String
|
||||||
@NSManaged var videoDuration: Double
|
@NSManaged var videoDuration: Double
|
||||||
|
|
||||||
|
@ -22,6 +22,7 @@ struct VideoContextMenuView: View {
|
|||||||
|
|
||||||
@Default(.saveHistory) private var saveHistory
|
@Default(.saveHistory) private var saveHistory
|
||||||
|
|
||||||
|
private var backgroundContext = PersistenceController.shared.container.newBackgroundContext()
|
||||||
private var viewContext: NSManagedObjectContext = PersistenceController.shared.container.viewContext
|
private var viewContext: NSManagedObjectContext = PersistenceController.shared.container.viewContext
|
||||||
|
|
||||||
init(video: Video) {
|
init(video: Video) {
|
||||||
@ -46,6 +47,10 @@ struct VideoContextMenuView: View {
|
|||||||
continueButton
|
continueButton
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if !(watch?.finished ?? false) {
|
||||||
|
markAsWatchedButton
|
||||||
|
}
|
||||||
|
|
||||||
if !watch.isNil, !watchingNow {
|
if !watch.isNil, !watchingNow {
|
||||||
removeFromHistoryButton
|
removeFromHistoryButton
|
||||||
}
|
}
|
||||||
@ -113,6 +118,9 @@ struct VideoContextMenuView: View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if let watch = watch, let watchedAtString = watch.watchedAtString {
|
if let watch = watch, let watchedAtString = watch.watchedAtString {
|
||||||
|
if watchedAtString == "in 0 seconds" {
|
||||||
|
return "Just watched"
|
||||||
|
}
|
||||||
return "Watched \(watchedAtString)"
|
return "Watched \(watchedAtString)"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -127,6 +135,14 @@ struct VideoContextMenuView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var markAsWatchedButton: some View {
|
||||||
|
Button {
|
||||||
|
Watch.markAsWatched(videoID: video.videoID, duration: video.length, context: backgroundContext)
|
||||||
|
} label: {
|
||||||
|
Label("Mark as watched", systemImage: "checkmark.circle.fill")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var removeFromHistoryButton: some View {
|
var removeFromHistoryButton: some View {
|
||||||
Button {
|
Button {
|
||||||
guard let watch = watch else {
|
guard let watch = watch else {
|
||||||
|
Loading…
Reference in New Issue
Block a user