@@ -17,6 +17,8 @@ public struct WebImage : View {
17
17
var placeholder : AnyView ?
18
18
var retryOnAppear : Bool = true
19
19
var cancelOnDisappear : Bool = true
20
+ var pausable : Bool = true
21
+ var purgeable : Bool = false
20
22
21
23
@ObservedObject var imageManager : ImageManager
22
24
@@ -26,9 +28,6 @@ public struct WebImage : View {
26
28
27
29
@ObservedObject var imagePlayer : ImagePlayer
28
30
29
- var pausable : Bool = true
30
- var purgeable : Bool = false
31
-
32
31
/// Create a web image with url, placeholder, custom options and context.
33
32
/// - Parameter url: The image url
34
33
/// - Parameter options: The options to use when downloading the image. See `SDWebImageOptions` for the possible values.
@@ -61,35 +60,28 @@ public struct WebImage : View {
61
60
imageManager. load ( )
62
61
}
63
62
return Group {
64
- if imageManager . image != nil {
63
+ if let image = imageManager . image {
65
64
if isAnimating && !imageManager. isIncremental {
66
- if imagePlayer. currentFrame != nil {
67
- configure ( image: imagePlayer. currentFrame!)
68
- . onPlatformAppear ( appear: {
69
- self . imagePlayer. startPlaying ( )
70
- } , disappear: {
71
- if self . pausable {
72
- self . imagePlayer. pausePlaying ( )
73
- } else {
74
- self . imagePlayer. stopPlaying ( )
75
- }
76
- if self . purgeable {
77
- self . imagePlayer. clearFrameBuffer ( )
78
- }
79
- } )
80
- } else {
81
- configure ( image: imageManager. image!)
82
- . onReceive ( imageManager. $image) { image in
83
- if let animatedImage = image as? SDAnimatedImageProvider {
84
- self . imagePlayer. setupPlayer ( animatedImage: animatedImage)
85
- }
65
+ setupPlayer ( )
66
+ . onPlatformAppear ( appear: {
67
+ print ( " appear: \( imageManager. url) " )
68
+ self . imagePlayer. startPlaying ( )
69
+ } , disappear: {
70
+ print ( " disappear: \( imageManager. url) " )
71
+ if self . pausable {
72
+ self . imagePlayer. pausePlaying ( )
73
+ } else {
74
+ self . imagePlayer. stopPlaying ( )
86
75
}
87
- }
76
+ if self . purgeable {
77
+ self . imagePlayer. clearFrameBuffer ( )
78
+ }
79
+ } )
88
80
} else {
89
- if imagePlayer . currentFrame != nil {
90
- configure ( image: imagePlayer . currentFrame! )
81
+ if let currentFrame = imagePlayer . currentFrame {
82
+ configure ( image: currentFrame)
91
83
} else {
92
- configure ( image: imageManager . image! )
84
+ configure ( image: image)
93
85
}
94
86
}
95
87
} else {
@@ -165,6 +157,19 @@ public struct WebImage : View {
165
157
}
166
158
}
167
159
160
+ /// Animated Image Support
161
+ func setupPlayer( ) -> some View {
162
+ if let currentFrame = imagePlayer. currentFrame {
163
+ return configure ( image: currentFrame)
164
+ } else {
165
+ if let animatedImage = imageManager. image as? SDAnimatedImageProvider {
166
+ self . imagePlayer. setupPlayer ( animatedImage: animatedImage)
167
+ self . imagePlayer. startPlaying ( )
168
+ }
169
+ return configure ( image: imageManager. image!)
170
+ }
171
+ }
172
+
168
173
/// Placeholder View Support
169
174
func setupPlaceholder( ) -> some View {
170
175
// Don't use `Group` because it will trigger `.onAppear` and `.onDisappear` when condition view removed, treat placeholder as an entire component
0 commit comments