Skip to content

Commit 3704002

Browse files
committed
Add the onSuccess, onFailure, onProgress methods to AnimatedImage/WebImage, make it possible to notify result
1 parent 5606897 commit 3704002

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

SDWebImageSwiftUI/Classes/AnimatedImage.swift

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import SDWebImage
1515
final class AnimatedImageModel : ObservableObject {
1616
@Published var image: PlatformImage?
1717
@Published var url: URL?
18+
@Published var successBlock: ((PlatformImage, SDImageCacheType) -> Void)?
19+
@Published var failureBlock: ((Error) -> Void)?
20+
@Published var progressBlock: ((Int, Int) -> Void)?
1821
}
1922

2023
// Layout Binding Object
@@ -67,7 +70,15 @@ public struct AnimatedImage : ViewRepresentable {
6770
func updateView(_ view: AnimatedImageViewWrapper, context: ViewRepresentableContext<AnimatedImage>) {
6871
view.wrapped.image = imageModel.image
6972
if let url = imageModel.url {
70-
view.wrapped.sd_setImage(with: url, placeholderImage: nil, options: webOptions, context: webContext)
73+
view.wrapped.sd_setImage(with: url, placeholderImage: nil, options: webOptions, context: webContext, progress: { (receivedSize, expectedSize, _) in
74+
self.imageModel.progressBlock?(receivedSize, expectedSize)
75+
}) { (image, error, cacheType, _) in
76+
if let image = image {
77+
self.imageModel.successBlock?(image, cacheType)
78+
} else {
79+
self.imageModel.failureBlock?(error ?? NSError())
80+
}
81+
}
7182
}
7283

7384
layoutView(view, context: context)
@@ -179,16 +190,6 @@ public struct AnimatedImage : ViewRepresentable {
179190
#endif
180191
}
181192

182-
public func image(_ image: PlatformImage?) -> Self {
183-
imageModel.image = image
184-
return self
185-
}
186-
187-
public func imageUrl(_ url: URL?) -> Self {
188-
imageModel.url = url
189-
return self
190-
}
191-
192193
public func resizable(
193194
capInsets: EdgeInsets = EdgeInsets(),
194195
resizingMode: Image.ResizingMode = .stretch) -> AnimatedImage
@@ -236,6 +237,23 @@ public struct AnimatedImage : ViewRepresentable {
236237
}
237238
}
238239

240+
extension AnimatedImage {
241+
public func onFailure(perform action: ((Error) -> Void)? = nil) -> AnimatedImage {
242+
imageModel.failureBlock = action
243+
return self
244+
}
245+
246+
public func onSuccess(perform action: ((PlatformImage, SDImageCacheType) -> Void)? = nil) -> AnimatedImage {
247+
imageModel.successBlock = action
248+
return self
249+
}
250+
251+
public func onProgress(perform action: ((Int, Int) -> Void)? = nil) -> AnimatedImage {
252+
imageModel.progressBlock = action
253+
return self
254+
}
255+
}
256+
239257
extension AnimatedImage {
240258
public init(url: URL?, placeholder: PlatformImage? = nil, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) {
241259
self.webOptions = options

SDWebImageSwiftUI/Classes/ImageManager.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ class ImageManager : ObservableObject {
2626
var url: URL?
2727
var options: SDWebImageOptions
2828
var context: [SDWebImageContextOption : Any]?
29+
var successBlock: ((PlatformImage, SDImageCacheType) -> Void)?
30+
var failureBlock: ((Error) -> Void)?
31+
var progressBlock: ((Int, Int) -> Void)?
2932

3033
init(url: URL?, options: SDWebImageOptions = [], context: [SDWebImageContextOption : Any]? = nil) {
3134
self.url = url
@@ -34,9 +37,17 @@ class ImageManager : ObservableObject {
3437
}
3538

3639
func load() {
37-
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: nil) { (image, data, error, cacheType, _, _) in
40+
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: { [weak self] (receivedSize, expectedSize, _) in
41+
self?.progressBlock?(receivedSize, expectedSize)
42+
}) { [weak self] (image, data, error, cacheType, _, _) in
43+
guard let self = self else {
44+
return
45+
}
3846
if let image = image {
3947
self.image = image
48+
self.successBlock?(image, cacheType)
49+
} else {
50+
self.failureBlock?(error ?? NSError())
4051
}
4152
}
4253
}

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,22 @@ extension WebImage {
8282
}
8383
}
8484

85+
extension WebImage {
86+
public func onFailure(perform action: ((Error) -> Void)? = nil) -> WebImage {
87+
self.imageManager.failureBlock = action
88+
return self
89+
}
90+
91+
public func onSuccess(perform action: ((PlatformImage, SDImageCacheType) -> Void)? = nil) -> WebImage {
92+
self.imageManager.successBlock = action
93+
return self
94+
}
95+
96+
public func onProgress(perform action: ((Int, Int) -> Void)? = nil) -> WebImage {
97+
self.imageManager.progressBlock = action
98+
return self
99+
}
100+
}
85101

86102
#if DEBUG
87103
struct WebImage_Previews : PreviewProvider {

0 commit comments

Comments
 (0)