Prechádzať zdrojové kódy

Merge branch 'master' of https://github.com/victorg1991/Kingfisher into victorg1991-master

# Please enter a commit message to explain why this merge is necessary,
# especially if it merges an updated upstream into a topic branch.
#
# Lines starting with '#' will be ignored, and an empty message aborts
# the commit.
onevcat 9 rokov pred
rodič
commit
3acfde1b46

+ 2 - 0
Sources/ImageDownloader.swift

@@ -80,12 +80,14 @@ The error code.
 
 
 - BadData: The downloaded data is not an image or the data is corrupted.
 - BadData: The downloaded data is not an image or the data is corrupted.
 - NotModified: The remote server responsed a 304 code. No image data downloaded.
 - NotModified: The remote server responsed a 304 code. No image data downloaded.
+- NotCached: The image rquested is not in cache and CacheOnly is activated
 - InvalidURL: The URL is invalid.
 - InvalidURL: The URL is invalid.
 */
 */
 public enum KingfisherError: Int {
 public enum KingfisherError: Int {
     case BadData = 10000
     case BadData = 10000
     case NotModified = 10001
     case NotModified = 10001
     case InvalidStatusCode = 10002
     case InvalidStatusCode = 10002
+	case NotCached = 10003
     case InvalidURL = 20000
     case InvalidURL = 20000
 }
 }
 
 

+ 6 - 0
Sources/KingfisherManager.swift

@@ -217,6 +217,12 @@ public class KingfisherManager {
             completionHandler: { image, cacheType in
             completionHandler: { image, cacheType in
                 if image != nil {
                 if image != nil {
                     diskTaskCompletionHandler(image: image, error: nil, cacheType:cacheType, imageURL: URL)
                     diskTaskCompletionHandler(image: image, error: nil, cacheType:cacheType, imageURL: URL)
+                } else if let options = options where options.onlyFromCache {
+                    diskTaskCompletionHandler(image: nil,
+                        error: NSError(domain: KingfisherErrorDomain, code: KingfisherError.NotCached.rawValue, userInfo: nil),
+                        cacheType:.None,
+                        imageURL: URL)
+                    return
                 } else {
                 } else {
                     self.downloadAndCacheImageWithURL(URL,
                     self.downloadAndCacheImageWithURL(URL,
                         forKey: key,
                         forKey: key,

+ 7 - 0
Sources/KingfisherOptionsInfo.swift

@@ -47,6 +47,7 @@ Items could be added into KingfisherOptionsInfo.
 - ForceRefresh: If set, `Kingfisher` will ignore the cache and try to fire a download task for the resource.
 - ForceRefresh: If set, `Kingfisher` will ignore the cache and try to fire a download task for the resource.
 - ForceTransition: If set, setting the image to an image view will happen with transition even when retrieved from cache. See `Transition` option for more.
 - ForceTransition: If set, setting the image to an image view will happen with transition even when retrieved from cache. See `Transition` option for more.
 - CacheMemoryOnly: If set, `Kingfisher` will only cache the value in memory but not in disk.
 - CacheMemoryOnly: If set, `Kingfisher` will only cache the value in memory but not in disk.
+- OnlyFromCache: If set, `Kingfisher` will only try to retrieve the image from cache not from network.
 - BackgroundDecode: Decode the image in background thread before using.
 - BackgroundDecode: Decode the image in background thread before using.
 - CallbackDispatchQueue: The associated value of this member will be used as the target queue of dispatch callbacks when retrieving images from cache. If not set, `Kingfisher` will use main quese for callbacks.
 - CallbackDispatchQueue: The associated value of this member will be used as the target queue of dispatch callbacks when retrieving images from cache. If not set, `Kingfisher` will use main quese for callbacks.
 - ScaleFactor: The associated value of this member will be used as the scale factor when converting retrieved data to an image.
 - ScaleFactor: The associated value of this member will be used as the scale factor when converting retrieved data to an image.
@@ -60,6 +61,7 @@ public enum KingfisherOptionsInfoItem {
     case ForceRefresh
     case ForceRefresh
     case ForceTransition
     case ForceTransition
     case CacheMemoryOnly
     case CacheMemoryOnly
+    case OnlyFromCache
     case BackgroundDecode
     case BackgroundDecode
     case CallbackDispatchQueue(dispatch_queue_t?)
     case CallbackDispatchQueue(dispatch_queue_t?)
     case ScaleFactor(CGFloat)
     case ScaleFactor(CGFloat)
@@ -81,6 +83,7 @@ func <== (lhs: KingfisherOptionsInfoItem, rhs: KingfisherOptionsInfoItem) -> Boo
     case (.ForceRefresh, .ForceRefresh): fallthrough
     case (.ForceRefresh, .ForceRefresh): fallthrough
     case (.ForceTransition, .ForceTransition): fallthrough
     case (.ForceTransition, .ForceTransition): fallthrough
     case (.CacheMemoryOnly, .CacheMemoryOnly): fallthrough
     case (.CacheMemoryOnly, .CacheMemoryOnly): fallthrough
+    case (.OnlyFromCache, .OnlyFromCache): fallthrough
     case (.BackgroundDecode, .BackgroundDecode): fallthrough
     case (.BackgroundDecode, .BackgroundDecode): fallthrough
     case (.CallbackDispatchQueue(_), .CallbackDispatchQueue(_)): fallthrough
     case (.CallbackDispatchQueue(_), .CallbackDispatchQueue(_)): fallthrough
     case (.ScaleFactor(_), .ScaleFactor(_)): fallthrough
     case (.ScaleFactor(_), .ScaleFactor(_)): fallthrough
@@ -149,6 +152,10 @@ extension CollectionType where Generator.Element == KingfisherOptionsInfoItem {
         return contains{ $0 <== .CacheMemoryOnly }
         return contains{ $0 <== .CacheMemoryOnly }
     }
     }
     
     
+    var onlyFromCache: Bool {
+        return contains{ $0 <== .OnlyFromCache }
+    }
+    
     var backgroundDecode: Bool {
     var backgroundDecode: Bool {
         return contains{ $0 <== .BackgroundDecode }
         return contains{ $0 <== .BackgroundDecode }
     }
     }

+ 18 - 1
Tests/KingfisherTests/KingfisherManagerTests.swift

@@ -148,7 +148,24 @@ class KingfisherManagerTests: XCTestCase {
         })
         })
         waitForExpectationsWithTimeout(5, handler: nil)
         waitForExpectationsWithTimeout(5, handler: nil)
     }
     }
-    
+
+    func testShouldNotDownloadImageIfCacheOnlyAndNotInCache() {
+        cleanDefaultCache()
+        let expectation = expectationWithDescription("wait for retrieving image cache")
+        let URLString = testKeys[0]
+        stubRequest("GET", URLString).andReturn(200).withBody(testImageData)
+
+        let URL = NSURL(string: URLString)!
+
+        manager.retrieveImageWithURL(URL, optionsInfo: [.OnlyFromCache], progressBlock: nil, completionHandler: { image, error, _, _ in
+                XCTAssertNil(image)
+                XCTAssertNotNil(error)
+                XCTAssertEqual(error!.code, KingfisherError.NotCached.rawValue)
+                expectation.fulfill()
+        })
+        waitForExpectationsWithTimeout(5, handler: nil)
+    }
+
     func testErrorCompletionHandlerRunningOnMainQueueDefaultly() {
     func testErrorCompletionHandlerRunningOnMainQueueDefaultly() {
         let expectation = expectationWithDescription("running on main queue")
         let expectation = expectationWithDescription("running on main queue")
         let URLString = testKeys[0]
         let URLString = testKeys[0]

+ 2 - 0
Tests/KingfisherTests/KingfisherOptionsInfoTests.swift

@@ -80,6 +80,7 @@ class KingfisherOptionsInfoTests: XCTestCase {
             .DownloadPriority(0.8),
             .DownloadPriority(0.8),
             .ForceRefresh,
             .ForceRefresh,
             .CacheMemoryOnly,
             .CacheMemoryOnly,
+            .OnlyFromCache,
             .BackgroundDecode,
             .BackgroundDecode,
             .CallbackDispatchQueue(queue),
             .CallbackDispatchQueue(queue),
             .ScaleFactor(2.0)
             .ScaleFactor(2.0)
@@ -98,6 +99,7 @@ class KingfisherOptionsInfoTests: XCTestCase {
         XCTAssertEqual(options.downloadPriority, 0.8)
         XCTAssertEqual(options.downloadPriority, 0.8)
         XCTAssertTrue(options.forceRefresh)
         XCTAssertTrue(options.forceRefresh)
         XCTAssertTrue(options.cacheMemoryOnly)
         XCTAssertTrue(options.cacheMemoryOnly)
+        XCTAssertTrue(options.onlyFromCache)
         XCTAssertTrue(options.backgroundDecode)
         XCTAssertTrue(options.backgroundDecode)
         
         
         XCTAssertEqual(dispatch_queue_get_label(options.callbackDispatchQueue), dispatch_queue_get_label(queue))
         XCTAssertEqual(dispatch_queue_get_label(options.callbackDispatchQueue), dispatch_queue_get_label(queue))