Skip to content

Commit

Permalink
Merge pull request onevcat#1829 from onevcat/fix/onFailureImage-swiftui
Browse files Browse the repository at this point in the history
Move and apply onFailureImage to common scope
  • Loading branch information
onevcat authored Oct 3, 2021
2 parents eab0df6 + 1438c76 commit 01a8752
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 14 deletions.
13 changes: 0 additions & 13 deletions Sources/General/KF.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,19 +390,6 @@ extension KF.Builder {
return self
}

/// Sets the image that will be used if an image retrieving task fails.
/// - Parameter image: The image that will be used when something goes wrong.
/// - Returns: A `KF.Builder` with changes applied.
///
/// If set and an image retrieving error occurred Kingfisher will set provided image (or empty)
/// in place of requested one. It's useful when you don't want to show placeholder
/// during loading time but wants to use some default image when requests will be failed.
///
public func onFailureImage(_ image: KFCrossPlatformImage?) -> Self {
options.onFailureImage = .some(image)
return self
}

/// Enables progressive image loading with a specified `ImageProgressive` setting to process the
/// progressive JPEG data and display it in a progressive way.
/// - Parameter progressive: The progressive settings which is used while loading.
Expand Down
14 changes: 13 additions & 1 deletion Sources/General/KFOptionsSetter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,12 +326,24 @@ extension KFOptionSetter {

/// Sets whether the image setting for an image view should happen with transition even when retrieved from cache.
/// - Parameter enabled: Enable the force transition or not.
/// - Returns: A `KF.Builder` with changes applied.
/// - Returns: A `Self` with changes applied.
public func forceTransition(_ enabled: Bool = true) -> Self {
options.forceTransition = enabled
return self
}

/// Sets the image that will be used if an image retrieving task fails.
/// - Parameter image: The image that will be used when something goes wrong.
/// - Returns: A `Self` with changes applied.
///
/// If set and an image retrieving error occurred Kingfisher will set provided image (or empty)
/// in place of requested one. It's useful when you don't want to show placeholder
/// during loading time but wants to use some default image when requests will be failed.
///
public func onFailureImage(_ image: KFCrossPlatformImage?) -> Self {
options.onFailureImage = .some(image)
return self
}
}

// MARK: - Request Modifier
Expand Down
7 changes: 7 additions & 0 deletions Sources/SwiftUI/ImageBinder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ extension KFImage {
context.onSuccessDelegate.call(value)
}
case .failure(let error):
CallbackQueue.mainCurrentOrAsync.execute {
if let image = context.options.onFailureImage {
self.loadedImage = image
}
self.loaded = true
}

CallbackQueue.mainAsync.execute {
context.onFailureDelegate.call(error)
}
Expand Down

0 comments on commit 01a8752

Please sign in to comment.