mirror of
https://github.com/yattee/yattee.git
synced 2024-12-23 05:53:41 +00:00
75 lines
1.8 KiB
Swift
75 lines
1.8 KiB
Swift
import Foundation
|
|
import SwiftyJSON
|
|
|
|
struct Playlist: Identifiable, Equatable, Hashable {
|
|
enum Visibility: String, CaseIterable, Identifiable {
|
|
case `public`, unlisted, `private`
|
|
|
|
var id: String {
|
|
rawValue
|
|
}
|
|
|
|
var name: String {
|
|
rawValue.capitalized.localized()
|
|
}
|
|
}
|
|
|
|
let id: String
|
|
var title: String
|
|
var visibility: Visibility
|
|
var editable = true
|
|
|
|
var updated: TimeInterval?
|
|
|
|
var videos = [Video]()
|
|
|
|
init(
|
|
id: String,
|
|
title: String,
|
|
visibility: Visibility,
|
|
editable: Bool = true,
|
|
updated: TimeInterval? = nil,
|
|
videos: [Video] = []
|
|
) {
|
|
self.id = id
|
|
self.title = title
|
|
self.visibility = visibility
|
|
self.editable = editable
|
|
self.updated = updated
|
|
self.videos = videos
|
|
}
|
|
|
|
var json: JSON {
|
|
return [
|
|
"id": id,
|
|
"title": title,
|
|
"visibility": visibility.rawValue,
|
|
"editable": editable ? "editable" : "",
|
|
"updated": updated ?? "",
|
|
"videos": videos.map(\.json).map(\.object)
|
|
]
|
|
}
|
|
|
|
static func from(_ json: JSON) -> Self {
|
|
return .init(
|
|
id: json["id"].stringValue,
|
|
title: json["title"].stringValue,
|
|
visibility: .init(rawValue: json["visibility"].stringValue) ?? .public,
|
|
updated: json["updated"].doubleValue,
|
|
videos: json["videos"].arrayValue.map { Video.from($0) }
|
|
)
|
|
}
|
|
|
|
static func == (lhs: Playlist, rhs: Playlist) -> Bool {
|
|
lhs.id == rhs.id && lhs.updated == rhs.updated
|
|
}
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
hasher.combine(id)
|
|
}
|
|
|
|
var channelPlaylist: ChannelPlaylist {
|
|
ChannelPlaylist(id: id, title: title, videos: videos, videosCount: videos.count)
|
|
}
|
|
}
|