1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-12 21:30:32 +05:30
yattee/Model/HistoryModel.swift

129 lines
3.9 KiB
Swift
Raw Permalink Normal View History

import CoreData
import CoreMedia
import Defaults
import Foundation
2022-12-09 05:45:19 +05:30
import Siesta
2022-11-10 22:41:28 +05:30
import SwiftyJSON
extension PlayerModel {
func historyVideo(_ id: String) -> Video? {
historyVideos.first { $0.videoID == id }
}
2023-05-25 17:58:29 +05:30
func loadHistoryVideoDetails(_ watch: Watch, onCompletion: @escaping () -> Void = {}) {
2022-12-09 05:45:19 +05:30
guard historyVideo(watch.videoID).isNil else {
2023-05-25 17:58:29 +05:30
onCompletion()
return
}
2022-12-09 05:45:19 +05:30
if !Video.VideoID.isValid(watch.videoID), let url = URL(string: watch.videoID) {
2022-11-10 22:41:28 +05:30
historyVideos.append(.local(url))
2023-05-25 17:58:29 +05:30
onCompletion()
2022-11-10 22:41:28 +05:30
return
}
2022-12-10 05:53:13 +05:30
if let video = VideosCacheModel.shared.retrieveVideo(watch.video.cacheKey) {
historyVideos.append(video)
2023-05-25 17:58:29 +05:30
onCompletion()
2022-12-10 05:53:13 +05:30
return
}
2022-12-10 01:03:01 +05:30
guard let api = playerAPI(watch.video) else { return }
api.video(watch.videoID)
.load()
2022-11-11 03:21:34 +05:30
.onSuccess { [weak self] response in
guard let self else { return }
2022-11-10 22:41:28 +05:30
2022-11-11 03:21:34 +05:30
if let video: Video = response.typedContent() {
2022-12-10 05:53:13 +05:30
VideosCacheModel.shared.storeVideo(video)
2022-11-11 03:21:34 +05:30
self.historyVideos.append(video)
2023-05-25 17:58:29 +05:30
onCompletion()
2022-11-11 03:21:34 +05:30
}
}
2022-11-11 03:21:34 +05:30
.onCompletion { _ in
2022-12-09 05:45:19 +05:30
self.logger.info("LOADED history details: \(watch.videoID)")
2022-11-10 22:41:28 +05:30
}
}
2023-06-08 01:55:10 +05:30
func updateWatch(finished: Bool = false, time: CMTime? = nil) {
guard let currentVideo, saveHistory, isPlaying else { return }
2022-12-09 05:45:19 +05:30
let id = currentVideo.videoID
2023-06-08 01:55:10 +05:30
let time = time ?? backend.currentTime
2022-02-17 01:53:11 +05:30
let seconds = time?.seconds ?? 0
2022-12-16 03:09:42 +05:30
if seconds < 3 {
return
}
let watchFetchRequest = Watch.fetchRequest()
watchFetchRequest.predicate = NSPredicate(format: "videoID = %@", id as String)
let results = try? backgroundContext.fetch(watchFetchRequest)
backgroundContext.perform { [weak self] in
2023-06-08 01:55:10 +05:30
guard let self, finished || time != nil || self.backend.isPlaying else {
return
}
let watch: Watch!
2023-05-25 22:33:59 +05:30
let duration = self.activeBackend == .mpv ? self.playerTime.duration.seconds : self.avPlayerBackend.playerItemDuration?.seconds ?? 0
if results?.isEmpty ?? true {
watch = Watch(context: self.backgroundContext)
watch.videoID = id
2022-12-09 05:45:19 +05:30
watch.appName = currentVideo.app.rawValue
watch.instanceURL = currentVideo.instanceURL
} else {
watch = results?.first
}
2022-12-04 16:37:06 +05:30
if duration.isFinite, duration > 0 {
watch.videoDuration = duration
}
2022-12-04 16:37:06 +05:30
if watch.finished {
if !finished, self.resetWatchedStatusOnPlaying, seconds.isFinite, seconds > 0 {
watch.stoppedAt = seconds
}
} else if seconds.isFinite, seconds > 0 {
watch.stoppedAt = seconds
}
watch.watchedAt = Date()
try? self.backgroundContext.save()
}
}
2022-11-15 16:52:27 +05:30
func removeHistory() {
removeAllWatches()
2022-12-11 01:38:03 +05:30
BookmarksCacheModel.shared.clear()
2022-11-15 16:52:27 +05:30
}
func removeWatch(_ watch: Watch) {
2022-12-16 04:21:49 +05:30
context.perform { [weak self] in
guard let self else { return }
self.context.delete(watch)
try? self.context.save()
FeedModel.shared.calculateUnwatchedFeed()
2023-05-25 17:58:29 +05:30
WatchModel.shared.watchesChanged()
2022-12-16 04:21:49 +05:30
}
}
func removeAllWatches() {
let watchesFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: "Watch")
let deleteRequest = NSBatchDeleteRequest(fetchRequest: watchesFetchRequest)
2023-05-25 17:58:29 +05:30
do {
try context.executeAndMergeChanges(deleteRequest)
try context.save()
} catch let error as NSError {
logger.info(.init(stringLiteral: error.localizedDescription))
}
}
}