Skip to content

Commit 2b510dc

Browse files
committed
Code garden with Xcode 12's if let syntax in function builder
1 parent d17d44e commit 2b510dc

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

SDWebImageSwiftUI/Classes/ImagePlayer.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public final class ImagePlayer : ObservableObject {
7878
player?.clearFrameBuffer()
7979
}
8080

81-
/// Setup the player using Animated Image
81+
/// Setup the player using Animated Image.
82+
/// After setup, you can always check `isValid` status, or call `startPlaying` to play the animation.
8283
/// - Parameter image: animated image
8384
public func setupPlayer(animatedImage: SDAnimatedImageProvider) {
8485
if isValid {
@@ -104,8 +105,6 @@ public final class ImagePlayer : ObservableObject {
104105
imagePlayer.playbackMode = playbackMode
105106

106107
self.player = imagePlayer
107-
108-
imagePlayer.startPlaying()
109108
}
110109
}
111110
}

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public struct WebImage : View {
1717
var placeholder: AnyView?
1818
var retryOnAppear: Bool = true
1919
var cancelOnDisappear: Bool = true
20+
var pausable: Bool = true
21+
var purgeable: Bool = false
2022

2123
@ObservedObject var imageManager: ImageManager
2224

@@ -26,9 +28,6 @@ public struct WebImage : View {
2628

2729
@ObservedObject var imagePlayer: ImagePlayer
2830

29-
var pausable: Bool = true
30-
var purgeable: Bool = false
31-
3231
/// Create a web image with url, placeholder, custom options and context.
3332
/// - Parameter url: The image url
3433
/// - Parameter options: The options to use when downloading the image. See `SDWebImageOptions` for the possible values.
@@ -61,35 +60,28 @@ public struct WebImage : View {
6160
imageManager.load()
6261
}
6362
return Group {
64-
if imageManager.image != nil {
63+
if let image = imageManager.image {
6564
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()
8675
}
87-
}
76+
if self.purgeable {
77+
self.imagePlayer.clearFrameBuffer()
78+
}
79+
})
8880
} else {
89-
if imagePlayer.currentFrame != nil {
90-
configure(image: imagePlayer.currentFrame!)
81+
if let currentFrame = imagePlayer.currentFrame {
82+
configure(image: currentFrame)
9183
} else {
92-
configure(image: imageManager.image!)
84+
configure(image: image)
9385
}
9486
}
9587
} else {
@@ -165,6 +157,19 @@ public struct WebImage : View {
165157
}
166158
}
167159

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+
168173
/// Placeholder View Support
169174
func setupPlaceholder() -> some View {
170175
// Don't use `Group` because it will trigger `.onAppear` and `.onDisappear` when condition view removed, treat placeholder as an entire component

0 commit comments

Comments
 (0)