mirror of
https://github.com/yattee/yattee.git
synced 2024-12-13 05:40:32 +05:30
Update watch history using background context
This commit is contained in:
parent
dbffa4ae08
commit
d659063897
@ -32,39 +32,46 @@ extension PlayerModel {
|
|||||||
let time = backend.currentTime
|
let time = backend.currentTime
|
||||||
let seconds = time?.seconds ?? 0
|
let seconds = time?.seconds ?? 0
|
||||||
|
|
||||||
let watch: Watch!
|
|
||||||
let watchFetchRequest = Watch.fetchRequest()
|
let watchFetchRequest = Watch.fetchRequest()
|
||||||
watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", id as String)
|
watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", id as String)
|
||||||
|
|
||||||
let results = try? context.fetch(watchFetchRequest)
|
let results = try? backgroundContext.fetch(watchFetchRequest)
|
||||||
|
|
||||||
if results?.isEmpty ?? true {
|
backgroundContext.perform { [weak self] in
|
||||||
if seconds < 1 {
|
guard let self = self else {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
watch = Watch(context: context)
|
|
||||||
watch.videoID = id
|
|
||||||
} else {
|
|
||||||
watch = results?.first
|
|
||||||
|
|
||||||
if !Defaults[.resetWatchedStatusOnPlaying], watch.finished {
|
let watch: Watch!
|
||||||
return
|
|
||||||
|
if results?.isEmpty ?? true {
|
||||||
|
if seconds < 1 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
watch = Watch(context: self.backgroundContext)
|
||||||
|
watch.videoID = id
|
||||||
|
} else {
|
||||||
|
watch = results?.first
|
||||||
|
|
||||||
|
if !Defaults[.resetWatchedStatusOnPlaying], watch.finished {
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let seconds = self.playerItemDuration?.seconds {
|
||||||
|
watch.videoDuration = seconds
|
||||||
|
}
|
||||||
|
|
||||||
|
if finished {
|
||||||
|
watch.stoppedAt = watch.videoDuration
|
||||||
|
} else if seconds.isFinite, seconds > 0 {
|
||||||
|
watch.stoppedAt = seconds
|
||||||
|
}
|
||||||
|
|
||||||
|
watch.watchedAt = Date()
|
||||||
|
|
||||||
|
try? self.backgroundContext.save()
|
||||||
}
|
}
|
||||||
|
|
||||||
if let seconds = playerItemDuration?.seconds {
|
|
||||||
watch.videoDuration = seconds
|
|
||||||
}
|
|
||||||
|
|
||||||
if finished {
|
|
||||||
watch.stoppedAt = watch.videoDuration
|
|
||||||
} else if seconds.isFinite, seconds > 0 {
|
|
||||||
watch.stoppedAt = seconds
|
|
||||||
}
|
|
||||||
|
|
||||||
watch.watchedAt = Date()
|
|
||||||
|
|
||||||
try? context.save()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func removeWatch(_ watch: Watch) {
|
func removeWatch(_ watch: Watch) {
|
||||||
|
@ -80,6 +80,7 @@ final class PlayerModel: ObservableObject {
|
|||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
var context: NSManagedObjectContext = PersistenceController.shared.container.viewContext
|
var context: NSManagedObjectContext = PersistenceController.shared.container.viewContext
|
||||||
|
var backgroundContext = PersistenceController.shared.container.newBackgroundContext()
|
||||||
|
|
||||||
@Published var playingInPictureInPicture = false
|
@Published var playingInPictureInPicture = false
|
||||||
var pipController: AVPictureInPictureController?
|
var pipController: AVPictureInPictureController?
|
||||||
|
Loading…
Reference in New Issue
Block a user