2021-12-27 02:44:46 +05:30
|
|
|
import CoreData
|
|
|
|
import CoreMedia
|
2022-01-03 01:09:19 +05:30
|
|
|
import Defaults
|
2021-12-27 02:44:46 +05:30
|
|
|
import Foundation
|
2022-11-10 22:41:28 +05:30
|
|
|
import SwiftyJSON
|
2021-12-27 02:44:46 +05:30
|
|
|
|
|
|
|
extension PlayerModel {
|
|
|
|
func historyVideo(_ id: String) -> Video? {
|
|
|
|
historyVideos.first { $0.videoID == id }
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadHistoryVideoDetails(_ id: Video.ID) {
|
|
|
|
guard historyVideo(id).isNil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-10 22:41:28 +05:30
|
|
|
if !Video.VideoID.isValid(id), let url = URL(string: id) {
|
|
|
|
historyVideos.append(.local(url))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if historyItemBeingLoaded == nil {
|
|
|
|
logger.info("loading history details: \(id)")
|
|
|
|
historyItemBeingLoaded = id
|
|
|
|
} else {
|
|
|
|
logger.info("POSTPONING history load: \(id)")
|
|
|
|
historyItemsToLoad.append(id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-17 02:46:35 +05:30
|
|
|
playerAPI.video(id).load().onSuccess { [weak self] response in
|
2022-11-10 22:41:28 +05:30
|
|
|
guard let self else { return }
|
|
|
|
|
|
|
|
if let video: Video = response.typedContent() {
|
|
|
|
self.historyVideos.append(video)
|
2021-12-27 02:44:46 +05:30
|
|
|
}
|
2022-11-10 22:41:28 +05:30
|
|
|
}.onCompletion { _ in
|
|
|
|
self.logger.info("LOADED history details: \(id)")
|
2021-12-27 02:44:46 +05:30
|
|
|
|
2022-11-10 22:41:28 +05:30
|
|
|
if self.historyItemBeingLoaded == id {
|
|
|
|
self.logger.info("setting no history loaded")
|
|
|
|
self.historyItemBeingLoaded = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if let id = self.historyItemsToLoad.popLast() {
|
|
|
|
self.loadHistoryVideoDetails(id)
|
|
|
|
}
|
2021-12-27 02:44:46 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateWatch(finished: Bool = false) {
|
2022-02-17 01:53:11 +05:30
|
|
|
guard let id = currentVideo?.videoID,
|
|
|
|
Defaults[.saveHistory]
|
|
|
|
else {
|
2021-12-27 02:44:46 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-17 01:53:11 +05:30
|
|
|
let time = backend.currentTime
|
|
|
|
let seconds = time?.seconds ?? 0
|
2021-12-27 02:44:46 +05:30
|
|
|
|
|
|
|
let watchFetchRequest = Watch.fetchRequest()
|
|
|
|
watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", id as String)
|
|
|
|
|
2022-06-08 03:45:15 +05:30
|
|
|
let results = try? backgroundContext.fetch(watchFetchRequest)
|
2021-12-27 02:44:46 +05:30
|
|
|
|
2022-06-08 03:45:15 +05:30
|
|
|
backgroundContext.perform { [weak self] in
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let self else {
|
2021-12-27 02:44:46 +05:30
|
|
|
return
|
|
|
|
}
|
2022-01-03 01:09:19 +05:30
|
|
|
|
2022-06-08 03:45:15 +05:30
|
|
|
let watch: Watch!
|
|
|
|
|
|
|
|
if results?.isEmpty ?? true {
|
|
|
|
if seconds < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
watch = Watch(context: self.backgroundContext)
|
|
|
|
watch.videoID = id
|
|
|
|
} else {
|
|
|
|
watch = results?.first
|
|
|
|
|
2022-08-08 22:55:42 +05:30
|
|
|
if !self.resetWatchedStatusOnPlaying, watch.finished {
|
2022-06-08 03:45:15 +05:30
|
|
|
return
|
|
|
|
}
|
2022-01-03 01:09:19 +05:30
|
|
|
}
|
2021-12-27 02:44:46 +05:30
|
|
|
|
2022-06-08 03:45:15 +05:30
|
|
|
if let seconds = self.playerItemDuration?.seconds {
|
|
|
|
watch.videoDuration = seconds
|
|
|
|
}
|
2021-12-27 02:44:46 +05:30
|
|
|
|
2022-06-08 03:45:15 +05:30
|
|
|
if finished {
|
|
|
|
watch.stoppedAt = watch.videoDuration
|
|
|
|
} else if seconds.isFinite, seconds > 0 {
|
|
|
|
watch.stoppedAt = seconds
|
|
|
|
}
|
2021-12-27 02:44:46 +05:30
|
|
|
|
2022-06-08 03:45:15 +05:30
|
|
|
watch.watchedAt = Date()
|
2021-12-27 02:44:46 +05:30
|
|
|
|
2022-06-08 03:45:15 +05:30
|
|
|
try? self.backgroundContext.save()
|
|
|
|
}
|
2021-12-27 02:44:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|