2021-06-10 22:50:10 +00:00
|
|
|
import Alamofire
|
2021-06-14 18:05:02 +00:00
|
|
|
import AVKit
|
2021-06-10 22:50:10 +00:00
|
|
|
import Foundation
|
2021-12-26 21:14:46 +00:00
|
|
|
import SwiftUI
|
2021-06-10 22:50:10 +00:00
|
|
|
import SwiftyJSON
|
|
|
|
|
2021-10-05 20:20:09 +00:00
|
|
|
struct Video: Identifiable, Equatable, Hashable {
|
2022-11-10 17:11:28 +00:00
|
|
|
enum VideoID {
|
|
|
|
static func isValid(_ id: Video.ID) -> Bool {
|
|
|
|
id.count == 11
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-10 22:50:10 +00:00
|
|
|
let id: String
|
2021-10-05 20:20:09 +00:00
|
|
|
let videoID: String
|
2021-06-10 22:50:10 +00:00
|
|
|
var title: String
|
2021-07-07 22:39:18 +00:00
|
|
|
var thumbnails: [Thumbnail]
|
2021-06-10 22:50:10 +00:00
|
|
|
var author: String
|
2021-06-11 00:05:59 +00:00
|
|
|
var length: TimeInterval
|
|
|
|
var published: String
|
|
|
|
var views: Int
|
2021-10-20 22:21:50 +00:00
|
|
|
var description: String?
|
|
|
|
var genre: String?
|
2021-06-11 21:11:59 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
// index used when in the Playlist
|
2022-05-21 22:29:51 +00:00
|
|
|
var indexID: String?
|
2021-07-09 14:53:53 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
var live: Bool
|
|
|
|
var upcoming: Bool
|
|
|
|
|
2021-06-14 18:05:02 +00:00
|
|
|
var streams = [Stream]()
|
2021-07-22 12:43:13 +00:00
|
|
|
|
2021-08-22 19:13:33 +00:00
|
|
|
var publishedAt: Date?
|
|
|
|
var likes: Int?
|
|
|
|
var dislikes: Int?
|
|
|
|
var keywords = [String]()
|
|
|
|
|
2021-08-25 22:12:59 +00:00
|
|
|
var channel: Channel
|
|
|
|
|
2021-11-02 23:02:02 +00:00
|
|
|
var related = [Video]()
|
2022-06-18 12:39:49 +00:00
|
|
|
var chapters = [Chapter]()
|
2021-11-02 23:02:02 +00:00
|
|
|
|
2022-07-05 17:20:25 +00:00
|
|
|
var captions = [Captions]()
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
init(
|
2021-10-05 20:20:09 +00:00
|
|
|
id: String? = nil,
|
|
|
|
videoID: String,
|
2022-08-19 21:55:02 +00:00
|
|
|
title: String = "",
|
|
|
|
author: String = "",
|
|
|
|
length: TimeInterval = .zero,
|
|
|
|
published: String = "",
|
|
|
|
views: Int = 0,
|
2021-10-20 22:21:50 +00:00
|
|
|
description: String? = nil,
|
|
|
|
genre: String? = nil,
|
2022-08-19 21:55:02 +00:00
|
|
|
channel: Channel = .init(id: "", name: ""),
|
2021-07-22 12:43:13 +00:00
|
|
|
thumbnails: [Thumbnail] = [],
|
|
|
|
indexID: String? = nil,
|
|
|
|
live: Bool = false,
|
2021-08-22 19:13:33 +00:00
|
|
|
upcoming: Bool = false,
|
|
|
|
publishedAt: Date? = nil,
|
|
|
|
likes: Int? = nil,
|
|
|
|
dislikes: Int? = nil,
|
2021-10-20 22:21:50 +00:00
|
|
|
keywords: [String] = [],
|
2021-11-02 23:02:02 +00:00
|
|
|
streams: [Stream] = [],
|
2022-06-18 12:39:49 +00:00
|
|
|
related: [Video] = [],
|
2022-07-05 17:20:25 +00:00
|
|
|
chapters: [Chapter] = [],
|
|
|
|
captions: [Captions] = []
|
2021-07-22 12:43:13 +00:00
|
|
|
) {
|
2021-10-05 20:20:09 +00:00
|
|
|
self.id = id ?? UUID().uuidString
|
|
|
|
self.videoID = videoID
|
2021-07-22 12:43:13 +00:00
|
|
|
self.title = title
|
|
|
|
self.author = author
|
|
|
|
self.length = length
|
|
|
|
self.published = published
|
|
|
|
self.views = views
|
|
|
|
self.description = description
|
|
|
|
self.genre = genre
|
2021-08-25 22:12:59 +00:00
|
|
|
self.channel = channel
|
2021-07-22 12:43:13 +00:00
|
|
|
self.thumbnails = thumbnails
|
|
|
|
self.indexID = indexID
|
|
|
|
self.live = live
|
|
|
|
self.upcoming = upcoming
|
2021-08-22 19:13:33 +00:00
|
|
|
self.publishedAt = publishedAt
|
|
|
|
self.likes = likes
|
|
|
|
self.dislikes = dislikes
|
|
|
|
self.keywords = keywords
|
2021-10-20 22:21:50 +00:00
|
|
|
self.streams = streams
|
2021-11-02 23:02:02 +00:00
|
|
|
self.related = related
|
2022-06-18 12:39:49 +00:00
|
|
|
self.chapters = chapters
|
2022-07-05 17:20:25 +00:00
|
|
|
self.captions = captions
|
2021-07-22 12:43:13 +00:00
|
|
|
}
|
2021-06-10 22:50:10 +00:00
|
|
|
|
2022-11-10 17:11:28 +00:00
|
|
|
static func local(_ url: URL) -> Video {
|
|
|
|
Video(
|
|
|
|
videoID: url.absoluteString,
|
|
|
|
streams: [.init(localURL: url)]
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var isLocal: Bool {
|
|
|
|
!VideoID.isValid(videoID)
|
|
|
|
}
|
|
|
|
|
|
|
|
var displayTitle: String {
|
|
|
|
if isLocal {
|
|
|
|
return localStreamFileName ?? localStream?.description ?? title
|
|
|
|
}
|
|
|
|
|
|
|
|
return title
|
|
|
|
}
|
|
|
|
|
|
|
|
var displayAuthor: String {
|
|
|
|
if isLocal, localStreamIsRemoteURL {
|
|
|
|
return remoteUrlHost ?? "Unknown"
|
|
|
|
}
|
|
|
|
|
|
|
|
return author
|
|
|
|
}
|
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
var publishedDate: String? {
|
|
|
|
(published.isEmpty || published == "0 seconds ago") ? nil : published
|
|
|
|
}
|
|
|
|
|
2021-08-22 19:13:33 +00:00
|
|
|
var viewsCount: String? {
|
2021-08-31 21:17:50 +00:00
|
|
|
views != 0 ? views.formattedAsAbbreviation() : nil
|
2021-08-22 19:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var likesCount: String? {
|
2021-12-19 16:15:27 +00:00
|
|
|
guard (likes ?? 0) > 0 else {
|
2021-11-11 21:07:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return likes?.formattedAsAbbreviation()
|
2021-08-22 19:13:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var dislikesCount: String? {
|
2021-12-19 16:15:27 +00:00
|
|
|
guard (dislikes ?? 0) > 0 else {
|
2021-11-11 21:07:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return dislikes?.formattedAsAbbreviation()
|
2021-06-11 00:05:59 +00:00
|
|
|
}
|
2021-06-14 18:05:02 +00:00
|
|
|
|
2021-07-22 12:43:13 +00:00
|
|
|
func thumbnailURL(quality: Thumbnail.Quality) -> URL? {
|
2021-10-24 21:36:24 +00:00
|
|
|
thumbnails.first { $0.quality == quality }?.url
|
2021-07-07 22:39:18 +00:00
|
|
|
}
|
2021-06-14 18:05:02 +00:00
|
|
|
|
2021-07-31 22:10:56 +00:00
|
|
|
static func == (lhs: Video, rhs: Video) -> Bool {
|
2021-12-26 21:14:46 +00:00
|
|
|
let videoIDIsEqual = lhs.videoID == rhs.videoID
|
|
|
|
|
|
|
|
if !lhs.indexID.isNil, !rhs.indexID.isNil {
|
|
|
|
return videoIDIsEqual && lhs.indexID == rhs.indexID
|
|
|
|
}
|
|
|
|
|
|
|
|
return videoIDIsEqual
|
2021-07-31 22:10:56 +00:00
|
|
|
}
|
2021-10-05 20:20:09 +00:00
|
|
|
|
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
hasher.combine(id)
|
|
|
|
}
|
2021-12-26 21:14:46 +00:00
|
|
|
|
|
|
|
var watchFetchRequest: FetchRequest<Watch> {
|
|
|
|
FetchRequest<Watch>(
|
|
|
|
entity: Watch.entity(),
|
|
|
|
sortDescriptors: [],
|
|
|
|
predicate: NSPredicate(format: "videoID = %@", videoID)
|
|
|
|
)
|
|
|
|
}
|
2022-11-10 17:11:28 +00:00
|
|
|
|
|
|
|
var localStream: Stream? {
|
|
|
|
guard isLocal else { return nil }
|
|
|
|
return streams.first
|
|
|
|
}
|
|
|
|
|
2022-11-12 23:01:04 +00:00
|
|
|
var localStreamImageSystemName: String {
|
|
|
|
guard localStream != nil else { return "" }
|
|
|
|
|
|
|
|
if localStreamIsDirectory {
|
|
|
|
return "folder"
|
|
|
|
}
|
|
|
|
if localStreamIsFile {
|
|
|
|
return "doc"
|
|
|
|
}
|
|
|
|
|
|
|
|
return "globe"
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:11:28 +00:00
|
|
|
var localStreamIsFile: Bool {
|
|
|
|
guard let localStream else { return false }
|
|
|
|
return localStream.localURL.isFileURL
|
|
|
|
}
|
|
|
|
|
|
|
|
var localStreamIsRemoteURL: Bool {
|
|
|
|
guard let localStream else { return false }
|
|
|
|
return !localStream.localURL.isFileURL
|
|
|
|
}
|
|
|
|
|
2022-11-12 23:01:04 +00:00
|
|
|
var localStreamIsDirectory: Bool {
|
|
|
|
guard let localStream else { return false }
|
|
|
|
#if os(iOS)
|
|
|
|
return DocumentsModel.shared.isDirectory(localStream.localURL)
|
|
|
|
#else
|
|
|
|
return false
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-11-10 17:11:28 +00:00
|
|
|
var remoteUrlHost: String? {
|
|
|
|
localStreamURLComponents?.host
|
|
|
|
}
|
|
|
|
|
|
|
|
var localStreamFileName: String? {
|
|
|
|
guard let path = localStream?.localURL?.lastPathComponent else { return nil }
|
|
|
|
|
|
|
|
if let localStreamFileExtension {
|
|
|
|
return String(path.dropLast(localStreamFileExtension.count + 1))
|
|
|
|
}
|
|
|
|
return String(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
var localStreamFileExtension: String? {
|
|
|
|
guard let path = localStreamURLComponents?.path else { return nil }
|
|
|
|
return path.contains(".") ? path.components(separatedBy: ".").last?.uppercased() : nil
|
|
|
|
}
|
|
|
|
|
|
|
|
private var localStreamURLComponents: URLComponents? {
|
|
|
|
guard let localStream else { return nil }
|
|
|
|
return URLComponents(url: localStream.localURL, resolvingAgainstBaseURL: false)
|
|
|
|
}
|
2021-06-10 22:50:10 +00:00
|
|
|
}
|