From 940907a6f93e56b5976618f5ebffc4a5a68a1545 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Mon, 26 Dec 2022 19:13:33 +0800 Subject: [PATCH] Fix the bug that isAnimating control does not works on WebImage --- SDWebImageSwiftUI/Classes/ImagePlayer.swift | 16 ---------------- SDWebImageSwiftUI/Classes/WebImage.swift | 19 ++++++++++++++----- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/SDWebImageSwiftUI/Classes/ImagePlayer.swift b/SDWebImageSwiftUI/Classes/ImagePlayer.swift index 4fdc405..4e61e82 100644 --- a/SDWebImageSwiftUI/Classes/ImagePlayer.swift +++ b/SDWebImageSwiftUI/Classes/ImagePlayer.swift @@ -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 diff --git a/SDWebImageSwiftUI/Classes/WebImage.swift b/SDWebImageSwiftUI/Classes/WebImage.swift index 2290182..65fdf15 100644 --- a/SDWebImageSwiftUI/Classes/WebImage.swift +++ b/SDWebImageSwiftUI/Classes/WebImage.swift @@ -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)