From 0136de2172bbb7a5d4fa5affe12fe808ae77b255 Mon Sep 17 00:00:00 2001 From: kean Date: Tue, 24 Dec 2024 13:12:04 -0500 Subject: [PATCH] Remove AnimatedImageCache --- .../Utility/Media/MediaExternalExporter.swift | 2 - .../Media/MemoryCache+Extensions.swift | 2 - .../Media/AnimatedImageCache.swift | 99 ------------------- 3 files changed, 103 deletions(-) delete mode 100644 WordPress/Classes/ViewRelated/Media/AnimatedImageCache.swift diff --git a/WordPress/Classes/Utility/Media/MediaExternalExporter.swift b/WordPress/Classes/Utility/Media/MediaExternalExporter.swift index 07f9e5e681b4..25809dfccba3 100644 --- a/WordPress/Classes/Utility/Media/MediaExternalExporter.swift +++ b/WordPress/Classes/Utility/Media/MediaExternalExporter.swift @@ -46,8 +46,6 @@ class MediaExternalExporter: MediaExporter { return Progress.discreteCompletedProgress() } - /// Downloads an external GIF file, or uses one from the AnimatedImageCache. - /// private func downloadGif(from url: URL, onCompletion: @escaping OnMediaExport, onError: @escaping OnExportError) -> Progress { Task { do { diff --git a/WordPress/Classes/Utility/Media/MemoryCache+Extensions.swift b/WordPress/Classes/Utility/Media/MemoryCache+Extensions.swift index 48e208b35a4e..1cbada8b8a06 100644 --- a/WordPress/Classes/Utility/Media/MemoryCache+Extensions.swift +++ b/WordPress/Classes/Utility/Media/MemoryCache+Extensions.swift @@ -13,8 +13,6 @@ extension MemoryCache { UIImageView.af.sharedImageDownloader = AlamofireImage.ImageDownloader( imageCache: AlamofireImageCacheAdapter(cache: .shared) ) - - // WordPress.AnimatedImageCache uses WordPress.MemoryCache directly } } diff --git a/WordPress/Classes/ViewRelated/Media/AnimatedImageCache.swift b/WordPress/Classes/ViewRelated/Media/AnimatedImageCache.swift deleted file mode 100644 index 47d0f2b782f2..000000000000 --- a/WordPress/Classes/ViewRelated/Media/AnimatedImageCache.swift +++ /dev/null @@ -1,99 +0,0 @@ -import UIKit -import WordPressMedia - -/// AnimatedImageCache is an image + animated gif data cache used in -/// CachedAnimatedImageView. It should be accessed via the `shared` singleton. -/// -final class AnimatedImageCache { - - // MARK: Singleton - - static let shared: AnimatedImageCache = AnimatedImageCache() - - private init() {} - - // MARK: Private fields - - fileprivate lazy var session: URLSession = { - let sessionConfiguration = URLSessionConfiguration.default - let session = URLSession(configuration: sessionConfiguration) - return session - }() - - // MARK: Instance methods - - func cacheData(data: Data, url: URL?) { - guard let url else { return } - let key = url.absoluteString + Constants.keyDataSuffix - MemoryCache.shared.setData(data, forKey: key) - } - - func cachedData(url: URL?) -> Data? { - guard let url else { return nil } - let key = url.absoluteString + Constants.keyDataSuffix - return MemoryCache.shared.geData(forKey: key) - } - - func cacheStaticImage(url: URL?, image: UIImage?) { - guard let url, let image else { return } - let key = url.absoluteString + Constants.keyStaticImageSuffix - MemoryCache.shared.setImage(image, forKey: key) - } - - func cachedStaticImage(url: URL?) -> UIImage? { - guard let url else { return nil } - let key = url.absoluteString + Constants.keyStaticImageSuffix - return MemoryCache.shared.getImage(forKey: key) - } - - func animatedImage(_ urlRequest: URLRequest, - placeholderImage: UIImage?, - success: ((Data, UIImage?) -> Void)?, - failure: ((NSError?) -> Void)? ) -> URLSessionTask? { - - if let cachedImageData = cachedData(url: urlRequest.url) { - success?(cachedImageData, cachedStaticImage(url: urlRequest.url)) - return nil - } - - let task = session.dataTask(with: urlRequest, completionHandler: { [weak self] (data, response, error) in - //check if view is still here - guard let self else { - return - } - // check if there is an error - if let error { - let nsError = error as NSError - // task.cancel() triggers an error that we don't want to send to the error handler. - if nsError.code != NSURLErrorCancelled { - failure?(nsError) - } - return - } - // check if data is here and is animated gif - guard let data else { - failure?(nil) - return - } - - let staticImage = UIImage(data: data) - if let key = urlRequest.url { - self.cacheData(data: data, url: key) - self.cacheStaticImage(url: key, image: staticImage) - } - success?(data, staticImage) - }) - - task.resume() - return task - } -} - -// MARK: - Constants - -private extension AnimatedImageCache { - struct Constants { - static let keyDataSuffix = "_data" - static let keyStaticImageSuffix = "_static_image" - } -}