2021-12-26 21:14:46 +00:00
|
|
|
import CoreData
|
|
|
|
import CoreMedia
|
2022-01-02 19:39:19 +00:00
|
|
|
import Defaults
|
2021-12-26 21:14:46 +00:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
extension PlayerModel {
|
|
|
|
func historyVideo(_ id: String) -> Video? {
|
|
|
|
historyVideos.first { $0.videoID == id }
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadHistoryVideoDetails(_ id: Video.ID) {
|
|
|
|
guard historyVideo(id).isNil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
accounts.api.video(id).load().onSuccess { [weak self] response in
|
|
|
|
guard let video: Video = response.typedContent() else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
self?.historyVideos.append(video)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateWatch(finished: Bool = false) {
|
2022-02-16 20:23:11 +00:00
|
|
|
guard let id = currentVideo?.videoID,
|
|
|
|
Defaults[.saveHistory]
|
|
|
|
else {
|
2021-12-26 21:14:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-16 20:23:11 +00:00
|
|
|
let time = backend.currentTime
|
|
|
|
let seconds = time?.seconds ?? 0
|
2021-12-26 21:14:46 +00:00
|
|
|
|
|
|
|
let watchFetchRequest = Watch.fetchRequest()
|
|
|
|
watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", id as String)
|
|
|
|
|
2022-06-07 22:15:15 +00:00
|
|
|
let results = try? backgroundContext.fetch(watchFetchRequest)
|
2021-12-26 21:14:46 +00:00
|
|
|
|
2022-06-07 22:15:15 +00:00
|
|
|
backgroundContext.perform { [weak self] in
|
|
|
|
guard let self = self else {
|
2021-12-26 21:14:46 +00:00
|
|
|
return
|
|
|
|
}
|
2022-01-02 19:39:19 +00:00
|
|
|
|
2022-06-07 22:15:15 +00:00
|
|
|
let watch: Watch!
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-01-02 19:39:19 +00:00
|
|
|
}
|
2021-12-26 21:14:46 +00:00
|
|
|
|
2022-06-07 22:15:15 +00:00
|
|
|
if let seconds = self.playerItemDuration?.seconds {
|
|
|
|
watch.videoDuration = seconds
|
|
|
|
}
|
2021-12-26 21:14:46 +00:00
|
|
|
|
2022-06-07 22:15:15 +00:00
|
|
|
if finished {
|
|
|
|
watch.stoppedAt = watch.videoDuration
|
|
|
|
} else if seconds.isFinite, seconds > 0 {
|
|
|
|
watch.stoppedAt = seconds
|
|
|
|
}
|
2021-12-26 21:14:46 +00:00
|
|
|
|
2022-06-07 22:15:15 +00:00
|
|
|
watch.watchedAt = Date()
|
2021-12-26 21:14:46 +00:00
|
|
|
|
2022-06-07 22:15:15 +00:00
|
|
|
try? self.backgroundContext.save()
|
|
|
|
}
|
2021-12-26 21:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeWatch(_ watch: Watch) {
|
|
|
|
context.delete(watch)
|
|
|
|
try? context.save()
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeAllWatches() {
|
|
|
|
let watchesFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Watch")
|
|
|
|
let deleteRequest = NSBatchDeleteRequest(fetchRequest: watchesFetchRequest)
|
|
|
|
_ = try? context.execute(deleteRequest)
|
|
|
|
_ = try? context.save()
|
|
|
|
}
|
|
|
|
}
|