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

conditional compile for CPListItem and SDK 14.0-14.4

Wayne Hartman 4 лет назад
Родитель
Сommit
6f280fbe8b
2 измененных файлов с 74 добавлено и 2 удалено
  1. 47 2
      Sources/Extensions/CPListItem+Kingfisher.swift
  2. 27 0
      Sources/General/KF.swift

+ 47 - 2
Sources/Extensions/CPListItem+Kingfisher.swift

@@ -110,7 +110,21 @@ extension KingfisherWrapper where Base: CPListItem {
     {
         var mutatingSelf = self
         guard let source = source else {
-            base.setImage(placeholder)
+            /**
+             * In iOS SDK 14.0-14.4 the image param was non-`nil`. The SDK changed in 14.5
+             * to allow `nil`. The compiler version 5.4 was introduced in this same SDK,
+             * which allows >=14.5 SDK to set a `nil` image. This compile check allows
+             * newer SDK users to set the image to `nil`, while still allowing older SDK
+             * users to compile the framework.
+             */
+            #if compiler(>=5.4)
+            self.base.setImage(placeholder)
+            #else
+            if let placeholder = placeholder {
+                self.base.setImage(placeholder)
+            }
+            #endif
+
             mutatingSelf.taskIdentifier = nil
             completionHandler?(.failure(KingfisherError.imageSettingError(reason: .emptySource)))
             return nil
@@ -118,7 +132,20 @@ extension KingfisherWrapper where Base: CPListItem {
         
         var options = parsedOptions
         if !options.keepCurrentImageWhileLoading {
-            base.setImage(placeholder)
+            /**
+             * In iOS SDK 14.0-14.4 the image param was non-`nil`. The SDK changed in 14.5
+             * to allow `nil`. The compiler version 5.4 was introduced in this same SDK,
+             * which allows >=14.5 SDK to set a `nil` image. This compile check allows
+             * newer SDK users to set the image to `nil`, while still allowing older SDK
+             * users to compile the framework.
+             */
+            #if compiler(>=5.4)
+            self.base.setImage(placeholder)
+            #else // Let older SDK users deal with the older behavior.
+            if let placeholder = placeholder {
+                self.base.setImage(placeholder)
+            }
+            #endif
         }
         
         let issuedIdentifier = Source.Identifier.next()
@@ -167,7 +194,25 @@ extension KingfisherWrapper where Base: CPListItem {
                             
                         case .failure:
                             if let image = options.onFailureImage {
+                                /**
+                                 * In iOS SDK 14.0-14.4 the image param was non-`nil`. The SDK changed in 14.5
+                                 * to allow `nil`. The compiler version 5.4 was introduced in this same SDK,
+                                 * which allows >=14.5 SDK to set a `nil` image. This compile check allows
+                                 * newer SDK users to set the image to `nil`, while still allowing older SDK
+                                 * users to compile the framework.
+                                 */
+                                #if compiler(>=5.4)
                                 self.base.setImage(image)
+                                #else // Let older SDK users deal with the older behavior.
+                                if let unwrapped = image {
+                                    self.base.setImage(unwrapped)
+                                }
+                                #endif
+                                
+                            } else {
+                                #if compiler(>=5.4)
+                                self.base.setImage(nil)
+                                #endif
                             }
                             completionHandler?(result)
                     }

+ 27 - 0
Sources/General/KF.swift

@@ -28,6 +28,10 @@
 import UIKit
 #endif
 
+#if canImport(CarPlay)
+import CarPlay
+#endif
+
 #if canImport(AppKit) && !targetEnvironment(macCatalyst)
 import AppKit
 #endif
@@ -211,6 +215,29 @@ extension KF.Builder {
         )
     }
     #endif // end of canImport(UIKit)
+    
+    #if canImport(CarPlay)
+    
+    /// Builds the image task request and sets it to the image for a list item.
+    /// - Parameters:
+    ///   - listItem: The list item which loads the task and should be set with the image.
+    /// - Returns: A task represents the image downloading, if initialized.
+    ///            This value is `nil` if the image is being loaded from cache.
+    @available(iOSApplicationExtension 14.0, *)
+    @discardableResult
+    public func set(to listItem: CPListItem) -> DownloadTask? {
+        let placeholderImage = placeholder as? KFCrossPlatformImage ?? nil
+        return listItem.kf.setImage(
+            with: source,
+            placeholder: placeholderImage,
+            parsedOptions: options,
+            progressBlock: progressBlock,
+            completionHandler: resultHandler
+        )
+        
+    }
+    
+    #endif
 
     #if canImport(AppKit) && !targetEnvironment(macCatalyst)
     /// Builds the image task request and sets it to a button.