Code garden with Xcode 12's if let syntax in function builder

This commit is contained in:
DreamPiggy 2021-03-10 22:27:41 +08:00
parent d17d44e3c9
commit 2935e2bb0c
2 changed files with 33 additions and 31 deletions

View File

@ -78,7 +78,8 @@ public final class ImagePlayer : ObservableObject {
player?.clearFrameBuffer()
}
/// Setup the player using Animated Image
/// Setup the player using Animated Image.
/// After setup, you can always check `isValid` status, or call `startPlaying` to play the animation.
/// - Parameter image: animated image
public func setupPlayer(animatedImage: SDAnimatedImageProvider) {
if isValid {
@ -104,8 +105,6 @@ public final class ImagePlayer : ObservableObject {
imagePlayer.playbackMode = playbackMode
self.player = imagePlayer
imagePlayer.startPlaying()
}
}
}

View File

@ -17,6 +17,8 @@ public struct WebImage : View {
var placeholder: AnyView?
var retryOnAppear: Bool = true
var cancelOnDisappear: Bool = true
var pausable: Bool = true
var purgeable: Bool = false
@ObservedObject var imageManager: ImageManager
@ -26,9 +28,6 @@ public struct WebImage : View {
@ObservedObject var imagePlayer: ImagePlayer
var pausable: Bool = true
var purgeable: Bool = false
/// Create a web image with url, placeholder, custom options and context.
/// - Parameter url: The image url
/// - Parameter options: The options to use when downloading the image. See `SDWebImageOptions` for the possible values.
@ -61,35 +60,26 @@ public struct WebImage : View {
imageManager.load()
}
return Group {
if imageManager.image != nil {
if let image = imageManager.image {
if isAnimating && !imageManager.isIncremental {
if imagePlayer.currentFrame != nil {
configure(image: imagePlayer.currentFrame!)
.onPlatformAppear(appear: {
self.imagePlayer.startPlaying()
}, disappear: {
if self.pausable {
self.imagePlayer.pausePlaying()
} else {
self.imagePlayer.stopPlaying()
}
if self.purgeable {
self.imagePlayer.clearFrameBuffer()
}
})
} else {
configure(image: imageManager.image!)
.onReceive(imageManager.$image) { image in
if let animatedImage = image as? SDAnimatedImageProvider {
self.imagePlayer.setupPlayer(animatedImage: animatedImage)
}
setupPlayer()
.onPlatformAppear(appear: {
self.imagePlayer.startPlaying()
}, disappear: {
if self.pausable {
self.imagePlayer.pausePlaying()
} else {
self.imagePlayer.stopPlaying()
}
}
if self.purgeable {
self.imagePlayer.clearFrameBuffer()
}
})
} else {
if imagePlayer.currentFrame != nil {
configure(image: imagePlayer.currentFrame!)
if let currentFrame = imagePlayer.currentFrame {
configure(image: currentFrame)
} else {
configure(image: imageManager.image!)
configure(image: image)
}
}
} else {
@ -165,6 +155,19 @@ public struct WebImage : View {
}
}
/// Animated Image Support
func setupPlayer() -> some View {
if let currentFrame = imagePlayer.currentFrame {
return configure(image: currentFrame)
} else {
if let animatedImage = imageManager.image as? SDAnimatedImageProvider {
self.imagePlayer.setupPlayer(animatedImage: animatedImage)
self.imagePlayer.startPlaying()
}
return configure(image: imageManager.image!)
}
}
/// Placeholder View Support
func setupPlaceholder() -> some View {
// Don't use `Group` because it will trigger `.onAppear` and `.onDisappear` when condition view removed, treat placeholder as an entire component