yattee/Model/Channel.swift

129 lines
3.0 KiB
Swift
Raw 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
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"
}
}
2022-11-27 10:42:16 +00:00
}
struct Tab: Identifiable, Hashable {
var contentType: ContentType
var data: String
var id: String {
contentType.id
}
}
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?
var verified: Bool? // swiftlint:disable discouraged_optional_boolean
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 {
guard contentType != .videos, contentType != .playlists else { return true }
return tabs.contains { $0.contentType == contentType }
2022-12-04 12:01:05 +00:00
}
2022-12-10 00:23:13 +00:00
var json: JSON {
[
"id": id,
"name": name,
"thumbnailURL": thumbnailURL?.absoluteString ?? ""
2022-12-10 00:23:13 +00:00
]
}
static func from(_ json: JSON) -> Self {
.init(
id: json["id"].stringValue,
name: json["name"].stringValue,
thumbnailURL: json["thumbnailURL"].url
2022-12-10 00:23:13 +00:00
)
}
2021-06-28 15:02:13 +00:00
}