2021-06-26 09:39:35 +00:00
|
|
|
import Foundation
|
|
|
|
import SwiftyJSON
|
|
|
|
|
2021-06-28 15:27:53 +00:00
|
|
|
struct Playlist: Identifiable, Equatable, Hashable {
|
2021-07-22 12:43:13 +00:00
|
|
|
enum Visibility: String, CaseIterable, Identifiable {
|
|
|
|
case `public`, unlisted, `private`
|
|
|
|
|
|
|
|
var id: String {
|
|
|
|
rawValue
|
|
|
|
}
|
|
|
|
|
|
|
|
var name: String {
|
2022-09-26 15:30:59 +00:00
|
|
|
rawValue.capitalized.localized()
|
2021-07-22 12:43:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 09:39:35 +00:00
|
|
|
let id: String
|
|
|
|
var title: String
|
2021-07-22 12:43:13 +00:00
|
|
|
var visibility: Visibility
|
2022-09-12 15:23:20 +00:00
|
|
|
var editable = true
|
2021-06-26 09:39:35 +00:00
|
|
|
|
2022-04-10 15:07:10 +00:00
|
|
|
var updated: TimeInterval?
|
2021-07-09 14:53:53 +00:00
|
|
|
|
2021-06-26 09:39:35 +00:00
|
|
|
var videos = [Video]()
|
|
|
|
|
2022-12-11 17:04:39 +00:00
|
|
|
init(
|
|
|
|
id: String,
|
|
|
|
title: String,
|
|
|
|
visibility: Visibility,
|
|
|
|
editable: Bool = true,
|
|
|
|
updated: TimeInterval? = nil,
|
|
|
|
videos: [Video] = []
|
|
|
|
) {
|
2021-08-16 15:52:42 +00:00
|
|
|
self.id = id
|
|
|
|
self.title = title
|
|
|
|
self.visibility = visibility
|
2022-09-12 15:23:20 +00:00
|
|
|
self.editable = editable
|
2021-08-16 15:52:42 +00:00
|
|
|
self.updated = updated
|
2021-12-17 16:39:26 +00:00
|
|
|
self.videos = videos
|
2021-08-16 15:52:42 +00:00
|
|
|
}
|
|
|
|
|
2022-12-11 17:04:39 +00:00
|
|
|
var json: JSON {
|
2022-12-16 11:31:43 +00:00
|
|
|
[
|
2022-12-11 17:04:39 +00:00
|
|
|
"id": id,
|
|
|
|
"title": title,
|
|
|
|
"visibility": visibility.rawValue,
|
|
|
|
"editable": editable ? "editable" : "",
|
|
|
|
"updated": updated ?? "",
|
|
|
|
"videos": videos.map(\.json).map(\.object)
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
static func from(_ json: JSON) -> Self {
|
2022-12-16 11:31:43 +00:00
|
|
|
.init(
|
2022-12-11 17:04:39 +00:00
|
|
|
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) }
|
|
|
|
)
|
2021-06-26 09:39:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-17 12:09:51 +00:00
|
|
|
static func == (lhs: Self, rhs: Self) -> Bool {
|
2021-07-09 14:53:53 +00:00
|
|
|
lhs.id == rhs.id && lhs.updated == rhs.updated
|
2021-06-26 09:39:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(id)
|
|
|
|
}
|
2022-12-11 17:44:55 +00:00
|
|
|
|
|
|
|
var channelPlaylist: ChannelPlaylist {
|
|
|
|
ChannelPlaylist(id: id, title: title, videos: videos, videosCount: videos.count)
|
|
|
|
}
|
2021-06-26 09:39:35 +00:00
|
|
|
}
|