@@ -15,6 +15,9 @@ import SDWebImage
15
15
final class AnimatedImageModel : ObservableObject {
16
16
@Published var image : PlatformImage ?
17
17
@Published var url : URL ?
18
+ @Published var successBlock : ( ( PlatformImage , SDImageCacheType ) -> Void ) ?
19
+ @Published var failureBlock : ( ( Error ) -> Void ) ?
20
+ @Published var progressBlock : ( ( Int , Int ) -> Void ) ?
18
21
}
19
22
20
23
// Layout Binding Object
@@ -67,7 +70,15 @@ public struct AnimatedImage : ViewRepresentable {
67
70
func updateView( _ view: AnimatedImageViewWrapper , context: ViewRepresentableContext < AnimatedImage > ) {
68
71
view. wrapped. image = imageModel. image
69
72
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
+ }
71
82
}
72
83
73
84
layoutView ( view, context: context)
@@ -179,16 +190,6 @@ public struct AnimatedImage : ViewRepresentable {
179
190
#endif
180
191
}
181
192
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
-
192
193
public func resizable(
193
194
capInsets: EdgeInsets = EdgeInsets ( ) ,
194
195
resizingMode: Image . ResizingMode = . stretch) -> AnimatedImage
@@ -236,6 +237,23 @@ public struct AnimatedImage : ViewRepresentable {
236
237
}
237
238
}
238
239
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
+
239
257
extension AnimatedImage {
240
258
public init ( url: URL ? , placeholder: PlatformImage ? = nil , options: SDWebImageOptions = [ ] , context: [ SDWebImageContextOption : Any ] ? = nil ) {
241
259
self . webOptions = options
0 commit comments