2021-12-27 02:44:46 +05:30
|
|
|
import CoreData
|
2022-05-29 21:20:54 +05:30
|
|
|
import CoreMedia
|
2021-12-27 02:44:46 +05:30
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
@objc(Watch)
|
|
|
|
final class Watch: NSManagedObject, Identifiable {
|
|
|
|
@Default(.watchedThreshold) private var watchedThreshold
|
2022-05-29 21:20:54 +05:30
|
|
|
@Default(.saveHistory) private var saveHistory
|
2023-05-23 21:30:33 +05:30
|
|
|
@Default(.showWatchingProgress) private var showWatchingProgress
|
2021-12-27 02:44:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
extension Watch {
|
|
|
|
@nonobjc class func fetchRequest() -> NSFetchRequest<Watch> {
|
|
|
|
NSFetchRequest<Watch>(entityName: "Watch")
|
|
|
|
}
|
|
|
|
|
2023-04-22 21:29:28 +05:30
|
|
|
@nonobjc class func markAsWatched(videoID: String, account: Account, duration: Double, watchedAt: Date? = nil, context: NSManagedObjectContext) {
|
2022-06-30 15:40:43 +05:30
|
|
|
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
|
2022-12-09 05:45:19 +05:30
|
|
|
watch?.appName = account.app?.rawValue
|
|
|
|
watch?.instanceURL = account.url
|
2022-06-30 15:40:43 +05:30
|
|
|
} else {
|
|
|
|
watch = results?.first
|
|
|
|
}
|
|
|
|
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let watch else { return }
|
2022-06-30 15:40:43 +05:30
|
|
|
|
|
|
|
watch.videoDuration = duration
|
|
|
|
watch.stoppedAt = duration
|
2023-04-22 21:29:28 +05:30
|
|
|
watch.watchedAt = watchedAt ?? .init()
|
2022-06-30 15:40:43 +05:30
|
|
|
|
|
|
|
try? context.save()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-27 02:44:46 +05:30
|
|
|
@NSManaged var videoID: String
|
|
|
|
@NSManaged var videoDuration: Double
|
|
|
|
|
|
|
|
@NSManaged var watchedAt: Date?
|
|
|
|
@NSManaged var stoppedAt: Double
|
|
|
|
|
2022-12-09 05:45:19 +05:30
|
|
|
@NSManaged var appName: String?
|
|
|
|
@NSManaged var instanceURL: URL?
|
|
|
|
|
2023-04-23 01:43:59 +05:30
|
|
|
var app: VideosApp? {
|
2022-12-09 05:45:19 +05:30
|
|
|
guard let appName else { return nil }
|
|
|
|
return .init(rawValue: appName)
|
|
|
|
}
|
|
|
|
|
2021-12-27 02:44:46 +05:30
|
|
|
var progress: Double {
|
|
|
|
guard videoDuration.isFinite, !videoDuration.isZero else {
|
2022-12-16 03:09:42 +05:30
|
|
|
return 100
|
2021-12-27 02:44:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
let progress = (stoppedAt / videoDuration) * 100
|
2022-01-03 01:09:19 +05:30
|
|
|
|
|
|
|
if progress >= Double(watchedThreshold) {
|
|
|
|
return 100
|
|
|
|
}
|
|
|
|
|
2021-12-27 02:44:46 +05:30
|
|
|
return min(max(progress, 0), 100)
|
|
|
|
}
|
|
|
|
|
|
|
|
var finished: Bool {
|
2022-12-13 05:08:26 +05:30
|
|
|
guard videoDuration.isFinite, !videoDuration.isZero else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return progress >= Double(watchedThreshold)
|
2021-12-27 02:44:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
var watchedAtString: String? {
|
2022-09-28 19:57:01 +05:30
|
|
|
guard let watchedAt else {
|
2021-12-27 02:44:46 +05:30
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
let formatter = RelativeDateTimeFormatter()
|
|
|
|
formatter.unitsStyle = .full
|
|
|
|
return formatter.localizedString(for: watchedAt, relativeTo: Date())
|
|
|
|
}
|
2022-05-29 21:20:54 +05:30
|
|
|
|
|
|
|
var timeToRestart: CMTime? {
|
|
|
|
finished ? nil : saveHistory ? .secondsInDefaultTimescale(stoppedAt) : nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var video: Video {
|
2022-12-10 01:03:01 +05:30
|
|
|
let url = URL(string: videoID)
|
|
|
|
|
2022-12-10 05:53:13 +05:30
|
|
|
if !Video.VideoID.isValid(videoID) {
|
2022-12-10 01:03:01 +05:30
|
|
|
if let url {
|
|
|
|
return .local(url)
|
|
|
|
}
|
2022-11-10 22:41:28 +05:30
|
|
|
}
|
|
|
|
|
2022-12-17 01:31:01 +05:30
|
|
|
return Video(app: app ?? AccountsModel.shared.current?.app ?? .local, instanceURL: instanceURL, videoID: videoID)
|
2022-05-29 21:20:54 +05:30
|
|
|
}
|
2023-05-23 21:30:33 +05:30
|
|
|
|
|
|
|
var isShowingProgress: Bool {
|
|
|
|
saveHistory && showWatchingProgress && (finished || progress > 0)
|
|
|
|
}
|
2021-12-27 02:44:46 +05:30
|
|
|
}
|