yattee/Model/Channel.swift

174 lines
4.5 KiB
Swift
Raw Permalink Normal View History

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
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
case releases
case podcasts
2022-11-27 10:42:16 +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()
}
}
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"
case .releases:
return "square.stack"
case .podcasts:
return "radio"
2022-12-04 12:01:05 +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?
var thumbnailURL: URL?
2022-11-27 10:42:16 +00:00
var description = ""
2022-11-27 10:42:16 +00:00
var subscriptionsCount: Int?
var subscriptionsText: String?
var totalViews: Int?
2023-04-22 13:08:33 +00:00
// swiftlint:disable discouraged_optional_boolean
var verified: Bool?
// swiftlint:enable discouraged_optional_boolean
2022-11-27 10:42:16 +00:00
var videos = [Video]()
var tabs = [Tab]()
2021-12-17 20:01:05 +00:00
var detailsLoaded: Bool {
!subscriptionsString.isNil
}
var subscriptionsString: String? {
if let subscriptionsCount, subscriptionsCount > 0 {
return subscriptionsCount.formattedAsAbbreviation()
}
return subscriptionsText
}
2022-11-27 10:42:16 +00:00
var totalViewsString: String? {
guard let totalViews, totalViews > 0 else { return nil }
return totalViews.formattedAsAbbreviation()
}
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 {
2023-06-17 12:09:51 +00:00
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? {
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,
"videos": videos.map(\.json.object)
2022-12-15 11:09:41 +00:00
]
}
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,
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
}