2021-12-26 21:14:46 +00:00
|
|
|
import CoreData
|
2022-05-29 15:50:54 +00:00
|
|
|
import CoreMedia
|
2021-12-26 21:14:46 +00:00
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
@objc(Watch)
|
|
|
|
final class Watch: NSManagedObject, Identifiable {
|
|
|
|
@Default(.watchedThreshold) private var watchedThreshold
|
2022-05-29 15:50:54 +00:00
|
|
|
@Default(.saveHistory) private var saveHistory
|
2021-12-26 21:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension Watch {
|
|
|
|
@nonobjc class func fetchRequest() -> NSFetchRequest<Watch> {
|
|
|
|
NSFetchRequest<Watch>(entityName: "Watch")
|
|
|
|
}
|
|
|
|
|
2022-06-30 10:10:43 +00:00
|
|
|
@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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
@NSManaged var videoID: String
|
|
|
|
@NSManaged var videoDuration: Double
|
|
|
|
|
|
|
|
@NSManaged var watchedAt: Date?
|
|
|
|
@NSManaged var stoppedAt: Double
|
|
|
|
|
|
|
|
var progress: Double {
|
|
|
|
guard videoDuration.isFinite, !videoDuration.isZero else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
let progress = (stoppedAt / videoDuration) * 100
|
2022-01-02 19:39:19 +00:00
|
|
|
|
|
|
|
if progress >= Double(watchedThreshold) {
|
|
|
|
return 100
|
|
|
|
}
|
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
return min(max(progress, 0), 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
var finished: Bool {
|
|
|
|
progress >= Double(watchedThreshold)
|
|
|
|
}
|
|
|
|
|
|
|
|
var watchedAtString: String? {
|
|
|
|
guard let watchedAt = watchedAt else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let formatter = RelativeDateTimeFormatter()
|
|
|
|
formatter.unitsStyle = .full
|
|
|
|
return formatter.localizedString(for: watchedAt, relativeTo: Date())
|
|
|
|
}
|
2022-05-29 15:50:54 +00:00
|
|
|
|
|
|
|
var timeToRestart: CMTime? {
|
|
|
|
finished ? nil : saveHistory ? .secondsInDefaultTimescale(stoppedAt) : nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var video: Video {
|
|
|
|
Video(
|
|
|
|
videoID: videoID, title: "", author: "",
|
|
|
|
length: 0, published: "", views: -1, channel: Channel(id: "", name: "")
|
|
|
|
)
|
|
|
|
}
|
2021-12-26 21:14:46 +00:00
|
|
|
}
|