update
This commit is contained in:
245
Pods/Kingfisher/Sources/Extensions/CPListItem+Kingfisher.swift
generated
Normal file
245
Pods/Kingfisher/Sources/Extensions/CPListItem+Kingfisher.swift
generated
Normal file
@@ -0,0 +1,245 @@
|
||||
|
||||
//
|
||||
// CPListItem+Kingfisher.swift
|
||||
// Kingfisher
|
||||
//
|
||||
// Created by Wayne Hartman on 2021-08-29.
|
||||
//
|
||||
// Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#if canImport(CarPlay) && !targetEnvironment(macCatalyst)
|
||||
import CarPlay
|
||||
|
||||
@available(iOS 14.0, *)
|
||||
extension KingfisherWrapper where Base: CPListItem {
|
||||
|
||||
// MARK: Setting Image
|
||||
|
||||
/// Sets an image to the image view with a source.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - source: The `Source` object contains information about the image.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested source
|
||||
/// Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with source: Source?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? []))
|
||||
return setImage(
|
||||
with: source,
|
||||
placeholder: placeholder,
|
||||
parsedOptions: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
/// Sets an image to the image view with a requested resource.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - resource: The `Resource` object contains information about the image.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested resource, from either cache
|
||||
/// or network. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with resource: Resource?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setImage(
|
||||
with: resource?.convertToSource(),
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
func setImage(
|
||||
with source: Source?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
parsedOptions: KingfisherParsedOptionsInfo,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
var mutatingSelf = self
|
||||
guard let source = source else {
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
|
||||
var options = parsedOptions
|
||||
if !options.keepCurrentImageWhileLoading {
|
||||
/**
|
||||
* 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()
|
||||
mutatingSelf.taskIdentifier = issuedIdentifier
|
||||
|
||||
if let block = progressBlock {
|
||||
options.onDataReceived = (options.onDataReceived ?? []) + [ImageLoadingProgressSideEffect(block)]
|
||||
}
|
||||
|
||||
let task = KingfisherManager.shared.retrieveImage(
|
||||
with: source,
|
||||
options: options,
|
||||
downloadTaskUpdated: { mutatingSelf.imageTask = $0 },
|
||||
progressiveImageSetter: { self.base.setImage($0) },
|
||||
referenceTaskIdentifierChecker: { issuedIdentifier == self.taskIdentifier },
|
||||
completionHandler: { result in
|
||||
CallbackQueue.mainCurrentOrAsync.execute {
|
||||
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.setImage(value.image)
|
||||
completionHandler?(result)
|
||||
|
||||
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
|
||||
}
|
||||
completionHandler?(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
mutatingSelf.imageTask = task
|
||||
return task
|
||||
}
|
||||
|
||||
// MARK: Cancelling Image
|
||||
|
||||
/// Cancel the image download task bounded to the image view if it is running.
|
||||
/// Nothing will happen if the downloading has already finished.
|
||||
public func cancelDownloadTask() {
|
||||
imageTask?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
private var taskIdentifierKey: Void?
|
||||
private var imageTaskKey: Void?
|
||||
|
||||
// MARK: Properties
|
||||
extension KingfisherWrapper where Base: CPListItem {
|
||||
|
||||
public private(set) var taskIdentifier: Source.Identifier.Value? {
|
||||
get {
|
||||
let box: Box<Source.Identifier.Value>? = getAssociatedObject(base, &taskIdentifierKey)
|
||||
return box?.value
|
||||
}
|
||||
set {
|
||||
let box = newValue.map { Box($0) }
|
||||
setRetainedAssociatedObject(base, &taskIdentifierKey, box)
|
||||
}
|
||||
}
|
||||
|
||||
private var imageTask: DownloadTask? {
|
||||
get { return getAssociatedObject(base, &imageTaskKey) }
|
||||
set { setRetainedAssociatedObject(base, &imageTaskKey, newValue)}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
537
Pods/Kingfisher/Sources/Extensions/ImageView+Kingfisher.swift
generated
Normal file
537
Pods/Kingfisher/Sources/Extensions/ImageView+Kingfisher.swift
generated
Normal file
@@ -0,0 +1,537 @@
|
||||
//
|
||||
// ImageView+Kingfisher.swift
|
||||
// Kingfisher
|
||||
//
|
||||
// Created by Wei Wang on 15/4/6.
|
||||
//
|
||||
// Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#if !os(watchOS)
|
||||
|
||||
#if os(macOS)
|
||||
import AppKit
|
||||
#else
|
||||
import UIKit
|
||||
#endif
|
||||
|
||||
extension KingfisherWrapper where Base: KFCrossPlatformImageView {
|
||||
|
||||
// MARK: Setting Image
|
||||
|
||||
/// Sets an image to the image view with a `Source`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - source: The `Source` object defines data information from network or a data provider.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
/// This is the easiest way to use Kingfisher to boost the image setting process from a source. Since all parameters
|
||||
/// have a default value except the `source`, you can set an image from a certain URL to an image view like this:
|
||||
///
|
||||
/// ```
|
||||
/// // Set image from a network source.
|
||||
/// let url = URL(string: "https://example.com/image.png")!
|
||||
/// imageView.kf.setImage(with: .network(url))
|
||||
///
|
||||
/// // Or set image from a data provider.
|
||||
/// let provider = LocalFileImageDataProvider(fileURL: fileURL)
|
||||
/// imageView.kf.setImage(with: .provider(provider))
|
||||
/// ```
|
||||
///
|
||||
/// For both `.network` and `.provider` source, there are corresponding view extension methods. So the code
|
||||
/// above is equivalent to:
|
||||
///
|
||||
/// ```
|
||||
/// imageView.kf.setImage(with: url)
|
||||
/// imageView.kf.setImage(with: provider)
|
||||
/// ```
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the source.
|
||||
/// Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with source: Source?,
|
||||
placeholder: Placeholder? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? .empty))
|
||||
return setImage(with: source, placeholder: placeholder, parsedOptions: options, progressBlock: progressBlock, completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
/// Sets an image to the image view with a `Source`.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - source: The `Source` object defines data information from network or a data provider.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
/// This is the easiest way to use Kingfisher to boost the image setting process from a source. Since all parameters
|
||||
/// have a default value except the `source`, you can set an image from a certain URL to an image view like this:
|
||||
///
|
||||
/// ```
|
||||
/// // Set image from a network source.
|
||||
/// let url = URL(string: "https://example.com/image.png")!
|
||||
/// imageView.kf.setImage(with: .network(url))
|
||||
///
|
||||
/// // Or set image from a data provider.
|
||||
/// let provider = LocalFileImageDataProvider(fileURL: fileURL)
|
||||
/// imageView.kf.setImage(with: .provider(provider))
|
||||
/// ```
|
||||
///
|
||||
/// For both `.network` and `.provider` source, there are corresponding view extension methods. So the code
|
||||
/// above is equivalent to:
|
||||
///
|
||||
/// ```
|
||||
/// imageView.kf.setImage(with: url)
|
||||
/// imageView.kf.setImage(with: provider)
|
||||
/// ```
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the source.
|
||||
/// Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// The `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with source: Source?,
|
||||
placeholder: Placeholder? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setImage(
|
||||
with: source,
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: nil,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
/// Sets an image to the image view with a requested resource.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - resource: The `Resource` object contains information about the resource.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
/// This is the easiest way to use Kingfisher to boost the image setting process from network. Since all parameters
|
||||
/// have a default value except the `resource`, you can set an image from a certain URL to an image view like this:
|
||||
///
|
||||
/// ```
|
||||
/// let url = URL(string: "https://example.com/image.png")!
|
||||
/// imageView.kf.setImage(with: url)
|
||||
/// ```
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested resource, from either cache
|
||||
/// or network. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with resource: Resource?,
|
||||
placeholder: Placeholder? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setImage(
|
||||
with: resource?.convertToSource(),
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
/// Sets an image to the image view with a requested resource.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - resource: The `Resource` object contains information about the resource.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
/// This is the easiest way to use Kingfisher to boost the image setting process from network. Since all parameters
|
||||
/// have a default value except the `resource`, you can set an image from a certain URL to an image view like this:
|
||||
///
|
||||
/// ```
|
||||
/// let url = URL(string: "https://example.com/image.png")!
|
||||
/// imageView.kf.setImage(with: url)
|
||||
/// ```
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested resource, from either cache
|
||||
/// or network. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// The `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with resource: Resource?,
|
||||
placeholder: Placeholder? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setImage(
|
||||
with: resource,
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: nil,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
/// Sets an image to the image view with a data provider.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - provider: The `ImageDataProvider` object contains information about the data.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the image data, from either cache
|
||||
/// or the data provider. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with provider: ImageDataProvider?,
|
||||
placeholder: Placeholder? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setImage(
|
||||
with: provider.map { .provider($0) },
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
/// Sets an image to the image view with a data provider.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - provider: The `ImageDataProvider` object contains information about the data.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the image data, from either cache
|
||||
/// or the data provider. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// The `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with provider: ImageDataProvider?,
|
||||
placeholder: Placeholder? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setImage(
|
||||
with: provider,
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: nil,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
func setImage(
|
||||
with source: Source?,
|
||||
placeholder: Placeholder? = nil,
|
||||
parsedOptions: KingfisherParsedOptionsInfo,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
var mutatingSelf = self
|
||||
guard let source = source else {
|
||||
mutatingSelf.placeholder = placeholder
|
||||
mutatingSelf.taskIdentifier = nil
|
||||
completionHandler?(.failure(KingfisherError.imageSettingError(reason: .emptySource)))
|
||||
return nil
|
||||
}
|
||||
|
||||
var options = parsedOptions
|
||||
|
||||
let isEmptyImage = base.image == nil && self.placeholder == nil
|
||||
if !options.keepCurrentImageWhileLoading || isEmptyImage {
|
||||
// Always set placeholder while there is no image/placeholder yet.
|
||||
mutatingSelf.placeholder = placeholder
|
||||
}
|
||||
|
||||
let maybeIndicator = indicator
|
||||
maybeIndicator?.startAnimatingView()
|
||||
|
||||
let issuedIdentifier = Source.Identifier.next()
|
||||
mutatingSelf.taskIdentifier = issuedIdentifier
|
||||
|
||||
if base.shouldPreloadAllAnimation() {
|
||||
options.preloadAllAnimationData = true
|
||||
}
|
||||
|
||||
if let block = progressBlock {
|
||||
options.onDataReceived = (options.onDataReceived ?? []) + [ImageLoadingProgressSideEffect(block)]
|
||||
}
|
||||
|
||||
let task = KingfisherManager.shared.retrieveImage(
|
||||
with: source,
|
||||
options: options,
|
||||
downloadTaskUpdated: { mutatingSelf.imageTask = $0 },
|
||||
progressiveImageSetter: { self.base.image = $0 },
|
||||
referenceTaskIdentifierChecker: { issuedIdentifier == self.taskIdentifier },
|
||||
completionHandler: { result in
|
||||
CallbackQueue.mainCurrentOrAsync.execute {
|
||||
maybeIndicator?.stopAnimatingView()
|
||||
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):
|
||||
guard self.needsTransition(options: options, cacheType: value.cacheType) else {
|
||||
mutatingSelf.placeholder = nil
|
||||
self.base.image = value.image
|
||||
completionHandler?(result)
|
||||
return
|
||||
}
|
||||
|
||||
self.makeTransition(image: value.image, transition: options.transition) {
|
||||
completionHandler?(result)
|
||||
}
|
||||
|
||||
case .failure:
|
||||
if let image = options.onFailureImage {
|
||||
mutatingSelf.placeholder = nil
|
||||
self.base.image = image
|
||||
}
|
||||
completionHandler?(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
mutatingSelf.imageTask = task
|
||||
return task
|
||||
}
|
||||
|
||||
// MARK: Cancelling Downloading Task
|
||||
|
||||
/// Cancels the image download task of the image view if it is running.
|
||||
/// Nothing will happen if the downloading has already finished.
|
||||
public func cancelDownloadTask() {
|
||||
imageTask?.cancel()
|
||||
}
|
||||
|
||||
private func needsTransition(options: KingfisherParsedOptionsInfo, cacheType: CacheType) -> Bool {
|
||||
switch options.transition {
|
||||
case .none:
|
||||
return false
|
||||
#if os(macOS)
|
||||
case .fade: // Fade is only a placeholder for SwiftUI on macOS.
|
||||
return false
|
||||
#else
|
||||
default:
|
||||
if options.forceTransition { return true }
|
||||
if cacheType == .none { return true }
|
||||
return false
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
private func makeTransition(image: KFCrossPlatformImage, transition: ImageTransition, done: @escaping () -> Void) {
|
||||
#if !os(macOS)
|
||||
// Force hiding the indicator without transition first.
|
||||
UIView.transition(
|
||||
with: self.base,
|
||||
duration: 0.0,
|
||||
options: [],
|
||||
animations: { self.indicator?.stopAnimatingView() },
|
||||
completion: { _ in
|
||||
var mutatingSelf = self
|
||||
mutatingSelf.placeholder = nil
|
||||
UIView.transition(
|
||||
with: self.base,
|
||||
duration: transition.duration,
|
||||
options: [transition.animationOptions, .allowUserInteraction],
|
||||
animations: { transition.animations?(self.base, image) },
|
||||
completion: { finished in
|
||||
transition.completion?(finished)
|
||||
done()
|
||||
}
|
||||
)
|
||||
}
|
||||
)
|
||||
#else
|
||||
done()
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Associated Object
|
||||
private var taskIdentifierKey: Void?
|
||||
private var indicatorKey: Void?
|
||||
private var indicatorTypeKey: Void?
|
||||
private var placeholderKey: Void?
|
||||
private var imageTaskKey: Void?
|
||||
|
||||
extension KingfisherWrapper where Base: KFCrossPlatformImageView {
|
||||
|
||||
// MARK: Properties
|
||||
public private(set) var taskIdentifier: Source.Identifier.Value? {
|
||||
get {
|
||||
let box: Box<Source.Identifier.Value>? = getAssociatedObject(base, &taskIdentifierKey)
|
||||
return box?.value
|
||||
}
|
||||
set {
|
||||
let box = newValue.map { Box($0) }
|
||||
setRetainedAssociatedObject(base, &taskIdentifierKey, box)
|
||||
}
|
||||
}
|
||||
|
||||
/// Holds which indicator type is going to be used.
|
||||
/// Default is `.none`, means no indicator will be shown while downloading.
|
||||
public var indicatorType: IndicatorType {
|
||||
get {
|
||||
return getAssociatedObject(base, &indicatorTypeKey) ?? .none
|
||||
}
|
||||
|
||||
set {
|
||||
switch newValue {
|
||||
case .none: indicator = nil
|
||||
case .activity: indicator = ActivityIndicator()
|
||||
case .image(let data): indicator = ImageIndicator(imageData: data)
|
||||
case .custom(let anIndicator): indicator = anIndicator
|
||||
}
|
||||
|
||||
setRetainedAssociatedObject(base, &indicatorTypeKey, newValue)
|
||||
}
|
||||
}
|
||||
|
||||
/// Holds any type that conforms to the protocol `Indicator`.
|
||||
/// The protocol `Indicator` has a `view` property that will be shown when loading an image.
|
||||
/// It will be `nil` if `indicatorType` is `.none`.
|
||||
public private(set) var indicator: Indicator? {
|
||||
get {
|
||||
let box: Box<Indicator>? = getAssociatedObject(base, &indicatorKey)
|
||||
return box?.value
|
||||
}
|
||||
|
||||
set {
|
||||
// Remove previous
|
||||
if let previousIndicator = indicator {
|
||||
previousIndicator.view.removeFromSuperview()
|
||||
}
|
||||
|
||||
// Add new
|
||||
if let newIndicator = newValue {
|
||||
// Set default indicator layout
|
||||
let view = newIndicator.view
|
||||
|
||||
base.addSubview(view)
|
||||
view.translatesAutoresizingMaskIntoConstraints = false
|
||||
view.centerXAnchor.constraint(
|
||||
equalTo: base.centerXAnchor, constant: newIndicator.centerOffset.x).isActive = true
|
||||
view.centerYAnchor.constraint(
|
||||
equalTo: base.centerYAnchor, constant: newIndicator.centerOffset.y).isActive = true
|
||||
|
||||
switch newIndicator.sizeStrategy(in: base) {
|
||||
case .intrinsicSize:
|
||||
break
|
||||
case .full:
|
||||
view.heightAnchor.constraint(equalTo: base.heightAnchor, constant: 0).isActive = true
|
||||
view.widthAnchor.constraint(equalTo: base.widthAnchor, constant: 0).isActive = true
|
||||
case .size(let size):
|
||||
view.heightAnchor.constraint(equalToConstant: size.height).isActive = true
|
||||
view.widthAnchor.constraint(equalToConstant: size.width).isActive = true
|
||||
}
|
||||
|
||||
newIndicator.view.isHidden = true
|
||||
}
|
||||
|
||||
// Save in associated object
|
||||
// Wrap newValue with Box to workaround an issue that Swift does not recognize
|
||||
// and casting protocol for associate object correctly. https://github.com/onevcat/Kingfisher/issues/872
|
||||
setRetainedAssociatedObject(base, &indicatorKey, newValue.map(Box.init))
|
||||
}
|
||||
}
|
||||
|
||||
private var imageTask: DownloadTask? {
|
||||
get { return getAssociatedObject(base, &imageTaskKey) }
|
||||
set { setRetainedAssociatedObject(base, &imageTaskKey, newValue)}
|
||||
}
|
||||
|
||||
/// Represents the `Placeholder` used for this image view. A `Placeholder` will be shown in the view while
|
||||
/// it is downloading an image.
|
||||
public private(set) var placeholder: Placeholder? {
|
||||
get { return getAssociatedObject(base, &placeholderKey) }
|
||||
set {
|
||||
if let previousPlaceholder = placeholder {
|
||||
previousPlaceholder.remove(from: base)
|
||||
}
|
||||
|
||||
if let newPlaceholder = newValue {
|
||||
newPlaceholder.add(to: base)
|
||||
} else {
|
||||
base.image = nil
|
||||
}
|
||||
setRetainedAssociatedObject(base, &placeholderKey, newValue)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension KFCrossPlatformImageView {
|
||||
@objc func shouldPreloadAllAnimation() -> Bool { return true }
|
||||
}
|
||||
|
||||
#endif
|
||||
362
Pods/Kingfisher/Sources/Extensions/NSButton+Kingfisher.swift
generated
Normal file
362
Pods/Kingfisher/Sources/Extensions/NSButton+Kingfisher.swift
generated
Normal file
@@ -0,0 +1,362 @@
|
||||
//
|
||||
// NSButton+Kingfisher.swift
|
||||
// Kingfisher
|
||||
//
|
||||
// Created by Jie Zhang on 14/04/2016.
|
||||
//
|
||||
// Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
|
||||
|
||||
import AppKit
|
||||
|
||||
extension KingfisherWrapper where Base: NSButton {
|
||||
|
||||
// MARK: Setting Image
|
||||
|
||||
/// Sets an image to the button with a source.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - source: The `Source` object contains information about how to get the image.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested source.
|
||||
/// Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with source: Source?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? .empty))
|
||||
return setImage(
|
||||
with: source,
|
||||
placeholder: placeholder,
|
||||
parsedOptions: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
/// Sets an image to the button with a requested resource.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - resource: The `Resource` object contains information about the resource.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested resource, from either cache
|
||||
/// or network. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with resource: Resource?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setImage(
|
||||
with: resource?.convertToSource(),
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
func setImage(
|
||||
with source: Source?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
parsedOptions: KingfisherParsedOptionsInfo,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> 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(
|
||||
with: source,
|
||||
options: options,
|
||||
downloadTaskUpdated: { mutatingSelf.imageTask = $0 },
|
||||
progressiveImageSetter: { self.base.image = $0 },
|
||||
referenceTaskIdentifierChecker: { issuedIdentifier == self.taskIdentifier },
|
||||
completionHandler: { result in
|
||||
CallbackQueue.mainCurrentOrAsync.execute {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
mutatingSelf.imageTask = task
|
||||
return task
|
||||
}
|
||||
|
||||
// MARK: Cancelling Downloading Task
|
||||
|
||||
/// Cancels the image download task of the button if it is running.
|
||||
/// Nothing will happen if the downloading has already finished.
|
||||
public func cancelImageDownloadTask() {
|
||||
imageTask?.cancel()
|
||||
}
|
||||
|
||||
// MARK: Setting Alternate Image
|
||||
|
||||
@discardableResult
|
||||
public func setAlternateImage(
|
||||
with source: Source?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? .empty))
|
||||
return setAlternateImage(
|
||||
with: source,
|
||||
placeholder: placeholder,
|
||||
parsedOptions: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
/// Sets an alternate image to the button with a requested resource.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - resource: The `Resource` object contains information about the resource.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested resource, from either cache
|
||||
/// or network. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setAlternateImage(
|
||||
with resource: Resource?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setAlternateImage(
|
||||
with: resource?.convertToSource(),
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
func setAlternateImage(
|
||||
with source: Source?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
parsedOptions: KingfisherParsedOptionsInfo,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> 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, 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(
|
||||
with: source,
|
||||
options: options,
|
||||
downloadTaskUpdated: { mutatingSelf.alternateImageTask = $0 },
|
||||
completionHandler: { result in
|
||||
CallbackQueue.mainCurrentOrAsync.execute {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
mutatingSelf.alternateImageTask = task
|
||||
return task
|
||||
}
|
||||
|
||||
// MARK: Cancelling Alternate Image Downloading Task
|
||||
|
||||
/// Cancels the alternate image download task of the button if it is running.
|
||||
/// Nothing will happen if the downloading has already finished.
|
||||
public func cancelAlternateImageDownloadTask() {
|
||||
alternateImageTask?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Associated Object
|
||||
private var taskIdentifierKey: Void?
|
||||
private var imageTaskKey: Void?
|
||||
|
||||
private var alternateTaskIdentifierKey: Void?
|
||||
private var alternateImageTaskKey: Void?
|
||||
|
||||
extension KingfisherWrapper where Base: NSButton {
|
||||
|
||||
// MARK: Properties
|
||||
|
||||
public private(set) var taskIdentifier: Source.Identifier.Value? {
|
||||
get {
|
||||
let box: Box<Source.Identifier.Value>? = getAssociatedObject(base, &taskIdentifierKey)
|
||||
return box?.value
|
||||
}
|
||||
set {
|
||||
let box = newValue.map { Box($0) }
|
||||
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)}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
271
Pods/Kingfisher/Sources/Extensions/NSTextAttachment+Kingfisher.swift
generated
Normal file
271
Pods/Kingfisher/Sources/Extensions/NSTextAttachment+Kingfisher.swift
generated
Normal file
@@ -0,0 +1,271 @@
|
||||
//
|
||||
// NSTextAttachment+Kingfisher.swift
|
||||
// Kingfisher
|
||||
//
|
||||
// Created by Benjamin Briggs on 22/07/2019.
|
||||
//
|
||||
// Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#if !os(watchOS)
|
||||
|
||||
#if os(macOS)
|
||||
import AppKit
|
||||
#else
|
||||
import UIKit
|
||||
#endif
|
||||
|
||||
extension KingfisherWrapper where Base: NSTextAttachment {
|
||||
|
||||
// MARK: Setting Image
|
||||
|
||||
/// Sets an image to the text attachment with a source.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - source: The `Source` object defines data information from network or a data provider.
|
||||
/// - attributedView: The owner of the attributed string which this `NSTextAttachment` is added.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested source
|
||||
/// Since this method will perform UI changes, you must call it from the main thread.
|
||||
///
|
||||
/// The retrieved image will be set to `NSTextAttachment.image` property. Because it is not an image view based
|
||||
/// rendering, options related to view, such as `.transition`, are not supported.
|
||||
///
|
||||
/// Kingfisher will call `setNeedsDisplay` on the `attributedView` when the image task done. It gives the view a
|
||||
/// chance to render the attributed string again for displaying the downloaded image. For example, if you set an
|
||||
/// attributed with this `NSTextAttachment` to a `UILabel` object, pass it as the `attributedView` parameter.
|
||||
///
|
||||
/// Here is a typical use case:
|
||||
///
|
||||
/// ```swift
|
||||
/// let attributedText = NSMutableAttributedString(string: "Hello World")
|
||||
/// let textAttachment = NSTextAttachment()
|
||||
///
|
||||
/// textAttachment.kf.setImage(
|
||||
/// with: URL(string: "https://onevcat.com/assets/images/avatar.jpg")!,
|
||||
/// attributedView: label,
|
||||
/// options: [
|
||||
/// .processor(
|
||||
/// ResizingImageProcessor(referenceSize: .init(width: 30, height: 30))
|
||||
/// |> RoundCornerImageProcessor(cornerRadius: 15))
|
||||
/// ]
|
||||
/// )
|
||||
/// attributedText.replaceCharacters(in: NSRange(), with: NSAttributedString(attachment: textAttachment))
|
||||
/// label.attributedText = attributedText
|
||||
/// ```
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with source: Source?,
|
||||
attributedView: @autoclosure @escaping () -> KFCrossPlatformView,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? .empty))
|
||||
return setImage(
|
||||
with: source,
|
||||
attributedView: attributedView,
|
||||
placeholder: placeholder,
|
||||
parsedOptions: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
/// Sets an image to the text attachment with a source.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - resource: The `Resource` object contains information about the resource.
|
||||
/// - attributedView: The owner of the attributed string which this `NSTextAttachment` is added.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested source
|
||||
/// Since this method will perform UI changes, you must call it from the main thread.
|
||||
///
|
||||
/// The retrieved image will be set to `NSTextAttachment.image` property. Because it is not an image view based
|
||||
/// rendering, options related to view, such as `.transition`, are not supported.
|
||||
///
|
||||
/// Kingfisher will call `setNeedsDisplay` on the `attributedView` when the image task done. It gives the view a
|
||||
/// chance to render the attributed string again for displaying the downloaded image. For example, if you set an
|
||||
/// attributed with this `NSTextAttachment` to a `UILabel` object, pass it as the `attributedView` parameter.
|
||||
///
|
||||
/// Here is a typical use case:
|
||||
///
|
||||
/// ```swift
|
||||
/// let attributedText = NSMutableAttributedString(string: "Hello World")
|
||||
/// let textAttachment = NSTextAttachment()
|
||||
///
|
||||
/// textAttachment.kf.setImage(
|
||||
/// with: URL(string: "https://onevcat.com/assets/images/avatar.jpg")!,
|
||||
/// attributedView: label,
|
||||
/// options: [
|
||||
/// .processor(
|
||||
/// ResizingImageProcessor(referenceSize: .init(width: 30, height: 30))
|
||||
/// |> RoundCornerImageProcessor(cornerRadius: 15))
|
||||
/// ]
|
||||
/// )
|
||||
/// attributedText.replaceCharacters(in: NSRange(), with: NSAttributedString(attachment: textAttachment))
|
||||
/// label.attributedText = attributedText
|
||||
/// ```
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with resource: Resource?,
|
||||
attributedView: @autoclosure @escaping () -> KFCrossPlatformView,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? .empty))
|
||||
return setImage(
|
||||
with: resource.map { .network($0) },
|
||||
attributedView: attributedView,
|
||||
placeholder: placeholder,
|
||||
parsedOptions: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
func setImage(
|
||||
with source: Source?,
|
||||
attributedView: @escaping () -> KFCrossPlatformView,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
parsedOptions: KingfisherParsedOptionsInfo,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> 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(
|
||||
with: source,
|
||||
options: options,
|
||||
progressiveImageSetter: { self.base.image = $0 },
|
||||
referenceTaskIdentifierChecker: { issuedIdentifier == self.taskIdentifier },
|
||||
completionHandler: { result in
|
||||
CallbackQueue.mainCurrentOrAsync.execute {
|
||||
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
|
||||
let view = attributedView()
|
||||
#if canImport(UIKit)
|
||||
view.setNeedsDisplay()
|
||||
#else
|
||||
view.setNeedsDisplay(view.bounds)
|
||||
#endif
|
||||
case .failure:
|
||||
if let image = options.onFailureImage {
|
||||
self.base.image = image
|
||||
}
|
||||
}
|
||||
completionHandler?(result)
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
mutatingSelf.imageTask = task
|
||||
return task
|
||||
}
|
||||
|
||||
// MARK: Cancelling Image
|
||||
|
||||
/// Cancel the image download task bounded to the text attachment if it is running.
|
||||
/// Nothing will happen if the downloading has already finished.
|
||||
public func cancelDownloadTask() {
|
||||
imageTask?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
private var taskIdentifierKey: Void?
|
||||
private var imageTaskKey: Void?
|
||||
|
||||
// MARK: Properties
|
||||
extension KingfisherWrapper where Base: NSTextAttachment {
|
||||
|
||||
public private(set) var taskIdentifier: Source.Identifier.Value? {
|
||||
get {
|
||||
let box: Box<Source.Identifier.Value>? = getAssociatedObject(base, &taskIdentifierKey)
|
||||
return box?.value
|
||||
}
|
||||
set {
|
||||
let box = newValue.map { Box($0) }
|
||||
setRetainedAssociatedObject(base, &taskIdentifierKey, box)
|
||||
}
|
||||
}
|
||||
|
||||
private var imageTask: DownloadTask? {
|
||||
get { return getAssociatedObject(base, &imageTaskKey) }
|
||||
set { setRetainedAssociatedObject(base, &imageTaskKey, newValue)}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
209
Pods/Kingfisher/Sources/Extensions/TVMonogramView+Kingfisher.swift
generated
Normal file
209
Pods/Kingfisher/Sources/Extensions/TVMonogramView+Kingfisher.swift
generated
Normal file
@@ -0,0 +1,209 @@
|
||||
//
|
||||
// TVMonogramView+Kingfisher.swift
|
||||
// Kingfisher
|
||||
//
|
||||
// Created by Marvin Nazari on 2020-12-07.
|
||||
//
|
||||
// Copyright (c) 2020 Wei Wang <onevcat@gmail.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
import Foundation
|
||||
|
||||
#if canImport(TVUIKit)
|
||||
|
||||
import TVUIKit
|
||||
|
||||
@available(tvOS 12.0, *)
|
||||
extension KingfisherWrapper where Base: TVMonogramView {
|
||||
|
||||
// MARK: Setting Image
|
||||
|
||||
/// Sets an image to the image view with a source.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - source: The `Source` object contains information about the image.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested source
|
||||
/// Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with source: Source?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? .empty))
|
||||
return setImage(
|
||||
with: source,
|
||||
placeholder: placeholder,
|
||||
parsedOptions: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
func setImage(
|
||||
with source: Source?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
parsedOptions: KingfisherParsedOptionsInfo,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> 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(
|
||||
with: source,
|
||||
options: options,
|
||||
downloadTaskUpdated: { mutatingSelf.imageTask = $0 },
|
||||
progressiveImageSetter: { self.base.image = $0 },
|
||||
referenceTaskIdentifierChecker: { issuedIdentifier == self.taskIdentifier },
|
||||
completionHandler: { result in
|
||||
CallbackQueue.mainCurrentOrAsync.execute {
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
mutatingSelf.imageTask = task
|
||||
return task
|
||||
}
|
||||
|
||||
/// Sets an image to the image view with a requested resource.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - resource: The `Resource` object contains information about the image.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested resource, from either cache
|
||||
/// or network. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with resource: Resource?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setImage(
|
||||
with: resource?.convertToSource(),
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
// MARK: Cancelling Image
|
||||
|
||||
/// Cancel the image download task bounded to the image view if it is running.
|
||||
/// Nothing will happen if the downloading has already finished.
|
||||
public func cancelDownloadTask() {
|
||||
imageTask?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
private var taskIdentifierKey: Void?
|
||||
private var imageTaskKey: Void?
|
||||
|
||||
// MARK: Properties
|
||||
@available(tvOS 12.0, *)
|
||||
extension KingfisherWrapper where Base: TVMonogramView {
|
||||
|
||||
public private(set) var taskIdentifier: Source.Identifier.Value? {
|
||||
get {
|
||||
let box: Box<Source.Identifier.Value>? = getAssociatedObject(base, &taskIdentifierKey)
|
||||
return box?.value
|
||||
}
|
||||
set {
|
||||
let box = newValue.map { Box($0) }
|
||||
setRetainedAssociatedObject(base, &taskIdentifierKey, box)
|
||||
}
|
||||
}
|
||||
|
||||
private var imageTask: DownloadTask? {
|
||||
get { return getAssociatedObject(base, &imageTaskKey) }
|
||||
set { setRetainedAssociatedObject(base, &imageTaskKey, newValue)}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
400
Pods/Kingfisher/Sources/Extensions/UIButton+Kingfisher.swift
generated
Normal file
400
Pods/Kingfisher/Sources/Extensions/UIButton+Kingfisher.swift
generated
Normal file
@@ -0,0 +1,400 @@
|
||||
//
|
||||
// UIButton+Kingfisher.swift
|
||||
// Kingfisher
|
||||
//
|
||||
// Created by Wei Wang on 15/4/13.
|
||||
//
|
||||
// Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#if !os(watchOS)
|
||||
|
||||
#if canImport(UIKit)
|
||||
import UIKit
|
||||
|
||||
extension KingfisherWrapper where Base: UIButton {
|
||||
|
||||
// MARK: Setting Image
|
||||
/// Sets an image to the button for a specified state with a source.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - source: The `Source` object contains information about the image.
|
||||
/// - state: The button state to which the image should be set.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested source, from either cache
|
||||
/// or network. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with source: Source?,
|
||||
for state: UIControl.State,
|
||||
placeholder: UIImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? .empty))
|
||||
return setImage(
|
||||
with: source,
|
||||
for: state,
|
||||
placeholder: placeholder,
|
||||
parsedOptions: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
/// Sets an image to the button for a specified state with a requested resource.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - resource: The `Resource` object contains information about the resource.
|
||||
/// - state: The button state to which the image should be set.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested resource, from either cache
|
||||
/// or network. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with resource: Resource?,
|
||||
for state: UIControl.State,
|
||||
placeholder: UIImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setImage(
|
||||
with: resource?.convertToSource(),
|
||||
for: state,
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with source: Source?,
|
||||
for state: UIControl.State,
|
||||
placeholder: UIImage? = nil,
|
||||
parsedOptions: KingfisherParsedOptionsInfo,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
guard let source = source else {
|
||||
base.setImage(placeholder, for: state)
|
||||
setTaskIdentifier(nil, for: state)
|
||||
completionHandler?(.failure(KingfisherError.imageSettingError(reason: .emptySource)))
|
||||
return nil
|
||||
}
|
||||
|
||||
var options = parsedOptions
|
||||
if !options.keepCurrentImageWhileLoading {
|
||||
base.setImage(placeholder, for: state)
|
||||
}
|
||||
|
||||
var mutatingSelf = self
|
||||
let issuedIdentifier = Source.Identifier.next()
|
||||
setTaskIdentifier(issuedIdentifier, for: state)
|
||||
|
||||
if let block = progressBlock {
|
||||
options.onDataReceived = (options.onDataReceived ?? []) + [ImageLoadingProgressSideEffect(block)]
|
||||
}
|
||||
|
||||
let task = KingfisherManager.shared.retrieveImage(
|
||||
with: source,
|
||||
options: options,
|
||||
downloadTaskUpdated: { mutatingSelf.imageTask = $0 },
|
||||
progressiveImageSetter: { self.base.setImage($0, for: state) },
|
||||
referenceTaskIdentifierChecker: { issuedIdentifier == self.taskIdentifier(for: state) },
|
||||
completionHandler: { result in
|
||||
CallbackQueue.mainCurrentOrAsync.execute {
|
||||
guard issuedIdentifier == self.taskIdentifier(for: state) 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.setTaskIdentifier(nil, for: state)
|
||||
|
||||
switch result {
|
||||
case .success(let value):
|
||||
self.base.setImage(value.image, for: state)
|
||||
completionHandler?(result)
|
||||
|
||||
case .failure:
|
||||
if let image = options.onFailureImage {
|
||||
self.base.setImage(image, for: state)
|
||||
}
|
||||
completionHandler?(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
mutatingSelf.imageTask = task
|
||||
return task
|
||||
}
|
||||
|
||||
// MARK: Cancelling Downloading Task
|
||||
|
||||
/// Cancels the image download task of the button if it is running.
|
||||
/// Nothing will happen if the downloading has already finished.
|
||||
public func cancelImageDownloadTask() {
|
||||
imageTask?.cancel()
|
||||
}
|
||||
|
||||
// MARK: Setting Background Image
|
||||
|
||||
/// Sets a background image to the button for a specified state with a source.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - source: The `Source` object contains information about the image.
|
||||
/// - state: The button state to which the image should be set.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested source
|
||||
/// Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setBackgroundImage(
|
||||
with source: Source?,
|
||||
for state: UIControl.State,
|
||||
placeholder: UIImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? .empty))
|
||||
return setBackgroundImage(
|
||||
with: source,
|
||||
for: state,
|
||||
placeholder: placeholder,
|
||||
parsedOptions: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
/// Sets a background image to the button for a specified state with a requested resource.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - resource: The `Resource` object contains information about the resource.
|
||||
/// - state: The button state to which the image should be set.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested resource, from either cache
|
||||
/// or network. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setBackgroundImage(
|
||||
with resource: Resource?,
|
||||
for state: UIControl.State,
|
||||
placeholder: UIImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setBackgroundImage(
|
||||
with: resource?.convertToSource(),
|
||||
for: state,
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
func setBackgroundImage(
|
||||
with source: Source?,
|
||||
for state: UIControl.State,
|
||||
placeholder: UIImage? = nil,
|
||||
parsedOptions: KingfisherParsedOptionsInfo,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
guard let source = source else {
|
||||
base.setBackgroundImage(placeholder, for: state)
|
||||
setBackgroundTaskIdentifier(nil, for: state)
|
||||
completionHandler?(.failure(KingfisherError.imageSettingError(reason: .emptySource)))
|
||||
return nil
|
||||
}
|
||||
|
||||
var options = parsedOptions
|
||||
if !options.keepCurrentImageWhileLoading {
|
||||
base.setBackgroundImage(placeholder, for: state)
|
||||
}
|
||||
|
||||
var mutatingSelf = self
|
||||
let issuedIdentifier = Source.Identifier.next()
|
||||
setBackgroundTaskIdentifier(issuedIdentifier, for: state)
|
||||
|
||||
if let block = progressBlock {
|
||||
options.onDataReceived = (options.onDataReceived ?? []) + [ImageLoadingProgressSideEffect(block)]
|
||||
}
|
||||
|
||||
let task = KingfisherManager.shared.retrieveImage(
|
||||
with: source,
|
||||
options: options,
|
||||
downloadTaskUpdated: { mutatingSelf.backgroundImageTask = $0 },
|
||||
progressiveImageSetter: { self.base.setBackgroundImage($0, for: state) },
|
||||
referenceTaskIdentifierChecker: { issuedIdentifier == self.backgroundTaskIdentifier(for: state) },
|
||||
completionHandler: { result in
|
||||
CallbackQueue.mainCurrentOrAsync.execute {
|
||||
guard issuedIdentifier == self.backgroundTaskIdentifier(for: state) 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.backgroundImageTask = nil
|
||||
mutatingSelf.setBackgroundTaskIdentifier(nil, for: state)
|
||||
|
||||
switch result {
|
||||
case .success(let value):
|
||||
self.base.setBackgroundImage(value.image, for: state)
|
||||
completionHandler?(result)
|
||||
|
||||
case .failure:
|
||||
if let image = options.onFailureImage {
|
||||
self.base.setBackgroundImage(image, for: state)
|
||||
}
|
||||
completionHandler?(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
mutatingSelf.backgroundImageTask = task
|
||||
return task
|
||||
}
|
||||
|
||||
// MARK: Cancelling Background Downloading Task
|
||||
|
||||
/// Cancels the background image download task of the button if it is running.
|
||||
/// Nothing will happen if the downloading has already finished.
|
||||
public func cancelBackgroundImageDownloadTask() {
|
||||
backgroundImageTask?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// MARK: - Associated Object
|
||||
private var taskIdentifierKey: Void?
|
||||
private var imageTaskKey: Void?
|
||||
|
||||
// MARK: Properties
|
||||
extension KingfisherWrapper where Base: UIButton {
|
||||
|
||||
private typealias TaskIdentifier = Box<[UInt: Source.Identifier.Value]>
|
||||
|
||||
public func taskIdentifier(for state: UIControl.State) -> Source.Identifier.Value? {
|
||||
return taskIdentifierInfo.value[state.rawValue]
|
||||
}
|
||||
|
||||
private func setTaskIdentifier(_ identifier: Source.Identifier.Value?, for state: UIControl.State) {
|
||||
taskIdentifierInfo.value[state.rawValue] = identifier
|
||||
}
|
||||
|
||||
private var taskIdentifierInfo: TaskIdentifier {
|
||||
return getAssociatedObject(base, &taskIdentifierKey) ?? {
|
||||
setRetainedAssociatedObject(base, &taskIdentifierKey, $0)
|
||||
return $0
|
||||
} (TaskIdentifier([:]))
|
||||
}
|
||||
|
||||
private var imageTask: DownloadTask? {
|
||||
get { return getAssociatedObject(base, &imageTaskKey) }
|
||||
set { setRetainedAssociatedObject(base, &imageTaskKey, newValue)}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private var backgroundTaskIdentifierKey: Void?
|
||||
private var backgroundImageTaskKey: Void?
|
||||
|
||||
// MARK: Background Properties
|
||||
extension KingfisherWrapper where Base: UIButton {
|
||||
|
||||
public func backgroundTaskIdentifier(for state: UIControl.State) -> Source.Identifier.Value? {
|
||||
return backgroundTaskIdentifierInfo.value[state.rawValue]
|
||||
}
|
||||
|
||||
private func setBackgroundTaskIdentifier(_ identifier: Source.Identifier.Value?, for state: UIControl.State) {
|
||||
backgroundTaskIdentifierInfo.value[state.rawValue] = identifier
|
||||
}
|
||||
|
||||
private var backgroundTaskIdentifierInfo: TaskIdentifier {
|
||||
return getAssociatedObject(base, &backgroundTaskIdentifierKey) ?? {
|
||||
setRetainedAssociatedObject(base, &backgroundTaskIdentifierKey, $0)
|
||||
return $0
|
||||
} (TaskIdentifier([:]))
|
||||
}
|
||||
|
||||
private var backgroundImageTask: DownloadTask? {
|
||||
get { return getAssociatedObject(base, &backgroundImageTaskKey) }
|
||||
mutating set { setRetainedAssociatedObject(base, &backgroundImageTaskKey, newValue) }
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
204
Pods/Kingfisher/Sources/Extensions/WKInterfaceImage+Kingfisher.swift
generated
Normal file
204
Pods/Kingfisher/Sources/Extensions/WKInterfaceImage+Kingfisher.swift
generated
Normal file
@@ -0,0 +1,204 @@
|
||||
//
|
||||
// WKInterfaceImage+Kingfisher.swift
|
||||
// Kingfisher
|
||||
//
|
||||
// Created by Rodrigo Borges Soares on 04/05/18.
|
||||
//
|
||||
// Copyright (c) 2019 Wei Wang <onevcat@gmail.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
#if canImport(WatchKit)
|
||||
|
||||
import WatchKit
|
||||
|
||||
extension KingfisherWrapper where Base: WKInterfaceImage {
|
||||
|
||||
// MARK: Setting Image
|
||||
|
||||
/// Sets an image to the image view with a source.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - source: The `Source` object contains information about the image.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested source
|
||||
/// Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with source: Source?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
let options = KingfisherParsedOptionsInfo(KingfisherManager.shared.defaultOptions + (options ?? .empty))
|
||||
return setImage(
|
||||
with: source,
|
||||
placeholder: placeholder,
|
||||
parsedOptions: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler
|
||||
)
|
||||
}
|
||||
|
||||
/// Sets an image to the image view with a requested resource.
|
||||
///
|
||||
/// - Parameters:
|
||||
/// - resource: The `Resource` object contains information about the image.
|
||||
/// - placeholder: A placeholder to show while retrieving the image from the given `resource`.
|
||||
/// - options: An options set to define image setting behaviors. See `KingfisherOptionsInfo` for more.
|
||||
/// - progressBlock: Called when the image downloading progress gets updated. If the response does not contain an
|
||||
/// `expectedContentLength`, this block will not be called.
|
||||
/// - completionHandler: Called when the image retrieved and set finished.
|
||||
/// - Returns: A task represents the image downloading.
|
||||
///
|
||||
/// - Note:
|
||||
///
|
||||
/// Internally, this method will use `KingfisherManager` to get the requested resource, from either cache
|
||||
/// or network. Since this method will perform UI changes, you must call it from the main thread.
|
||||
/// Both `progressBlock` and `completionHandler` will be also executed in the main thread.
|
||||
///
|
||||
@discardableResult
|
||||
public func setImage(
|
||||
with resource: Resource?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
options: KingfisherOptionsInfo? = nil,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
return setImage(
|
||||
with: resource?.convertToSource(),
|
||||
placeholder: placeholder,
|
||||
options: options,
|
||||
progressBlock: progressBlock,
|
||||
completionHandler: completionHandler)
|
||||
}
|
||||
|
||||
func setImage(
|
||||
with source: Source?,
|
||||
placeholder: KFCrossPlatformImage? = nil,
|
||||
parsedOptions: KingfisherParsedOptionsInfo,
|
||||
progressBlock: DownloadProgressBlock? = nil,
|
||||
completionHandler: ((Result<RetrieveImageResult, KingfisherError>) -> Void)? = nil) -> DownloadTask?
|
||||
{
|
||||
var mutatingSelf = self
|
||||
guard let source = source else {
|
||||
base.setImage(placeholder)
|
||||
mutatingSelf.taskIdentifier = nil
|
||||
completionHandler?(.failure(KingfisherError.imageSettingError(reason: .emptySource)))
|
||||
return nil
|
||||
}
|
||||
|
||||
var options = parsedOptions
|
||||
if !options.keepCurrentImageWhileLoading {
|
||||
base.setImage(placeholder)
|
||||
}
|
||||
|
||||
let issuedIdentifier = Source.Identifier.next()
|
||||
mutatingSelf.taskIdentifier = issuedIdentifier
|
||||
|
||||
if let block = progressBlock {
|
||||
options.onDataReceived = (options.onDataReceived ?? []) + [ImageLoadingProgressSideEffect(block)]
|
||||
}
|
||||
|
||||
let task = KingfisherManager.shared.retrieveImage(
|
||||
with: source,
|
||||
options: options,
|
||||
downloadTaskUpdated: { mutatingSelf.imageTask = $0 },
|
||||
progressiveImageSetter: { self.base.setImage($0) },
|
||||
referenceTaskIdentifierChecker: { issuedIdentifier == self.taskIdentifier },
|
||||
completionHandler: { result in
|
||||
CallbackQueue.mainCurrentOrAsync.execute {
|
||||
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.setImage(value.image)
|
||||
completionHandler?(result)
|
||||
|
||||
case .failure:
|
||||
if let image = options.onFailureImage {
|
||||
self.base.setImage(image)
|
||||
}
|
||||
completionHandler?(result)
|
||||
}
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
mutatingSelf.imageTask = task
|
||||
return task
|
||||
}
|
||||
|
||||
// MARK: Cancelling Image
|
||||
|
||||
/// Cancel the image download task bounded to the image view if it is running.
|
||||
/// Nothing will happen if the downloading has already finished.
|
||||
public func cancelDownloadTask() {
|
||||
imageTask?.cancel()
|
||||
}
|
||||
}
|
||||
|
||||
private var taskIdentifierKey: Void?
|
||||
private var imageTaskKey: Void?
|
||||
|
||||
// MARK: Properties
|
||||
extension KingfisherWrapper where Base: WKInterfaceImage {
|
||||
|
||||
public private(set) var taskIdentifier: Source.Identifier.Value? {
|
||||
get {
|
||||
let box: Box<Source.Identifier.Value>? = getAssociatedObject(base, &taskIdentifierKey)
|
||||
return box?.value
|
||||
}
|
||||
set {
|
||||
let box = newValue.map { Box($0) }
|
||||
setRetainedAssociatedObject(base, &taskIdentifierKey, box)
|
||||
}
|
||||
}
|
||||
|
||||
private var imageTask: DownloadTask? {
|
||||
get { return getAssociatedObject(base, &imageTaskKey) }
|
||||
set { setRetainedAssociatedObject(base, &imageTaskKey, newValue)}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user