2022-06-26 12:25:54 +00:00
|
|
|
import Defaults
|
2021-10-24 22:26:25 +00:00
|
|
|
import Foundation
|
|
|
|
|
|
|
|
final class ThumbnailsModel: ObservableObject {
|
2022-11-24 20:36:05 +00:00
|
|
|
static var shared = ThumbnailsModel()
|
|
|
|
|
2021-10-24 22:26:25 +00:00
|
|
|
@Published var unloadable = Set<URL>()
|
|
|
|
|
|
|
|
func insertUnloadable(_ url: URL) {
|
2022-12-16 21:37:30 +00:00
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.unloadable.insert(url)
|
|
|
|
}
|
2021-10-24 22:26:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func isUnloadable(_ url: URL!) -> Bool {
|
|
|
|
guard !url.isNil else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return unloadable.contains(url)
|
|
|
|
}
|
|
|
|
|
2024-08-19 14:35:29 +00:00
|
|
|
func best(_ video: Video) -> (url: URL?, quality: Thumbnail.Quality?) {
|
2022-06-26 12:25:54 +00:00
|
|
|
for quality in availableQualitites {
|
2021-11-05 20:53:43 +00:00
|
|
|
let url = video.thumbnailURL(quality: quality)
|
|
|
|
if !isUnloadable(url) {
|
2024-08-19 14:35:29 +00:00
|
|
|
return (url, quality)
|
2021-11-05 20:53:43 +00:00
|
|
|
}
|
2021-10-24 22:26:25 +00:00
|
|
|
}
|
|
|
|
|
2024-08-19 14:35:29 +00:00
|
|
|
return (nil, nil)
|
2021-10-24 22:26:25 +00:00
|
|
|
}
|
2022-06-26 12:25:54 +00:00
|
|
|
|
|
|
|
private var availableQualitites: [Thumbnail.Quality] {
|
|
|
|
switch Defaults[.thumbnailsQuality] {
|
|
|
|
case .highest:
|
2024-08-19 14:35:29 +00:00
|
|
|
return [.maxres, .high, .medium, .default]
|
|
|
|
case .high:
|
|
|
|
return [.high, .medium, .default]
|
2022-06-26 12:25:54 +00:00
|
|
|
case .medium:
|
|
|
|
return [.medium, .default]
|
|
|
|
case .low:
|
|
|
|
return [.default]
|
|
|
|
}
|
|
|
|
}
|
2021-10-24 22:26:25 +00:00
|
|
|
}
|