diff options
| author | Fuwn <[email protected]> | 2025-08-28 15:39:59 -0700 |
|---|---|---|
| committer | Fuwn <[email protected]> | 2025-08-28 15:39:59 -0700 |
| commit | add606fb0555a9a73f624e15e5fd916a49469ab8 (patch) | |
| tree | 27ff8daf5e3fee1a97970ca9bc70f26ab690c570 /Sora/Data/ImageCacheManager.swift | |
| parent | feat: Development commit (diff) | |
| download | sora-testing-add606fb0555a9a73f624e15e5fd916a49469ab8.tar.xz sora-testing-add606fb0555a9a73f624e15e5fd916a49469ab8.zip | |
feat: Development commit
Diffstat (limited to 'Sora/Data/ImageCacheManager.swift')
| -rw-r--r-- | Sora/Data/ImageCacheManager.swift | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/Sora/Data/ImageCacheManager.swift b/Sora/Data/ImageCacheManager.swift index 8b886df..cd8b4e2 100644 --- a/Sora/Data/ImageCacheManager.swift +++ b/Sora/Data/ImageCacheManager.swift @@ -15,20 +15,30 @@ final class ImageCacheManager { ) private var cancellables = Set<AnyCancellable>() private let downloadQueue = OperationQueue() + private var preloadingURLs = Set<URL>() // MARK: - Initialisation private init() { downloadQueue.maxConcurrentOperationCount = 5 + downloadQueue.qualityOfService = .utility } // MARK: - Public Methods func preloadImages(_ urls: [URL]) { - for url in urls { + let newURLs = urls.filter { !preloadingURLs.contains($0) } + + for url in newURLs { + preloadingURLs.insert(url) + downloadQueue.addOperation { let cancellable = URLSession.shared.dataTaskPublisher(for: url) .map { CachedURLResponse(response: $0.response, data: $0.data) } .sink( - receiveCompletion: { _ in () }, + receiveCompletion: { _ in + DispatchQueue.main.async { [weak self] in + self?.preloadingURLs.remove(url) + } + }, receiveValue: { [weak self] cachedResponse in self?.cache.storeCachedResponse(cachedResponse, for: URLRequest(url: url)) } @@ -40,4 +50,14 @@ final class ImageCacheManager { } } } + + func clearCache() { + cache.removeAllCachedResponses() + cancellables.removeAll() + preloadingURLs.removeAll() + } + + func getCachedResponse(for url: URL) -> CachedURLResponse? { + cache.cachedResponse(for: URLRequest(url: url)) + } } |