2021-10-05 20:20:09 +00:00
|
|
|
import AVFoundation
|
2021-10-24 18:01:08 +00:00
|
|
|
import Defaults
|
2021-10-05 20:20:09 +00:00
|
|
|
import Foundation
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
struct PlayerQueueItem: Hashable, Identifiable, Defaults.Serializable {
|
|
|
|
static let bridge = PlayerQueueItemBridge()
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
var id = UUID()
|
2021-10-24 18:01:08 +00:00
|
|
|
var video: Video!
|
|
|
|
var videoID: Video.ID
|
2022-12-12 23:38:26 +00:00
|
|
|
var app: VideosApp?
|
|
|
|
var instanceURL: URL?
|
2021-10-22 20:49:31 +00:00
|
|
|
var playbackTime: CMTime?
|
|
|
|
var videoDuration: TimeInterval?
|
2021-10-05 20:20:09 +00:00
|
|
|
|
2021-12-26 21:14:46 +00:00
|
|
|
static func from(_ watch: Watch, video: Video? = nil) -> Self {
|
|
|
|
.init(
|
|
|
|
video,
|
|
|
|
videoID: watch.videoID,
|
2022-12-12 23:38:26 +00:00
|
|
|
app: watch.app,
|
|
|
|
instanceURL: watch.instanceURL,
|
2021-12-26 21:14:46 +00:00
|
|
|
playbackTime: CMTime.secondsInDefaultTimescale(watch.stoppedAt),
|
|
|
|
videoDuration: watch.videoDuration
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:38:26 +00:00
|
|
|
init(
|
2022-12-18 18:39:03 +00:00
|
|
|
_ video: Video? = .fixture,
|
2022-12-12 23:38:26 +00:00
|
|
|
videoID: Video.ID? = nil,
|
|
|
|
app: VideosApp? = nil,
|
|
|
|
instanceURL: URL? = nil,
|
|
|
|
playbackTime: CMTime? = nil,
|
|
|
|
videoDuration: TimeInterval? = nil
|
|
|
|
) {
|
2021-10-05 20:20:09 +00:00
|
|
|
self.video = video
|
2021-10-24 18:01:08 +00:00
|
|
|
self.videoID = videoID ?? video!.videoID
|
2022-12-12 23:38:26 +00:00
|
|
|
self.app = app
|
|
|
|
self.instanceURL = instanceURL
|
2021-10-22 20:49:31 +00:00
|
|
|
self.playbackTime = playbackTime
|
|
|
|
self.videoDuration = videoDuration
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 20:49:31 +00:00
|
|
|
var duration: TimeInterval {
|
2022-02-27 20:31:17 +00:00
|
|
|
videoDuration ?? video?.length ?? .zero
|
2021-10-22 20:49:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var shouldRestartPlaying: Bool {
|
2022-08-29 15:58:26 +00:00
|
|
|
guard Defaults[.watchedVideoPlayNowBehavior] == .continue else { return true }
|
|
|
|
|
2021-10-22 20:49:31 +00:00
|
|
|
guard let seconds = playbackTime?.seconds else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-12 23:07:23 +00:00
|
|
|
if duration <= 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-11-19 13:11:04 +00:00
|
|
|
return (seconds / duration) * 100 > Double(Defaults[.watchedThreshold])
|
2021-10-22 20:49:31 +00:00
|
|
|
}
|
|
|
|
|
2022-06-26 11:12:32 +00:00
|
|
|
var hasDetailsLoaded: Bool {
|
2022-11-10 17:11:28 +00:00
|
|
|
guard let video else { return false }
|
|
|
|
return !video.streams.isEmpty
|
2022-06-26 11:12:32 +00:00
|
|
|
}
|
|
|
|
|
2021-10-22 20:49:31 +00:00
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(id)
|
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
}
|