1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-13 22:00:31 +05:30
yattee/Model/Player/PlayerQueueItemBridge.swift

80 lines
2.1 KiB
Swift
Raw Normal View History

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 19:57:01 +05:30
guard let value else {
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 22:41:28 +05:30
var localURL = ""
if let video = value.video, video.isLocal {
localURL = video.localStream?.localURL.absoluteString ?? ""
}
return [
2022-11-10 22:41:28 +05:30
"localURL": localURL,
2021-11-02 03:26:18 +05:30
"videoID": value.videoID,
"playbackTime": playbackTime,
"videoDuration": videoDuration
]
}
func deserialize(_ object: Serializable?) -> Value? {
2022-11-10 22:41:28 +05:30
guard let object else { return nil }
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-11-10 22:41:28 +05:30
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 }
return PlayerQueueItem(
videoID: videoID,
playbackTime: playbackTime,
videoDuration: videoDuration
)
}
}