2021-06-28 15:02:13 +00:00
|
|
|
import AVFoundation
|
|
|
|
import Defaults
|
|
|
|
import Foundation
|
2021-08-25 22:12:59 +00:00
|
|
|
import SwiftyJSON
|
2021-06-28 15:02:13 +00:00
|
|
|
|
2021-08-31 21:17:50 +00:00
|
|
|
struct Channel: Identifiable, Hashable {
|
2022-12-04 12:01:05 +00:00
|
|
|
enum ContentType: String, Identifiable, CaseIterable {
|
2022-11-27 10:42:16 +00:00
|
|
|
case videos
|
|
|
|
case playlists
|
|
|
|
case livestreams
|
|
|
|
case shorts
|
|
|
|
case channels
|
|
|
|
|
2023-02-28 20:03:02 +00:00
|
|
|
static func from(_ name: String) -> Self? {
|
|
|
|
let rawValueMatch = allCases.first { $0.rawValue == name }
|
|
|
|
guard rawValueMatch.isNil else { return rawValueMatch! }
|
|
|
|
|
|
|
|
if name == "streams" { return .livestreams }
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-27 10:42:16 +00:00
|
|
|
var id: String {
|
|
|
|
rawValue
|
|
|
|
}
|
|
|
|
|
2022-12-04 12:01:05 +00:00
|
|
|
var description: String {
|
|
|
|
switch self {
|
|
|
|
case .livestreams:
|
|
|
|
return "Live Streams".localized()
|
|
|
|
default:
|
|
|
|
return rawValue.capitalized.localized()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-27 10:42:16 +00:00
|
|
|
var contentItemType: ContentItem.ContentType {
|
|
|
|
switch self {
|
|
|
|
case .videos:
|
|
|
|
return .video
|
|
|
|
case .playlists:
|
|
|
|
return .playlist
|
|
|
|
case .livestreams:
|
|
|
|
return .video
|
|
|
|
case .shorts:
|
|
|
|
return .video
|
|
|
|
case .channels:
|
|
|
|
return .channel
|
|
|
|
}
|
|
|
|
}
|
2022-12-04 12:01:05 +00:00
|
|
|
|
|
|
|
var systemImage: String {
|
|
|
|
switch self {
|
|
|
|
case .videos:
|
|
|
|
return "video"
|
|
|
|
case .playlists:
|
|
|
|
return "list.and.film"
|
|
|
|
case .livestreams:
|
|
|
|
return "dot.radiowaves.left.and.right"
|
|
|
|
case .shorts:
|
|
|
|
return "1.square"
|
|
|
|
case .channels:
|
|
|
|
return "person.3"
|
|
|
|
}
|
|
|
|
}
|
2023-02-28 21:13:39 +00:00
|
|
|
|
|
|
|
var alwaysAvailable: Bool {
|
|
|
|
self == .videos || self == .playlists
|
|
|
|
}
|
2022-11-27 10:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct Tab: Identifiable, Hashable {
|
|
|
|
var contentType: ContentType
|
|
|
|
var data: String
|
|
|
|
|
|
|
|
var id: String {
|
|
|
|
contentType.id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 23:07:32 +00:00
|
|
|
var app: VideosApp
|
|
|
|
var instanceID: Instance.ID?
|
|
|
|
var instanceURL: URL?
|
|
|
|
|
2021-06-28 15:02:13 +00:00
|
|
|
var id: String
|
|
|
|
var name: String
|
2022-11-27 10:42:16 +00:00
|
|
|
var bannerURL: URL?
|
2021-10-21 23:29:10 +00:00
|
|
|
var thumbnailURL: URL?
|
2022-11-27 10:42:16 +00:00
|
|
|
var description = ""
|
2021-08-31 21:17:50 +00:00
|
|
|
|
2022-11-27 10:42:16 +00:00
|
|
|
var subscriptionsCount: Int?
|
|
|
|
var subscriptionsText: String?
|
|
|
|
|
|
|
|
var totalViews: Int?
|
|
|
|
var verified: Bool? // swiftlint:disable discouraged_optional_boolean
|
|
|
|
|
|
|
|
var videos = [Video]()
|
|
|
|
var tabs = [Tab]()
|
2021-08-31 21:17:50 +00:00
|
|
|
|
2021-12-17 20:01:05 +00:00
|
|
|
var detailsLoaded: Bool {
|
|
|
|
!subscriptionsString.isNil
|
|
|
|
}
|
|
|
|
|
2021-08-31 21:17:50 +00:00
|
|
|
var subscriptionsString: String? {
|
2022-12-04 18:11:19 +00:00
|
|
|
if let subscriptionsCount, subscriptionsCount > 0 {
|
|
|
|
return subscriptionsCount.formattedAsAbbreviation()
|
2021-08-31 21:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return subscriptionsText
|
|
|
|
}
|
|
|
|
|
2022-11-27 10:42:16 +00:00
|
|
|
var totalViewsString: String? {
|
|
|
|
guard let totalViews, totalViews > 0 else { return nil }
|
|
|
|
|
|
|
|
return totalViews.formattedAsAbbreviation()
|
|
|
|
}
|
|
|
|
|
2021-08-31 21:17:50 +00:00
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(id)
|
2021-06-28 15:02:13 +00:00
|
|
|
}
|
2022-11-27 10:42:16 +00:00
|
|
|
|
|
|
|
var contentItem: ContentItem {
|
|
|
|
ContentItem(channel: self)
|
|
|
|
}
|
2022-12-04 12:01:05 +00:00
|
|
|
|
|
|
|
func hasData(for contentType: ContentType) -> Bool {
|
2022-12-04 18:11:19 +00:00
|
|
|
return tabs.contains { $0.contentType == contentType }
|
2022-12-04 12:01:05 +00:00
|
|
|
}
|
2022-12-10 00:23:13 +00:00
|
|
|
|
2022-12-13 23:07:32 +00:00
|
|
|
var cacheKey: String {
|
|
|
|
switch app {
|
|
|
|
case .local:
|
|
|
|
return id
|
|
|
|
case .invidious:
|
|
|
|
return "youtube-\(id)"
|
|
|
|
case .piped:
|
|
|
|
return "youtube-\(id)"
|
|
|
|
case .peerTube:
|
|
|
|
return "peertube-\(instanceURL?.absoluteString ?? "unknown-instance")-\(id)"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var hasExtendedDetails: Bool {
|
|
|
|
thumbnailURL != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var thumbnailURLOrCached: URL? {
|
2023-02-28 20:03:02 +00:00
|
|
|
thumbnailURL ?? ChannelsCacheModel.shared.retrieve(cacheKey)?.channel?.thumbnailURL
|
2022-12-13 23:07:32 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 11:09:41 +00:00
|
|
|
var json: JSON {
|
|
|
|
[
|
|
|
|
"app": app.rawValue,
|
|
|
|
"id": id,
|
|
|
|
"name": name,
|
2022-12-16 11:31:14 +00:00
|
|
|
"bannerURL": bannerURL?.absoluteString as Any,
|
|
|
|
"thumbnailURL": thumbnailURL?.absoluteString as Any,
|
2022-12-17 13:24:09 +00:00
|
|
|
"description": description,
|
2022-12-16 11:31:14 +00:00
|
|
|
"subscriptionsCount": subscriptionsCount as Any,
|
|
|
|
"subscriptionsText": subscriptionsText as Any,
|
|
|
|
"totalViews": totalViews as Any,
|
|
|
|
"verified": verified as Any,
|
2022-12-15 11:09:41 +00:00
|
|
|
"videos": videos.map { $0.json.object }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
2022-12-10 00:23:13 +00:00
|
|
|
static func from(_ json: JSON) -> Self {
|
|
|
|
.init(
|
2022-12-13 23:07:32 +00:00
|
|
|
app: VideosApp(rawValue: json["app"].stringValue) ?? .local,
|
2022-12-10 00:23:13 +00:00
|
|
|
id: json["id"].stringValue,
|
2022-12-11 11:38:57 +00:00
|
|
|
name: json["name"].stringValue,
|
2022-12-16 11:31:14 +00:00
|
|
|
bannerURL: json["bannerURL"].url,
|
2022-12-15 11:09:41 +00:00
|
|
|
thumbnailURL: json["thumbnailURL"].url,
|
2022-12-17 13:24:09 +00:00
|
|
|
description: json["description"].stringValue,
|
2022-12-16 11:31:14 +00:00
|
|
|
subscriptionsCount: json["subscriptionsCount"].int,
|
|
|
|
subscriptionsText: json["subscriptionsText"].string,
|
|
|
|
totalViews: json["totalViews"].int,
|
2022-12-15 11:09:41 +00:00
|
|
|
videos: json["videos"].arrayValue.map { Video.from($0) }
|
2022-12-10 00:23:13 +00:00
|
|
|
)
|
|
|
|
}
|
2021-06-28 15:02:13 +00:00
|
|
|
}
|