2022-06-26 17:55:54 +05:30
|
|
|
import Defaults
|
2021-10-25 03:56:25 +05:30
|
|
|
import Foundation
|
|
|
|
|
|
|
|
final class ThumbnailsModel: ObservableObject {
|
2022-11-25 02:06:05 +05:30
|
|
|
static var shared = ThumbnailsModel()
|
|
|
|
|
2021-10-25 03:56:25 +05:30
|
|
|
@Published var unloadable = Set<URL>()
|
2024-08-30 20:31:33 +05:30
|
|
|
private var retryCounts = [URL: Int]()
|
|
|
|
private let maxRetries = 3
|
2024-08-31 02:44:41 +05:30
|
|
|
private let retryDelay: TimeInterval = 1.0
|
2021-10-25 03:56:25 +05:30
|
|
|
|
|
|
|
func insertUnloadable(_ url: URL) {
|
2024-08-30 20:31:33 +05:30
|
|
|
let retries = (retryCounts[url] ?? 0) + 1
|
|
|
|
|
|
|
|
if retries >= maxRetries {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.unloadable.insert(url)
|
|
|
|
self.retryCounts.removeValue(forKey: url)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.retryCounts[url] = retries
|
|
|
|
}
|
|
|
|
DispatchQueue.global().asyncAfter(deadline: .now() + retryDelay) {
|
|
|
|
DispatchQueue.main.async {
|
|
|
|
self.retryCounts[url] = retries
|
|
|
|
}
|
|
|
|
}
|
2022-12-17 03:07:30 +05:30
|
|
|
}
|
2021-10-25 03:56:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func isUnloadable(_ url: URL!) -> Bool {
|
|
|
|
guard !url.isNil else {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return unloadable.contains(url)
|
|
|
|
}
|
|
|
|
|
2024-08-19 20:05:29 +05:30
|
|
|
func best(_ video: Video) -> (url: URL?, quality: Thumbnail.Quality?) {
|
2022-06-26 17:55:54 +05:30
|
|
|
for quality in availableQualitites {
|
2021-11-06 02:23:43 +05:30
|
|
|
let url = video.thumbnailURL(quality: quality)
|
|
|
|
if !isUnloadable(url) {
|
2024-08-19 20:05:29 +05:30
|
|
|
return (url, quality)
|
2021-11-06 02:23:43 +05:30
|
|
|
}
|
2021-10-25 03:56:25 +05:30
|
|
|
}
|
|
|
|
|
2024-08-19 20:05:29 +05:30
|
|
|
return (nil, nil)
|
2021-10-25 03:56:25 +05:30
|
|
|
}
|
2022-06-26 17:55:54 +05:30
|
|
|
|
|
|
|
private var availableQualitites: [Thumbnail.Quality] {
|
|
|
|
switch Defaults[.thumbnailsQuality] {
|
|
|
|
case .highest:
|
2024-08-19 20:05:29 +05:30
|
|
|
return [.maxres, .high, .medium, .default]
|
|
|
|
case .high:
|
|
|
|
return [.high, .medium, .default]
|
2022-06-26 17:55:54 +05:30
|
|
|
case .medium:
|
|
|
|
return [.medium, .default]
|
|
|
|
case .low:
|
|
|
|
return [.default]
|
|
|
|
}
|
|
|
|
}
|
2021-10-25 03:56:25 +05:30
|
|
|
}
|