Просмотр исходного кода

Add alternativeSources to option info and test suite

onevcat 6 лет назад
Родитель
Сommit
5b3e8a9733

+ 9 - 0
Sources/General/KingfisherManager.swift

@@ -456,5 +456,14 @@ public class KingfisherManager {
 extension KingfisherManager {
 extension KingfisherManager {
     struct RetrievingContext {
     struct RetrievingContext {
         var options: KingfisherParsedOptionsInfo
         var options: KingfisherParsedOptionsInfo
+        
+        mutating func popAlternativeSource() -> Source? {
+            guard var alternativeSources = options.alternativeSources, !alternativeSources.isEmpty else {
+                return nil
+            }
+            let nextSource = alternativeSources.removeFirst()
+            options.alternativeSources = alternativeSources
+            return nextSource
+        }
     }
     }
 }
 }

+ 4 - 0
Sources/General/KingfisherOptionsInfo.swift

@@ -229,6 +229,8 @@ public enum KingfisherOptionsInfoItem {
     
     
     /// Enable progressive image loading, Kingfisher will use the `ImageProgressive` of
     /// Enable progressive image loading, Kingfisher will use the `ImageProgressive` of
     case progressiveJPEG(ImageProgressive)
     case progressiveJPEG(ImageProgressive)
+
+    case alternativeSources([Source])
 }
 }
 
 
 // Improve performance by parsing the input `KingfisherOptionsInfo` (self) first.
 // Improve performance by parsing the input `KingfisherOptionsInfo` (self) first.
@@ -270,6 +272,7 @@ public struct KingfisherParsedOptionsInfo {
     public var diskCacheAccessExtendingExpiration: ExpirationExtending = .cacheTime
     public var diskCacheAccessExtendingExpiration: ExpirationExtending = .cacheTime
     public var processingQueue: CallbackQueue? = nil
     public var processingQueue: CallbackQueue? = nil
     public var progressiveJPEG: ImageProgressive? = nil
     public var progressiveJPEG: ImageProgressive? = nil
+    public var alternativeSources: [Source]? = nil
 
 
     var onDataReceived: [DataReceivingSideEffect]? = nil
     var onDataReceived: [DataReceivingSideEffect]? = nil
     
     
@@ -310,6 +313,7 @@ public struct KingfisherParsedOptionsInfo {
             case .diskCacheAccessExtendingExpiration(let expirationExtending): diskCacheAccessExtendingExpiration = expirationExtending
             case .diskCacheAccessExtendingExpiration(let expirationExtending): diskCacheAccessExtendingExpiration = expirationExtending
             case .processingQueue(let queue): processingQueue = queue
             case .processingQueue(let queue): processingQueue = queue
             case .progressiveJPEG(let value): progressiveJPEG = value
             case .progressiveJPEG(let value): progressiveJPEG = value
+            case .alternativeSources(let sources): alternativeSources = sources
             }
             }
         }
         }
 
 

+ 27 - 0
Tests/KingfisherTests/KingfisherManagerTests.swift

@@ -687,6 +687,33 @@ class KingfisherManagerTests: XCTestCase {
         }
         }
         XCTAssertTrue(called)
         XCTAssertTrue(called)
     }
     }
+
+    func testContextRemovingAlternativeSource() {
+        let allSources: [Source] = [
+            .network(URL(string: "1")!),
+            .network(URL(string: "2")!)
+        ]
+        let info = KingfisherParsedOptionsInfo([.alternativeSources(allSources)])
+        var context = KingfisherManager.RetrievingContext(options: info)
+
+        let source1 = context.popAlternativeSource()
+        XCTAssertNotNil(source1)
+        guard case .network(let r1) = source1! else {
+            XCTFail("Should be a network source, but \(source1!)")
+            return
+        }
+        XCTAssertEqual(r1.downloadURL.absoluteString, "1")
+
+        let source2 = context.popAlternativeSource()
+        XCTAssertNotNil(source2)
+        guard case .network(let r2) = source2! else {
+            XCTFail("Should be a network source, but \(source2!)")
+            return
+        }
+        XCTAssertEqual(r2.downloadURL.absoluteString, "2")
+
+        XCTAssertNil(context.popAlternativeSource())
+    }
 }
 }
 
 
 class SimpleProcessor: ImageProcessor {
 class SimpleProcessor: ImageProcessor {