Skip to content
This repository was archived by the owner on Jun 4, 2025. It is now read-only.

Commit 888a9ed

Browse files
committed
Move the cache injection method
1 parent 08f45f0 commit 888a9ed

1 file changed

Lines changed: 9 additions & 16 deletions

File tree

Sources/WordPressUI/Extensions/UIImageView+Networking.swift

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public extension UIImageView {
7979
return
8080
}
8181

82-
if let cachedImage = Downloader.cache.getImage(forKey: url.absoluteString) {
82+
if let cachedImage = ImageCache.shared.getImage(forKey: url.absoluteString) {
8383
handleSuccess(cachedImage, url)
8484
return
8585
}
@@ -98,7 +98,7 @@ public extension UIImageView {
9898

9999
DispatchQueue.main.async {
100100
if response?.url == url {
101-
Downloader.cache.setImage(image, forKey: url.absoluteString)
101+
ImageCache.shared.setImage(image, forKey: url.absoluteString)
102102
handleSuccess(image, url)
103103
} else {
104104
failure?(ImageDownloadError.urlMismatch)
@@ -117,7 +117,7 @@ public extension UIImageView {
117117
/// and we need to prevent returning the (old) cached entry.
118118
///
119119
@objc func overrideImageCache(for url: URL, with image: UIImage) {
120-
Downloader.cache.setImage(image, forKey: url.absoluteString)
120+
ImageCache.shared.setImage(image, forKey: url.absoluteString)
121121

122122
// Remove all cached responses - removing an individual response does not work since iOS 7.
123123
// This feels hacky to do but what else can we do...
@@ -159,11 +159,6 @@ public extension UIImageView {
159159
/// Private helper structure
160160
///
161161
private struct Downloader {
162-
163-
/// Stores all of the previously downloaded images.
164-
///
165-
static var cache: ImageCaching = ImageCache()
166-
167162
/// Key used to associate the current URL.
168163
///
169164
static var urlKey = "urlKey"
@@ -172,26 +167,24 @@ public extension UIImageView {
172167
///
173168
static var taskKey = "downloadTaskKey"
174169
}
175-
176-
/// Changes the default cache used by the image dowloader.
177-
public static func setSharedCache(_ cache: ImageCaching) {
178-
Downloader.cache = cache
179-
}
180170
}
181171

182172
public protocol ImageCaching {
183173
func setImage(_ image: UIImage, forKey key: String)
184174
func getImage(forKey key: String) -> UIImage?
185175
}
186176

187-
private final class ImageCache: ImageCaching {
177+
public class ImageCache: ImageCaching {
188178
private let cache = NSCache<NSString, UIImage>()
189179

190-
func setImage(_ image: UIImage, forKey key: String) {
180+
/// Changes the default cache used by the image dowloader.
181+
public static var shared: ImageCaching = ImageCache()
182+
183+
public func setImage(_ image: UIImage, forKey key: String) {
191184
cache.setObject(image, forKey: key as NSString)
192185
}
193186

194-
func getImage(forKey key: String) -> UIImage? {
187+
public func getImage(forKey key: String) -> UIImage? {
195188
cache.object(forKey: key as NSString)
196189
}
197190
}

0 commit comments

Comments
 (0)