mirror of
https://github.com/yattee/yattee.git
synced 2025-08-06 18:54:11 +00:00
Persistence for queue, history and last played
This commit is contained in:
67
Model/Player/PlayerQueueItemBridge.swift
Normal file
67
Model/Player/PlayerQueueItemBridge.swift
Normal file
@@ -0,0 +1,67 @@
|
||||
import CoreMedia
|
||||
import Defaults
|
||||
import Foundation
|
||||
|
||||
struct PlayerQueueItemBridge: Defaults.Bridge {
|
||||
typealias Value = PlayerQueueItem
|
||||
typealias Serializable = [String: String]
|
||||
|
||||
func serialize(_ value: Value?) -> Serializable? {
|
||||
guard let value = value else {
|
||||
return nil
|
||||
}
|
||||
|
||||
let videoID = value.videoID.isEmpty ? value.video!.videoID : value.videoID
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
"videoID": videoID,
|
||||
"playbackTime": playbackTime,
|
||||
"videoDuration": videoDuration
|
||||
]
|
||||
}
|
||||
|
||||
func deserialize(_ object: Serializable?) -> Value? {
|
||||
guard
|
||||
let object = object,
|
||||
let videoID = object["videoID"]
|
||||
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)
|
||||
}
|
||||
|
||||
return PlayerQueueItem(
|
||||
videoID: videoID,
|
||||
playbackTime: playbackTime,
|
||||
videoDuration: videoDuration
|
||||
)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user