@@ -103,22 +103,10 @@ public struct WebImage : View {
103
103
104
104
public var body : some View {
105
105
return Group {
106
+ // Render Logic
106
107
if imageManager. image != nil && imageModel. url == imageManager. currentURL {
107
108
if isAnimating && !imageManager. isIncremental {
108
109
setupPlayer ( )
109
- . onDisappear {
110
- // Only stop the player which is not intermediate status
111
- if !imagePlayer. isWaiting {
112
- if self . imageConfiguration. pausable {
113
- self . imagePlayer. pausePlaying ( )
114
- } else {
115
- self . imagePlayer. stopPlaying ( )
116
- }
117
- if self . imageConfiguration. purgeable {
118
- self . imagePlayer. clearFrameBuffer ( )
119
- }
120
- }
121
- }
122
110
} else {
123
111
if let currentFrame = imagePlayer. currentFrame {
124
112
configure ( image: currentFrame)
@@ -127,11 +115,10 @@ public struct WebImage : View {
127
115
}
128
116
}
129
117
} else {
118
+ // Load Logic
130
119
setupPlaceholder ( )
131
120
. onPlatformAppear ( appear: {
132
- self . imageManager. successBlock = self . imageHandler. successBlock
133
- self . imageManager. failureBlock = self . imageHandler. failureBlock
134
- self . imageManager. progressBlock = self . imageHandler. progressBlock
121
+ setupManager ( )
135
122
if ( self . imageManager. error == nil ) {
136
123
// Load remote image when first appear
137
124
self . imageManager. load ( url: imageModel. url, options: imageModel. options, context: imageModel. context)
@@ -201,14 +188,46 @@ public struct WebImage : View {
201
188
}
202
189
}
203
190
191
+ /// Image Manager status
192
+ func setupManager( ) {
193
+ self . imageManager. successBlock = self . imageHandler. successBlock
194
+ self . imageManager. failureBlock = self . imageHandler. failureBlock
195
+ self . imageManager. progressBlock = self . imageHandler. progressBlock
196
+ if imageModel. url != imageManager. currentURL {
197
+ imageManager. cancel ( )
198
+ imageManager. image = nil
199
+ imageManager. imageData = nil
200
+ imageManager. cacheType = . none
201
+ imageManager. error = nil
202
+ imageManager. isIncremental = false
203
+ imageManager. indicatorStatus. isLoading = false
204
+ imageManager. indicatorStatus. progress = 0
205
+ }
206
+ }
207
+
204
208
/// Animated Image Support
205
209
func setupPlayer( ) -> some View {
210
+ let disappearAction = {
211
+ // Only stop the player which is not intermediate status
212
+ if !imagePlayer. isWaiting {
213
+ if self . imageConfiguration. pausable {
214
+ self . imagePlayer. pausePlaying ( )
215
+ } else {
216
+ self . imagePlayer. stopPlaying ( )
217
+ }
218
+ if self . imageConfiguration. purgeable {
219
+ self . imagePlayer. clearFrameBuffer ( )
220
+ }
221
+ }
222
+ }
206
223
if let currentFrame = imagePlayer. currentFrame, imagePlayer. currentAnimatedImage == imageManager. image! {
207
- return configure ( image: currentFrame) . onAppear {
224
+ return configure ( image: currentFrame) . onPlatformAppear ( appear : {
208
225
self . imagePlayer. startPlaying ( )
209
- }
226
+ } , disappear: {
227
+ disappearAction ( )
228
+ } )
210
229
} else {
211
- return configure ( image: imageManager. image!) . onAppear {
230
+ return configure ( image: imageManager. image!) . onPlatformAppear ( appear : {
212
231
self . imagePlayer. stopPlaying ( )
213
232
if let animatedImage = imageManager. image as? PlatformImage & SDAnimatedImageProvider {
214
233
// Clear previous status
@@ -225,7 +244,9 @@ public struct WebImage : View {
225
244
self . imagePlayer. setupPlayer ( animatedImage: animatedImage)
226
245
self . imagePlayer. startPlaying ( )
227
246
}
228
- }
247
+ } , disappear: {
248
+ disappearAction ( )
249
+ } )
229
250
}
230
251
}
231
252
@@ -235,12 +256,12 @@ public struct WebImage : View {
235
256
if let placeholder = placeholder {
236
257
// If use `.delayPlaceholder`, the placeholder is applied after loading failed, hide during loading :)
237
258
if imageModel. options. contains ( . delayPlaceholder) && imageManager. error == nil {
238
- return AnyView ( configure ( image: . empty) )
259
+ return AnyView ( configure ( image: . empty) . id ( UUID ( ) ) ) // UUID to avoid SwiftUI engine cache the status and does not call `onAppear`
239
260
} else {
240
261
return placeholder
241
262
}
242
263
} else {
243
- return AnyView ( configure ( image: . empty) )
264
+ return AnyView ( configure ( image: . empty) . id ( UUID ( ) ) ) // UUID to avoid SwiftUI engine cache the status and does not call `onAppear`
244
265
}
245
266
}
246
267
}
0 commit comments