Merge pull request #251 from SDWebImage/bugfix/is_animating_webimage

Fix the bug that isAnimating control does not works on WebImage
This commit is contained in:
DreamPiggy 2022-12-27 15:43:40 +08:00 committed by GitHub
commit 92f7b1779b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 21 deletions

View File

@ -14,8 +14,6 @@ import SDWebImage
public final class ImagePlayer : ObservableObject {
var player: SDAnimatedImagePlayer?
var waitingPlaying = false
/// Max buffer size
public var maxBufferSize: UInt?
@ -51,14 +49,6 @@ public final class ImagePlayer : ObservableObject {
player != nil
}
/// The player is preparing to resume from previous stop state. This is intermediate status when previous frame disappear and new frame appear
public var isWaiting: Bool {
if let player = player {
return player.isPlaying && waitingPlaying
}
return true
}
/// Current playing status
public var isPlaying: Bool {
player?.isPlaying ?? false
@ -67,12 +57,6 @@ public final class ImagePlayer : ObservableObject {
/// Start the animation
public func startPlaying() {
player?.startPlaying()
waitingPlaying = true
DispatchQueue.main.async {
// This workaround `WebImage` caller
// Which previous frame onDisappear and new frame onAppear, cause player status wrong
self.waitingPlaying = false
}
}
/// Pause the animation

View File

@ -117,11 +117,7 @@ public struct WebImage : View {
if isAnimating && !imageManager.isIncremental {
setupPlayer()
} else {
if let currentFrame = imagePlayer.currentFrame {
configure(image: currentFrame)
} else {
configure(image: imageManager.image!)
}
displayImage()
}
} else {
// Load Logic
@ -231,6 +227,16 @@ public struct WebImage : View {
}
}
/// Static Image Display
func displayImage() -> some View {
disappearAction()
if let currentFrame = imagePlayer.currentFrame {
return configure(image: currentFrame)
} else {
return configure(image: imageManager.image!)
}
}
/// Animated Image Support
func setupPlayer() -> some View {
let shouldResetPlayer: Bool
@ -240,6 +246,9 @@ public struct WebImage : View {
} else {
shouldResetPlayer = false
}
if !shouldResetPlayer {
imagePlayer.startPlaying()
}
if let currentFrame = imagePlayer.currentFrame, !shouldResetPlayer {
// Bind frame index to ID to ensure onDisappear called with sync
return configure(image: currentFrame)