2021-10-24 18:01:08 +00:00
|
|
|
import CoreMedia
|
|
|
|
import Defaults
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
struct PlayerQueueItemBridge: Defaults.Bridge {
|
|
|
|
typealias Value = PlayerQueueItem
|
|
|
|
typealias Serializable = [String: String]
|
|
|
|
|
|
|
|
func serialize(_ value: Value?) -> Serializable? {
|
2022-09-28 14:27:01 +00:00
|
|
|
guard let value else {
|
2021-10-24 18:01:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var playbackTime = ""
|
|
|
|
if let time = value.playbackTime {
|
|
|
|
if time.seconds.isFinite {
|
|
|
|
playbackTime = String(time.seconds)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var videoDuration = ""
|
|
|
|
if let duration = value.videoDuration {
|
|
|
|
if duration.isFinite {
|
|
|
|
videoDuration = String(duration)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:11:28 +00:00
|
|
|
var localURL = ""
|
|
|
|
if let video = value.video, video.isLocal {
|
|
|
|
localURL = video.localStream?.localURL.absoluteString ?? ""
|
|
|
|
}
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
return [
|
2022-11-10 17:11:28 +00:00
|
|
|
"localURL": localURL,
|
2021-11-01 21:56:18 +00:00
|
|
|
"videoID": value.videoID,
|
2022-12-12 23:38:26 +00:00
|
|
|
"app": (value.app ?? value.video.app)?.rawValue ?? "",
|
|
|
|
"instanceURL": (value.instanceURL ?? value.video.instanceURL)?.absoluteString ?? "",
|
2021-10-24 18:01:08 +00:00
|
|
|
"playbackTime": playbackTime,
|
|
|
|
"videoDuration": videoDuration
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
func deserialize(_ object: Serializable?) -> Value? {
|
2022-11-10 17:11:28 +00:00
|
|
|
guard let object else { return nil }
|
2021-10-24 18:01:08 +00:00
|
|
|
|
2022-12-12 23:38:26 +00:00
|
|
|
var app: VideosApp?
|
|
|
|
var instanceURL: URL?
|
2021-10-24 18:01:08 +00:00
|
|
|
var playbackTime: CMTime?
|
|
|
|
var videoDuration: TimeInterval?
|
|
|
|
|
|
|
|
if let time = object["playbackTime"],
|
|
|
|
!time.isEmpty,
|
|
|
|
let seconds = TimeInterval(time)
|
|
|
|
{
|
|
|
|
playbackTime = .secondsInDefaultTimescale(seconds)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let duration = object["videoDuration"],
|
|
|
|
!duration.isEmpty
|
|
|
|
{
|
|
|
|
videoDuration = TimeInterval(duration)
|
|
|
|
}
|
|
|
|
|
2022-12-12 23:38:26 +00:00
|
|
|
if let appString = object["app"],
|
|
|
|
!appString.isEmpty
|
|
|
|
{
|
|
|
|
app = VideosApp(rawValue: appString)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let url = object["instanceURL"]?.url {
|
|
|
|
instanceURL = url
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:11:28 +00:00
|
|
|
if let localUrlString = object["localURL"],
|
|
|
|
!localUrlString.isEmpty,
|
|
|
|
let localURL = URL(string: localUrlString)
|
|
|
|
{
|
|
|
|
return PlayerQueueItem(
|
|
|
|
.local(localURL),
|
|
|
|
playbackTime: playbackTime,
|
|
|
|
videoDuration: videoDuration
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let videoID = object["videoID"] else { return nil }
|
|
|
|
|
2021-10-24 18:01:08 +00:00
|
|
|
return PlayerQueueItem(
|
2022-12-12 23:38:26 +00:00
|
|
|
.init(app: app ?? .local, instanceURL: instanceURL, videoID: videoID),
|
2021-10-24 18:01:08 +00:00
|
|
|
videoID: videoID,
|
2022-12-12 23:38:26 +00:00
|
|
|
app: app,
|
|
|
|
instanceURL: instanceURL,
|
2021-10-24 18:01:08 +00:00
|
|
|
playbackTime: playbackTime,
|
|
|
|
videoDuration: videoDuration
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|