yattee/Model/Video.swift

137 lines
3.3 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 {
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
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)
)
}
2021-06-10 22:50:10 +00:00
}