Parcourir la source

Adaptation to multiple platforms

Mx-Iris il y a 2 ans
Parent
commit
2113a62872

+ 4 - 4
Kingfisher.xcodeproj/project.pbxproj

@@ -119,7 +119,7 @@
 		D8FCF6A821C5A0E500F9ABC0 /* RedirectHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = D8FCF6A721C5A0E500F9ABC0 /* RedirectHandler.swift */; };
 		D9638BA61C7DC71F0046523D /* ImagePrefetcherTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9638BA41C7DC71F0046523D /* ImagePrefetcherTests.swift */; };
 		DCEB2842257E4BE100D7A610 /* TVMonogramView+Kingfisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = DCEB2841257E4BE100D7A610 /* TVMonogramView+Kingfisher.swift */; };
-		E9E3ED8B2B1F66B200734CFF /* AppKit+Kingfisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E3ED8A2B1F66B200734CFF /* AppKit+Kingfisher.swift */; };
+		E9E3ED8B2B1F66B200734CFF /* HasImageComponent+Kingfisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = E9E3ED8A2B1F66B200734CFF /* HasImageComponent+Kingfisher.swift */; };
 		F72CE9CE1FCF17ED00CC522A /* ImageModifierTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = F72CE9CD1FCF17ED00CC522A /* ImageModifierTests.swift */; };
 /* End PBXBuildFile section */
 
@@ -296,7 +296,7 @@
 		D8FCF6A721C5A0E500F9ABC0 /* RedirectHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RedirectHandler.swift; sourceTree = "<group>"; };
 		D9638BA41C7DC71F0046523D /* ImagePrefetcherTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImagePrefetcherTests.swift; sourceTree = "<group>"; };
 		DCEB2841257E4BE100D7A610 /* TVMonogramView+Kingfisher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "TVMonogramView+Kingfisher.swift"; sourceTree = "<group>"; };
-		E9E3ED8A2B1F66B200734CFF /* AppKit+Kingfisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "AppKit+Kingfisher.swift"; sourceTree = "<group>"; };
+		E9E3ED8A2B1F66B200734CFF /* HasImageComponent+Kingfisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "HasImageComponent+Kingfisher.swift"; sourceTree = "<group>"; };
 		F72CE9CD1FCF17ED00CC522A /* ImageModifierTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageModifierTests.swift; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
@@ -394,7 +394,7 @@
 				D12EB83B24DD8EFC00329EE1 /* NSTextAttachment+Kingfisher.swift */,
 				D12AB6AC215D2BB50013BA68 /* ImageView+Kingfisher.swift */,
 				D12AB6AD215D2BB50013BA68 /* NSButton+Kingfisher.swift */,
-				E9E3ED8A2B1F66B200734CFF /* AppKit+Kingfisher.swift */,
+				E9E3ED8A2B1F66B200734CFF /* HasImageComponent+Kingfisher.swift */,
 				D12AB6AE215D2BB50013BA68 /* UIButton+Kingfisher.swift */,
 				D12AB6AF215D2BB50013BA68 /* WKInterfaceImage+Kingfisher.swift */,
 				DCEB2841257E4BE100D7A610 /* TVMonogramView+Kingfisher.swift */,
@@ -830,7 +830,7 @@
 				D1AEB09425890DE7008556DF /* ImageBinder.swift in Sources */,
 				D12AB728215D2BB50013BA68 /* String+MD5.swift in Sources */,
 				4B8E2917216F3F7F0095FAD1 /* ImageDownloaderDelegate.swift in Sources */,
-				E9E3ED8B2B1F66B200734CFF /* AppKit+Kingfisher.swift in Sources */,
+				E9E3ED8B2B1F66B200734CFF /* HasImageComponent+Kingfisher.swift in Sources */,
 				D1132C9725919F69003E528D /* KFOptionsSetter.swift in Sources */,
 				D18B3222251852E100662F63 /* KF.swift in Sources */,
 				D12AB704215D2BB50013BA68 /* Kingfisher.swift in Sources */,

+ 21 - 38
Sources/Extensions/AppKit+Kingfisher.swift → Sources/Extensions/HasImageComponent+Kingfisher.swift

@@ -24,12 +24,11 @@
 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 //  THE SOFTWARE.
 
-#if canImport(AppKit) && !targetEnvironment(macCatalyst)
-
 public protocol KingfisherHasImageComponent: KingfisherCompatible {
-    var image: NSImage? { set get }
+    var image: KFCrossPlatformImage? { set get }
 }
 
+#if canImport(AppKit) && !targetEnvironment(macCatalyst)
 import AppKit
 
 @available(macOS 13.0, *)
@@ -54,11 +53,12 @@ extension NSStatusItem: KingfisherHasImageComponent {}
 
 extension NSCell: KingfisherHasImageComponent {}
 
-extension KingfisherWrapper where Base: KingfisherHasImageComponent {
+#endif
 
+extension KingfisherWrapper where Base: KingfisherHasImageComponent {
     // MARK: Setting Image
 
-    /// Sets an image to the button with a source.
+    /// Sets an image to the component with a source.
     ///
     /// - Parameters:
     ///   - source: The `Source` object contains information about how to get the image.
@@ -80,8 +80,8 @@ extension KingfisherWrapper where Base: KingfisherHasImageComponent {
         placeholder: KFCrossPlatformImage? = nil,
         options: KingfisherOptionsInfo? = nil,
         progressBlock: DownloadProgressBlock? = nil,
-        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
-    {
+        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
+    ) -> DownloadTask? {
         let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? .empty))
         return setImage(
             with: source,
@@ -92,7 +92,7 @@ extension KingfisherWrapper where Base: KingfisherHasImageComponent {
         )
     }
 
-    /// Sets an image to the button with a requested resource.
+    /// Sets an image to the component with a requested resource.
     ///
     /// - Parameters:
     ///   - resource: The `Resource` object contains information about the resource.
@@ -114,14 +114,15 @@ extension KingfisherWrapper where Base: KingfisherHasImageComponent {
         placeholder: KFCrossPlatformImage? = nil,
         options: KingfisherOptionsInfo? = nil,
         progressBlock: DownloadProgressBlock? = nil,
-        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
-    {
+        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
+    ) -> DownloadTask? {
         return setImage(
             with: resource?.convertToSource(),
             placeholder: placeholder,
             options: options,
             progressBlock: progressBlock,
-            completionHandler: completionHandler)
+            completionHandler: completionHandler
+        )
     }
 
     func setImage(
@@ -129,10 +130,10 @@ extension KingfisherWrapper where Base: KingfisherHasImageComponent {
         placeholder: KFCrossPlatformImage? = nil,
         parsedOptions: KingfisherParsedOptionsInfo,
         progressBlock: DownloadProgressBlock? = nil,
-        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
-    {
+        completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil
+    ) -> DownloadTask? {
         var mutatingSelf = self
-        guard let source = source else {
+        guard let source else {
             base.image = placeholder
             mutatingSelf.taskIdentifier = nil
             completionHandler?(.failure(KingfisherError.imageSettingError(reason: .emptySource)))
@@ -176,7 +177,7 @@ extension KingfisherWrapper where Base: KingfisherHasImageComponent {
                     mutatingSelf.taskIdentifier = nil
 
                     switch result {
-                    case .success(let value):
+                    case let .success(value):
                         self.base.image = value.image
                         completionHandler?(result)
 
@@ -196,15 +197,15 @@ extension KingfisherWrapper where Base: KingfisherHasImageComponent {
 
     // MARK: Cancelling Downloading Task
 
-    /// Cancels the image download task of the button if it is running.
+    /// Cancels the image download task of the component if it is running.
     /// Nothing will happen if the downloading has already finished.
     public func cancelImageDownloadTask() {
         imageTask?.cancel()
     }
 }
 
-
 // MARK: - Associated Object
+
 private var taskIdentifierKey: Void?
 private var imageTaskKey: Void?
 
@@ -212,9 +213,8 @@ private var alternateTaskIdentifierKey: Void?
 private var alternateImageTaskKey: Void?
 
 extension KingfisherWrapper where Base: KingfisherHasImageComponent {
-
     // MARK: Properties
-    
+
     public private(set) var taskIdentifier: Source.Identifier.Value? {
         get {
             let box: Box<Source.Identifier.Value>? = getAssociatedObject(base, &taskIdentifierKey)
@@ -225,26 +225,9 @@ extension KingfisherWrapper where Base: KingfisherHasImageComponent {
             setRetainedAssociatedObject(base, &taskIdentifierKey, box)
         }
     }
-    
+
     private var imageTask: DownloadTask? {
         get { return getAssociatedObject(base, &imageTaskKey) }
-        set { setRetainedAssociatedObject(base, &imageTaskKey, newValue)}
-    }
-
-    public private(set) var alternateTaskIdentifier: Source.Identifier.Value? {
-        get {
-            let box: Box<Source.Identifier.Value>? = getAssociatedObject(base, &alternateTaskIdentifierKey)
-            return box?.value
-        }
-        set {
-            let box = newValue.map { Box($0) }
-            setRetainedAssociatedObject(base, &alternateTaskIdentifierKey, box)
-        }
-    }
-
-    private var alternateImageTask: DownloadTask? {
-        get { return getAssociatedObject(base, &alternateImageTaskKey) }
-        set { setRetainedAssociatedObject(base, &alternateImageTaskKey, newValue)}
+        set { setRetainedAssociatedObject(base, &imageTaskKey, newValue) }
     }
 }
-#endif