Browse Source

Improve empty option syntax

onevcat 7 years ago
parent
commit
51876944f5

+ 3 - 3
Kingfisher.xcodeproj/project.pbxproj

@@ -367,13 +367,13 @@
 			children = (
 				D12AB6A9215D2BB50013BA68 /* Info.plist */,
 				D12AB6AA215D2BB50013BA68 /* Kingfisher.h */,
-				D12AB6B5215D2BB50013BA68 /* Cache */,
-				D12AB6AB215D2BB50013BA68 /* Extensions */,
 				D12AB6B0215D2BB50013BA68 /* General */,
 				D12AB6A2215D2BB50013BA68 /* Image */,
 				D12AB69C215D2BB50013BA68 /* Networking */,
-				D12AB6B9215D2BB50013BA68 /* Utility */,
+				D12AB6B5215D2BB50013BA68 /* Cache */,
 				D12AB6BD215D2BB50013BA68 /* Views */,
+				D12AB6AB215D2BB50013BA68 /* Extensions */,
+				D12AB6B9215D2BB50013BA68 /* Utility */,
 			);
 			path = Sources;
 			sourceTree = "<group>";

+ 1 - 1
Sources/Cache/CacheSerializer.swift

@@ -77,7 +77,7 @@ public struct DefaultCacheSerializer: CacheSerializer {
     }
     
     public func image(with data: Data, options: KingfisherOptionsInfo?) -> Image? {
-        let options = options ?? KingfisherEmptyOptionsInfo
+        let options = options ?? .empty
         return KingfisherClass.image(data: data, options: options.imageCreatingOptions)
     }
 }

+ 1 - 1
Sources/Cache/FormatIndicatedCacheSerializer.swift

@@ -86,7 +86,7 @@ public struct FormatIndicatedCacheSerializer: CacheSerializer {
     
     /// Same implementation as `DefaultCacheSerializer`.
     public func image(with data: Data, options: KingfisherOptionsInfo?) -> Image? {
-        let options = options ?? KingfisherEmptyOptionsInfo
+        let options = options ?? .empty
         return KingfisherClass.image(data: data, options: options.imageCreatingOptions)
     }
 }

+ 3 - 3
Sources/Cache/ImageCache.swift

@@ -292,7 +292,7 @@ open class ImageCache {
         }
         
         var block: RetrieveImageDiskTask?
-        let options = options ?? KingfisherEmptyOptionsInfo
+        let options = options ?? .empty
         let imageModifier = options.imageModifier
 
         if let image = self.retrieveImageInMemoryCache(forKey: key, options: options) {
@@ -362,7 +362,7 @@ open class ImageCache {
     */
     open func retrieveImageInMemoryCache(forKey key: String, options: KingfisherOptionsInfo? = nil) -> Image? {
         
-        let options = options ?? KingfisherEmptyOptionsInfo
+        let options = options ?? .empty
         let computedKey = key.computedKey(with: options.processor.identifier)
         
         return memoryCache.object(forKey: computedKey as NSString) as? Image
@@ -379,7 +379,7 @@ open class ImageCache {
     */
     open func retrieveImageInDiskCache(forKey key: String, options: KingfisherOptionsInfo? = nil) -> Image? {
         
-        let options = options ?? KingfisherEmptyOptionsInfo
+        let options = options ?? .empty
         let computedKey = key.computedKey(with: options.processor.identifier)
         
         return diskImage(forComputedKey: computedKey, serializer: options.cacheSerializer, options: options)

+ 1 - 1
Sources/Extensions/ImageView+Kingfisher.swift

@@ -67,7 +67,7 @@ extension KingfisherClass where Base: ImageView {
             return .empty
         }
         
-        var options = KingfisherManager.shared.defaultOptions + (options ?? KingfisherEmptyOptionsInfo)
+        var options = KingfisherManager.shared.defaultOptions + (options ?? .empty)
         let noImageOrPlaceholderSet = base.image == nil && self.placeholder == nil
         
         if !options.keepCurrentImageWhileLoading || noImageOrPlaceholderSet { // Always set placeholder while there is no image/placehoer yet.

+ 2 - 2
Sources/Extensions/NSButton+Kingfisher.swift

@@ -63,7 +63,7 @@ extension KingfisherClass where Base: NSButton {
             return .empty
         }
         
-        let options = KingfisherManager.shared.defaultOptions + (options ?? KingfisherEmptyOptionsInfo)
+        let options = KingfisherManager.shared.defaultOptions + (options ?? .empty)
         if !options.keepCurrentImageWhileLoading {
             base.image = placeholder
         }
@@ -138,7 +138,7 @@ extension KingfisherClass where Base: NSButton {
             return .empty
         }
         
-        let options = KingfisherManager.shared.defaultOptions + (options ?? KingfisherEmptyOptionsInfo)
+        let options = KingfisherManager.shared.defaultOptions + (options ?? .empty)
         if !options.keepCurrentImageWhileLoading {
             base.alternateImage = placeholder
         }

+ 2 - 2
Sources/Extensions/UIButton+Kingfisher.swift

@@ -65,7 +65,7 @@ extension KingfisherClass where Base: UIButton {
             return .empty
         }
         
-        let options = KingfisherManager.shared.defaultOptions + (options ?? KingfisherEmptyOptionsInfo)
+        let options = KingfisherManager.shared.defaultOptions + (options ?? .empty)
         if !options.keepCurrentImageWhileLoading {
             base.setImage(placeholder, for: state)
         }
@@ -143,7 +143,7 @@ extension KingfisherClass where Base: UIButton {
             return .empty
         }
         
-        let options = KingfisherManager.shared.defaultOptions + (options ?? KingfisherEmptyOptionsInfo)
+        let options = KingfisherManager.shared.defaultOptions + (options ?? .empty)
         if !options.keepCurrentImageWhileLoading {
             base.setBackgroundImage(placeholder, for: state)
         }

+ 2 - 2
Sources/General/KingfisherManager.swift

@@ -81,7 +81,7 @@ public class KingfisherManager {
     /// will overwrite the default ones if exist.
     ///
     /// - Note: This option will not be applied to independent using of `ImageDownloader` or `ImageCache`.
-    public var defaultOptions = KingfisherEmptyOptionsInfo
+    public var defaultOptions = KingfisherOptionsInfo.empty
     
     var currentDefaultOptions: KingfisherOptionsInfo {
         return [.downloader(downloader), .targetCache(cache)] + defaultOptions
@@ -121,7 +121,7 @@ public class KingfisherManager {
         completionHandler: CompletionHandler?) -> RetrieveImageTask
     {
         let task = RetrieveImageTask()
-        let options = currentDefaultOptions + (options ?? KingfisherEmptyOptionsInfo)
+        let options = currentDefaultOptions + (options ?? .empty)
         if options.forceRefresh {
             _ = downloadAndCacheImage(
                 with: resource.downloadURL,

+ 14 - 12
Sources/General/KingfisherOptionsInfo.swift

@@ -35,8 +35,11 @@ import UIKit
 *	KingfisherOptionsInfo is a typealias for [KingfisherOptionsInfoItem]. You can use the enum of option item with value to control some behaviors of Kingfisher.
 */
 public typealias KingfisherOptionsInfo = [KingfisherOptionsInfoItem]
+
 extension Array where Element == KingfisherOptionsInfoItem {
+    
     static var empty: KingfisherOptionsInfo = []
+    
     var imageCreatingOptions: ImageCreatingOptions {
         return ImageCreatingOptions(
             scale: scaleFactor,
@@ -45,7 +48,6 @@ extension Array where Element == KingfisherOptionsInfoItem {
             onlyFirstFrame: onlyLoadFirstFrame)
     }
 }
-let KingfisherEmptyOptionsInfo = [KingfisherOptionsInfoItem]()
 
 /**
 Items could be added into KingfisherOptionsInfo.
@@ -169,11 +171,11 @@ infix operator <== : ItemComparisonPrecedence
 // This operator returns true if two `KingfisherOptionsInfoItem` enum is the same, without considering the associated values.
 func <== (lhs: KingfisherOptionsInfoItem, rhs: KingfisherOptionsInfoItem) -> Bool {
     switch (lhs, rhs) {
-    case (.targetCache(_), .targetCache(_)): return true
-    case (.originalCache(_), .originalCache(_)): return true
-    case (.downloader(_), .downloader(_)): return true
-    case (.transition(_), .transition(_)): return true
-    case (.downloadPriority(_), .downloadPriority(_)): return true
+    case (.targetCache, .targetCache): return true
+    case (.originalCache, .originalCache): return true
+    case (.downloader, .downloader): return true
+    case (.transition, .transition): return true
+    case (.downloadPriority, .downloadPriority): return true
     case (.forceRefresh, .forceRefresh): return true
     case (.fromMemoryCacheOrRefresh, .fromMemoryCacheOrRefresh): return true
     case (.forceTransition, .forceTransition): return true
@@ -181,13 +183,13 @@ func <== (lhs: KingfisherOptionsInfoItem, rhs: KingfisherOptionsInfoItem) -> Boo
     case (.waitForCache, .waitForCache): return true
     case (.onlyFromCache, .onlyFromCache): return true
     case (.backgroundDecode, .backgroundDecode): return true
-    case (.callbackDispatchQueue(_), .callbackDispatchQueue(_)): return true
-    case (.scaleFactor(_), .scaleFactor(_)): return true
+    case (.callbackDispatchQueue, .callbackDispatchQueue): return true
+    case (.scaleFactor, .scaleFactor): return true
     case (.preloadAllAnimationData, .preloadAllAnimationData): return true
-    case (.requestModifier(_), .requestModifier(_)): return true
-    case (.processor(_), .processor(_)): return true
-    case (.cacheSerializer(_), .cacheSerializer(_)): return true
-    case (.imageModifier(_), .imageModifier(_)): return true
+    case (.requestModifier, .requestModifier): return true
+    case (.processor, .processor): return true
+    case (.cacheSerializer, .cacheSerializer): return true
+    case (.imageModifier, .imageModifier): return true
     case (.keepCurrentImageWhileLoading, .keepCurrentImageWhileLoading): return true
     case (.onlyLoadFirstFrame, .onlyLoadFirstFrame): return true
     case (.cacheOriginalImage, .cacheOriginalImage): return true

+ 11 - 4
Sources/Image/ImageProcessor.swift

@@ -27,16 +27,23 @@
 import Foundation
 import CoreGraphics
 
-#if os(macOS)
+#if canImport(AppKit)
 import AppKit
 #endif
 
-/// The item which could be processed by an `ImageProcessor`
+/// Represents an item which could be processed by an `ImageProcessor`
 ///
-/// - image: Input image
-/// - data:  Input data
+/// - image: Input image. The processor should provide a way to apply
+///          processing on this `image` and return the result image.
+/// - data:  Input data. The processor should provide a way to apply
+///          processing on this `image` and return the result image.
 public enum ImageProcessItem {
+    /// Input image. The processor should provide a way to apply
+    /// processing on this `image` and return the result image.
     case image(Image)
+    
+    /// Input data. The processor should provide a way to apply
+    /// processing on this `image` and return the result image.
     case data(Data)
 }
 

+ 22 - 22
Sources/Image/ImageTransition.swift

@@ -26,9 +26,6 @@
 
 #if os(macOS)
 // Not implemented for macOS and watchOS yet.
-    
-import AppKit
-
 /// Image transition is not supported on macOS.
 public enum ImageTransition {
     case none
@@ -38,7 +35,6 @@ public enum ImageTransition {
 }
 
 #elseif os(watchOS)
-import UIKit
 /// Image transition is not supported on watchOS.
 public enum ImageTransition {
     case none
@@ -49,35 +45,39 @@ public enum ImageTransition {
 #else
 import UIKit
 
-/**
-Transition effect which will be used when an image downloaded and set by `UIImageView` extension API in Kingfisher.
-You can assign an enum value with transition duration as an item in `KingfisherOptionsInfo` 
-to enable the animation transition.
-
-Apple's UIViewAnimationOptions is used under the hood.
-For custom transition, you should specified your own transition options, animations and 
-completion handler as well.
-*/
+/// Transition effect which will be used when an image downloaded and set by `UIImageView` extension API in Kingfisher.
+/// You can assign an enum value with transition duration as an item in `KingfisherOptionsInfo`
+/// to enable the animation transition.
+///
+/// Apple's UIViewAnimationOptions is used under the hood.
+/// For custom transition, you should specified your own transition options, animations and
+/// completion handler as well.
+///
+/// - none: No animation transition.
+/// - fade: Fade in the loaded image in a given duration.
+/// - flipFromLeft: Flip from left transition.
+/// - flipFromRight: Flip from right transition.
+/// - flipFromTop: Flip from top transition.
+/// - flipFromBottom: Flip from bottom transition.
+/// - customduration: Custom transition.
 public enum ImageTransition {
-    ///  No animation transition.
+    /// No animation transition.
     case none
-    
-    /// Fade in the loaded image.
+    /// Fade in the loaded image in a given duration.
     case fade(TimeInterval)
-
     /// Flip from left transition.
     case flipFromLeft(TimeInterval)
-
     /// Flip from right transition.
     case flipFromRight(TimeInterval)
-    
     /// Flip from top transition.
     case flipFromTop(TimeInterval)
-    
     /// Flip from bottom transition.
     case flipFromBottom(TimeInterval)
-    
-    /// Custom transition.
+    /// Custom transition defined by a general animation block.
+    ///    - duration: The time duration of this custom transition.
+    ///    - options: `UIView.AnimationOptions` should be used in the transition.
+    ///    - animations: The animation block will be applied when setting image.
+    ///    - completion: A block called when the transition animation finishes.
     case custom(duration: TimeInterval,
                  options: UIView.AnimationOptions,
               animations: ((UIImageView, UIImage) -> Void)?,

+ 1 - 1
Sources/Networking/ImageDownloader.swift

@@ -390,7 +390,7 @@ extension ImageDownloader {
                 let loadObjectForURL = fetchLoads[url] ?? ImageFetchLoad()
                 let callbackPair = (progressBlock: progressBlock, completionHandler: completionHandler)
                 
-                loadObjectForURL.contents.append((callbackPair, options ?? KingfisherEmptyOptionsInfo))
+                loadObjectForURL.contents.append((callbackPair, options ?? .empty))
                 
                 fetchLoads[url] = loadObjectForURL
                 

+ 1 - 1
Sources/Networking/ImagePrefetcher.swift

@@ -132,7 +132,7 @@ public class ImagePrefetcher {
         prefetchQueue = DispatchQueue(label: prefetchQueueName)
         
         // We want all callbacks from our prefetch queue, so we should ignore the call back queue in options
-        var optionsInfoWithoutQueue = options?.removeAllMatchesIgnoringAssociatedValue(.callbackDispatchQueue(nil)) ?? KingfisherEmptyOptionsInfo
+        var optionsInfoWithoutQueue = options?.removeAllMatchesIgnoringAssociatedValue(.callbackDispatchQueue(nil)) ?? .empty
         
         // Add our own callback dispatch queue to make sure all callbacks are coming back in our expected queue
         optionsInfoWithoutQueue.append(.callbackDispatchQueue(prefetchQueue))

+ 2 - 1
Sources/Utility/ExtenionHelpers.swift

@@ -32,7 +32,8 @@ extension Float {
     }
 }
 
-#if os(macOS)
+#if canImport(AppKit)
+import AppKit
 extension NSBezierPath {
     convenience init(roundedRect rect: NSRect, topLeftRadius: CGFloat, topRightRadius: CGFloat,
                      bottomLeftRadius: CGFloat, bottomRightRadius: CGFloat)

+ 1 - 1
Tests/KingfisherTests/KingfisherOptionsInfoTests.swift

@@ -41,7 +41,7 @@ class KingfisherOptionsInfoTests: XCTestCase {
     }
     
     func testEmptyOptionsShouldParseCorrectly() {
-        let options = KingfisherEmptyOptionsInfo
+        let options = KingfisherOptionsInfo.empty
         XCTAssertTrue(options.targetCache === nil)
         XCTAssertTrue(options.downloader === nil)