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

Simplify the NSButton set image with generic method

onevcat 1 год назад
Родитель
Сommit
cda2849ffc

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

@@ -362,7 +362,7 @@ extension KingfisherWrapper where Base: KingfisherImageSettable {
     ) -> DownloadTask? {
         return setImage(
             with: source, 
-            imageAccessor: .init(
+            imageAccessor: ImagePropertyAccessor(
                 setImage: { base.setImage($0, options: $1) },
                 getImage: { base.getImage() }
             ),

+ 40 - 128
Sources/Extensions/NSButton+Kingfisher.swift

@@ -108,68 +108,28 @@ extension KingfisherWrapper where Base: NSButton {
     ) -> DownloadTask?
     {
         var mutatingSelf = self
-        guard let source = source else {
-            base.image = placeholder
-            mutatingSelf.taskIdentifier = nil
-            completionHandler?(.failure(KingfisherError.imageSettingError(reason: .emptySource)))
-            return nil
-        }
-
-        var options = parsedOptions
-        if !options.keepCurrentImageWhileLoading {
-            base.image = placeholder
-        }
-
-        let issuedIdentifier = Source.Identifier.next()
-        mutatingSelf.taskIdentifier = issuedIdentifier
-
-        if let block = progressBlock {
-            options.onDataReceived = (options.onDataReceived ?? []) + [ImageLoadingProgressSideEffect(block)]
-        }
-
-        let task = KingfisherManager.shared.retrieveImage(
+        return setImage(
             with: source,
-            options: options,
-            downloadTaskUpdated: { task in
-                Task { @MainActor in mutatingSelf.imageTask = task }
-            },
-            progressiveImageSetter: { self.base.image = $0 },
-            referenceTaskIdentifierChecker: { issuedIdentifier == self.taskIdentifier },
-            completionHandler: { result in
-                CallbackQueueMain.currentOrAsync {
-                    guard issuedIdentifier == self.taskIdentifier else {
-                        let reason: KingfisherError.ImageSettingErrorReason
-                        do {
-                            let value = try result.get()
-                            reason = .notCurrentSourceTask(result: value, error: nil, source: source)
-                        } catch {
-                            reason = .notCurrentSourceTask(result: nil, error: error, source: source)
-                        }
-                        let error = KingfisherError.imageSettingError(reason: reason)
-                        completionHandler?(.failure(error))
-                        return
-                    }
-
-                    mutatingSelf.imageTask = nil
-                    mutatingSelf.taskIdentifier = nil
-
-                    switch result {
-                    case .success(let value):
-                        self.base.image = value.image
-                        completionHandler?(result)
-
-                    case .failure:
-                        if let image = options.onFailureImage {
-                            self.base.image = image
-                        }
-                        completionHandler?(result)
-                    }
-                }
-            }
+            imageAccessor: ImagePropertyAccessor(
+                setImage: { image, _ in
+                    base.image = image
+                }, getImage: {
+                    base.image
+                }),
+            taskAccessor: TaskPropertyAccessor<Void>(
+                setTaskIdentifier: { _, id in
+                    mutatingSelf.taskIdentifier = id
+                },
+                getTaskIdentifier: { _ in
+                    mutatingSelf.taskIdentifier
+                }, setTask: { task in
+                    mutatingSelf.imageTask = task
+                }),
+            placeholder: placeholder,
+            parsedOptions: parsedOptions,
+            progressBlock: progressBlock,
+            completionHandler: completionHandler
         )
-
-        mutatingSelf.imageTask = task
-        return task
     }
 
     // MARK: Cancelling Downloading Task
@@ -242,76 +202,28 @@ extension KingfisherWrapper where Base: NSButton {
     ) -> DownloadTask?
     {
         var mutatingSelf = self
-        guard let source = source else {
-            base.alternateImage = placeholder
-            mutatingSelf.alternateTaskIdentifier = nil
-            completionHandler?(.failure(KingfisherError.imageSettingError(reason: .emptySource)))
-            return nil
-        }
-
-        var options = parsedOptions
-        if !options.keepCurrentImageWhileLoading {
-            base.alternateImage = placeholder
-        }
-
-        let issuedIdentifier = Source.Identifier.next()
-        mutatingSelf.alternateTaskIdentifier = issuedIdentifier
-
-        if let block = progressBlock {
-            options.onDataReceived = (options.onDataReceived ?? []) + [ImageLoadingProgressSideEffect(block)]
-        }
-
-        if let provider = ImageProgressiveProvider(options: options, refresh: { image in
-            self.base.alternateImage = image
-        }) {
-            options.onDataReceived = (options.onDataReceived ?? []) + [provider]
-        }
-
-        options.onDataReceived?.forEach {
-            $0.onShouldApply = { issuedIdentifier == self.alternateTaskIdentifier }
-        }
-
-        let task = KingfisherManager.shared.retrieveImage(
+        return setImage(
             with: source,
-            options: options,
-            downloadTaskUpdated: { task in
-                Task { @MainActor in mutatingSelf.alternateImageTask = task }
-            },
-            completionHandler: { result in
-                CallbackQueueMain.currentOrAsync {
-                    guard issuedIdentifier == self.alternateTaskIdentifier else {
-                        let reason: KingfisherError.ImageSettingErrorReason
-                        do {
-                            let value = try result.get()
-                            reason = .notCurrentSourceTask(result: value, error: nil, source: source)
-                        } catch {
-                            reason = .notCurrentSourceTask(result: nil, error: error, source: source)
-                        }
-                        let error = KingfisherError.imageSettingError(reason: reason)
-                        completionHandler?(.failure(error))
-                        return
-                    }
-
-                    mutatingSelf.alternateImageTask = nil
-                    mutatingSelf.alternateTaskIdentifier = nil
-
-                    switch result {
-                    case .success(let value):
-                        self.base.alternateImage = value.image
-                        completionHandler?(result)
-
-                    case .failure:
-                        if let image = options.onFailureImage {
-                            self.base.alternateImage = image
-                        }
-                        completionHandler?(result)
-                    }
-                }
-            }
+            imageAccessor: ImagePropertyAccessor(
+                setImage: { image, _ in
+                    base.alternateImage = image
+                }, getImage: {
+                    base.alternateImage
+                }),
+            taskAccessor: TaskPropertyAccessor<Void>(
+                setTaskIdentifier: { _, id in
+                    mutatingSelf.alternateTaskIdentifier = id
+                },
+                getTaskIdentifier: { _ in
+                    mutatingSelf.alternateTaskIdentifier
+                }, setTask: { task in
+                    mutatingSelf.alternateImageTask = task
+                }),
+            placeholder: placeholder,
+            parsedOptions: parsedOptions,
+            progressBlock: progressBlock,
+            completionHandler: completionHandler
         )
-
-        mutatingSelf.alternateImageTask = task
-        return task
     }
 
     // MARK: Cancelling Alternate Image Downloading Task