summaryrefslogtreecommitdiff
path: root/Sora/Data/ImageCacheManager.swift
blob: 8a14198d12a16bcd06218ed1af27e459beb21849 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import Combine
@preconcurrency import SwiftUI

@MainActor
final class ImageCacheManager {
  // MARK: - Singleton
  static let shared = ImageCacheManager()

  // MARK: - Private Properties
  private let cache = URLCache(
    memoryCapacity: 100 * 1_024 * 1_024,  // 100 MB
    diskCapacity: 500 * 1_024 * 1_024,  // 500 MB
    directory: FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?
      .appendingPathComponent("SoraImageCache", isDirectory: true)
  )
  private var cancellables = Set<AnyCancellable>()
  private let downloadQueue = OperationQueue()
  private var preloadingURLs = Set<URL>()
  private var memoryWarningObserver: NSObjectProtocol?

  // MARK: - Initialisation
  private init() {
    downloadQueue.maxConcurrentOperationCount = 5
    downloadQueue.qualityOfService = .utility

    #if os(iOS)
      memoryWarningObserver = NotificationCenter.default.addObserver(
        forName: UIApplication.didReceiveMemoryWarningNotification,
        object: nil,
        queue: .main
      ) { [weak self] _ in
        Task { @MainActor in
          self?.handleMemoryPressure()
        }
      }
    #endif
  }

  // MARK: - Public Methods
  func preloadImages(_ urls: [URL]) {
    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
              DispatchQueue.main.async { [weak self] in
                self?.preloadingURLs.remove(url)
              }
            },
            receiveValue: { [weak self] cachedResponse in
              self?.cache.storeCachedResponse(cachedResponse, for: URLRequest(url: url))
            }
          )

        DispatchQueue.main.async { [weak self] in
          self?.cancellables.insert(cancellable)
        }
      }
    }
  }

  func clearCache() {
    cache.removeAllCachedResponses()
    cancellables.removeAll()
    preloadingURLs.removeAll()
  }

  func getCachedResponse(for url: URL) -> CachedURLResponse? {
    cache.cachedResponse(for: URLRequest(url: url))
  }

  private func handleMemoryPressure() {
    cache.removeAllCachedResponses()
    downloadQueue.cancelAllOperations()
    cancellables.removeAll()
    preloadingURLs.removeAll()
  }

  deinit {
    if let observer = memoryWarningObserver {
      NotificationCenter.default.removeObserver(observer)
    }
  }
}