yattee/Model/Video.swift

230 lines
5.7 KiB
Swift
Raw Normal View History

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
import SwiftUI
2021-06-10 22:50:10 +00:00
import SwiftyJSON
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
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
var indexID: String?
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]()
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(
id: String? = nil,
videoID: String,
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,
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] = [],
related: [Video] = [],
2022-07-05 17:20:25 +00:00
chapters: [Chapter] = [],
captions: [Captions] = []
2021-07-22 12:43:13 +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
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? {
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
static func == (lhs: Video, rhs: Video) -> Bool {
let videoIDIsEqual = lhs.videoID == rhs.videoID
if !lhs.indexID.isNil, !rhs.indexID.isNil {
return videoIDIsEqual && lhs.indexID == rhs.indexID
}
return videoIDIsEqual
}
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
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
}