1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 06:40:32 +05:30
yattee/Model/Channel.swift

174 lines
4.6 KiB
Swift
Raw Normal View History

2021-06-28 20:32:13 +05:30
import AVFoundation
import Defaults
import Foundation
2021-08-26 03:42:59 +05:30
import SwiftyJSON
2021-06-28 20:32:13 +05:30
struct Channel: Identifiable, Hashable {
2022-12-04 17:31:05 +05:30
enum ContentType: String, Identifiable, CaseIterable {
2022-11-27 16:12:16 +05:30
case videos
case playlists
case livestreams
case shorts
case channels
case releases
case podcasts
2022-11-27 16:12:16 +05:30
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 16:12:16 +05:30
var id: String {
rawValue
}
2022-12-04 17:31:05 +05:30
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 17:31:05 +05:30
}
}
var alwaysAvailable: Bool {
self == .videos || self == .playlists
}
2022-11-27 16:12:16 +05:30
}
struct Tab: Identifiable, Hashable {
var contentType: ContentType
var data: String
var id: String {
contentType.id
}
}
2022-12-14 04:37:32 +05:30
var app: VideosApp
var instanceID: Instance.ID?
var instanceURL: URL?
2021-06-28 20:32:13 +05:30
var id: String
var name: String
2022-11-27 16:12:16 +05:30
var bannerURL: URL?
var thumbnailURL: URL?
2022-11-27 16:12:16 +05:30
var description = ""
2022-11-27 16:12:16 +05:30
var subscriptionsCount: Int?
var subscriptionsText: String?
var totalViews: Int?
2023-04-22 18:38:33 +05:30
// swiftlint:disable discouraged_optional_boolean
var verified: Bool?
// swiftlint:enable discouraged_optional_boolean
2022-11-27 16:12:16 +05:30
var videos = [Video]()
var tabs = [Tab]()
2021-12-18 01:31:05 +05:30
var detailsLoaded: Bool {
!subscriptionsString.isNil
}
var subscriptionsString: String? {
if let subscriptionsCount, subscriptionsCount > 0 {
return subscriptionsCount.formattedAsAbbreviation()
}
return subscriptionsText
}
2022-11-27 16:12:16 +05:30
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 20:32:13 +05:30
}
2022-11-27 16:12:16 +05:30
var contentItem: ContentItem {
ContentItem(channel: self)
}
2022-12-04 17:31:05 +05:30
func hasData(for contentType: ContentType) -> Bool {
2023-06-17 17:39:51 +05:30
tabs.contains { $0.contentType == contentType }
2022-12-04 17:31:05 +05:30
}
2022-12-10 05:53:13 +05:30
2022-12-14 04:37:32 +05:30
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-14 04:37:32 +05:30
}
2022-12-15 16:39:41 +05:30
var json: JSON {
[
"app": app.rawValue,
"id": id,
"name": name,
2022-12-16 17:01:14 +05:30
"bannerURL": bannerURL?.absoluteString as Any,
"thumbnailURL": thumbnailURL?.absoluteString as Any,
2022-12-17 18:54:09 +05:30
"description": description,
2022-12-16 17:01:14 +05:30
"subscriptionsCount": subscriptionsCount as Any,
"subscriptionsText": subscriptionsText as Any,
"totalViews": totalViews as Any,
"verified": verified as Any,
2022-12-15 16:39:41 +05:30
"videos": videos.map { $0.json.object }
]
}
2022-12-10 05:53:13 +05:30
static func from(_ json: JSON) -> Self {
.init(
2022-12-14 04:37:32 +05:30
app: VideosApp(rawValue: json["app"].stringValue) ?? .local,
2022-12-10 05:53:13 +05:30
id: json["id"].stringValue,
name: json["name"].stringValue,
2022-12-16 17:01:14 +05:30
bannerURL: json["bannerURL"].url,
2022-12-15 16:39:41 +05:30
thumbnailURL: json["thumbnailURL"].url,
2022-12-17 18:54:09 +05:30
description: json["description"].stringValue,
2022-12-16 17:01:14 +05:30
subscriptionsCount: json["subscriptionsCount"].int,
subscriptionsText: json["subscriptionsText"].string,
totalViews: json["totalViews"].int,
2022-12-15 16:39:41 +05:30
videos: json["videos"].arrayValue.map { Video.from($0) }
2022-12-10 05:53:13 +05:30
)
}
2021-06-28 20:32:13 +05:30
}