@@ -10,17 +10,25 @@ import Combine
1010import Foundation
1111
1212public final class RemoteImageService : NSObject , ObservableObject {
13+ public typealias RemoteImageCacheKeyProvider = ( RemoteImageType ) -> AnyObject
14+
1315 private let dependencies : RemoteImageServiceDependenciesProtocol
1416 private var cancellable : AnyCancellable ?
15-
17+
1618 @Published var state : RemoteImageState = . loading
17-
18- public static let cache = NSCache < NSObject , PlatformSpecificImageType > ( )
19-
19+
20+ public static var cache : RemoteImageCache = DefaultRemoteImageCache ( )
21+ public static var cacheKeyProvider : RemoteImageCacheKeyProvider = { remoteImageType in
22+ switch remoteImageType {
23+ case . phAsset( let localIdentifier) : return localIdentifier as NSString
24+ case . url( let url) : return url as NSURL
25+ }
26+ }
27+
2028 init ( dependencies: RemoteImageServiceDependenciesProtocol ) {
2129 self . dependencies = dependencies
2230 }
23-
31+
2432 func fetchImage( ofType type: RemoteImageType ) {
2533 switch type {
2634 case . url( let url) :
@@ -34,15 +42,16 @@ public final class RemoteImageService: NSObject, ObservableObject {
3442extension RemoteImageService {
3543 private func fetchImage( atURL url: URL ) {
3644 cancellable? . cancel ( )
37-
38- if let image = RemoteImageService . cache. object ( forKey: url as NSURL ) {
45+
46+ let cacheKey = Self . cacheKeyProvider ( . url( url) )
47+ if let image = Self . cache. object ( forKey: cacheKey) {
3948 state = . image( image)
4049 return
4150 }
42-
51+
4352 let urlSession = URLSession . shared
4453 let urlRequest = URLRequest ( url: url)
45-
54+
4655 cancellable = urlSession. dataTaskPublisher ( for: urlRequest)
4756 . map { PlatformSpecificImageType ( data: $0. data) }
4857 . receive ( on: RunLoop . main)
@@ -54,23 +63,24 @@ extension RemoteImageService {
5463 }
5564 } ) { image in
5665 if let image = image {
57- RemoteImageService . cache. setObject ( image, forKey: url as NSURL )
66+ Self . cache. setObject ( image, forKey: cacheKey )
5867 self . state = . image( image)
5968 } else {
6069 self . state = . error( RemoteImageServiceError . couldNotCreateImage)
6170 }
6271 }
6372 }
64-
73+
6574 private func fetchImage( withLocalIdentifier localIdentifier: String ) {
66- if let image = RemoteImageService . cache. object ( forKey: localIdentifier as NSString ) {
75+ let cacheKey = Self . cacheKeyProvider ( . phAsset( localIdentifier: localIdentifier) )
76+ if let image = Self . cache. object ( forKey: cacheKey) {
6777 state = . image( image)
6878 return
6979 }
70-
80+
7181 dependencies. photoKitService. getPhotoData ( localIdentifier: localIdentifier, success: { data in
7282 if let image = PlatformSpecificImageType ( data: data) {
73- RemoteImageService . cache. setObject ( image, forKey: localIdentifier as NSString )
83+ Self . cache. setObject ( image, forKey: cacheKey )
7484 self . state = . image( image)
7585 } else {
7686 self . state = . error( RemoteImageServiceError . couldNotCreateImage)
0 commit comments