Ver Fonte

Some refactor for preparing adding source support

onevcat há 6 anos atrás
pai
commit
ee175db2a3

+ 16 - 6
Sources/General/KingfisherManager.swift

@@ -166,7 +166,10 @@ public class KingfisherManager {
     {
         if options.forceRefresh {
             return loadAndCacheImage(
-                source: source, options: options, progressBlock: progressBlock, completionHandler: completionHandler)
+                source: source,
+                options: options,
+                progressBlock: progressBlock,
+                completionHandler: completionHandler)?.value
         } else {
             let loadedFromCache = retrieveImageFromCache(
                 source: source,
@@ -184,7 +187,10 @@ public class KingfisherManager {
             }
             
             return loadAndCacheImage(
-                source: source, options: options, progressBlock: progressBlock, completionHandler: completionHandler)
+                source: source,
+                options: options,
+                progressBlock: progressBlock,
+                completionHandler: completionHandler)?.value
         }
     }
 
@@ -230,7 +236,7 @@ public class KingfisherManager {
         source: Source,
         options: KingfisherParsedOptionsInfo,
         progressBlock: DownloadProgressBlock? = nil,
-        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask?
+        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)?) -> DownloadTask.WrappedTask?
     {
         func cacheImage(_ result: Result<ImageLoadingResult, KingfisherError>)
         {
@@ -276,14 +282,18 @@ public class KingfisherManager {
         switch source {
         case .network(let resource):
             let downloader = options.downloader ?? self.downloader
-            return downloader.downloadImage(
+            guard let task = downloader.downloadImage(
                 with: resource.downloadURL,
                 options: options,
                 progressBlock: progressBlock,
-                completionHandler: cacheImage)
+                completionHandler: cacheImage) else
+            {
+                return nil
+            }
+            return .download(task)
         case .provider(let provider):
             provideImage(provider: provider, options: options, completionHandler: cacheImage)
-            return nil
+            return .dataProviding
         }
     }
     

+ 21 - 0
Sources/Networking/ImageDownloader.swift

@@ -74,6 +74,27 @@ public struct DownloadTask {
     }
 }
 
+extension DownloadTask {
+    enum WrappedTask {
+        case download(DownloadTask)
+        case dataProviding
+
+        func cancel() {
+            switch self {
+            case .download(let task): task.cancel()
+            case .dataProviding: break
+            }
+        }
+
+        var value: DownloadTask? {
+            switch self {
+            case .download(let task): return task
+            case .dataProviding: return nil
+            }
+        }
+    }
+}
+
 /// Represents a downloading manager for requesting the image with a URL from server.
 open class ImageDownloader {
 

+ 7 - 2
Sources/Networking/ImagePrefetcher.swift

@@ -40,6 +40,9 @@ import UIKit
 public typealias PrefetcherProgressBlock =
     ((_ skippedResources: [Resource], _ failedResources: [Resource], _ completedResources: [Resource]) -> Void)
 
+public typealias PrefetcherSourceProgressBlock =
+    ((_ skippedSources: [Source], _ failedSources: [Source], _ completedSources: [Source]) -> Void)
+
 /// Completion block of prefetcher.
 ///
 /// - `skippedResources`: An array of resources that are already cached before the prefetching starting.
@@ -49,6 +52,9 @@ public typealias PrefetcherProgressBlock =
 public typealias PrefetcherCompletionHandler =
     ((_ skippedResources: [Resource], _ failedResources: [Resource], _ completedResources: [Resource]) -> Void)
 
+public typealias PrefetcherSourceCompletionHandler =
+    ((_ skippedSources: [Source], _ failedSources: [Source], _ completedSources: [Source]) -> Void)
+
 /// `ImagePrefetcher` represents a downloading manager for requesting many images via URLs, then caching them.
 /// This is useful when you know a list of image resources and want to download them before showing. It also works with
 /// some Cocoa prefetching mechanism like table view or collection view `prefetchDataSource`, to start image downloading
@@ -58,14 +64,13 @@ public class ImagePrefetcher {
     /// The maximum concurrent downloads to use when prefetching images. Default is 5.
     public var maxConcurrentDownloads = 5
 
-    
     private let prefetchResources: [Resource]
     private let optionsInfo: KingfisherParsedOptionsInfo
 
     private var progressBlock: PrefetcherProgressBlock?
     private var completionHandler: PrefetcherCompletionHandler?
     
-    private var tasks = [URL: DownloadTask]()
+    private var tasks = [URL: DownloadTask.WrappedTask]()
     
     private var pendingResources: ArraySlice<Resource>
     private var skippedResources = [Resource]()