Skip to content

Commit

Permalink
Supports to sync status for multiple SDAnimatedImageView who use the …
Browse files Browse the repository at this point in the history
…same image model
  • Loading branch information
dreampiggy committed May 17, 2024
1 parent b7af5e6 commit 16211f6
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 7 deletions.
4 changes: 3 additions & 1 deletion Example/SDWebImageSwiftUI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
archiveVersion = 1;
classes = {
};
objectVersion = 52;
objectVersion = 54;
objects = {

/* Begin PBXBuildFile section */
Expand Down Expand Up @@ -194,6 +194,7 @@
32E529512348A0DF00EA46FF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
32E529542348A0DF00EA46FF /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
32E529562348A0DF00EA46FF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
32F4114B2BF72FE800B4ECB7 /* SDWebImage */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = SDWebImage; path = ../../SDWebImage; sourceTree = "<group>"; };
3E9F8B5F06960FFFBD1A5F99 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
54859B427E0A79E823713963 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -432,6 +433,7 @@
607FACC71AFB9204008FA782 = {
isa = PBXGroup;
children = (
32F4114B2BF72FE800B4ECB7 /* SDWebImage */,
3294617E2AA36761009E391B /* SDWebImageSwiftUI */,
607FACF51AFB993E008FA782 /* Podspec Metadata */,
320CDC2A22FADB44007CF858 /* SDWebImageSwiftUIDemo */,
Expand Down
29 changes: 28 additions & 1 deletion Example/SDWebImageSwiftUIDemo/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,33 @@ class UserSettings: ObservableObject {
#endif
}

struct ContentView: View {
let imageURL = URL(string: "http://apng.onevcat.com/assets/elephant.png")!

var body: some View {
ScrollView {
LazyVStack {
ForEach(1 ... 200, id: \.self) { index in
HStack {
AnimatedImage(url: imageURL)
.customLoopCount(0)
.playbackMode(.normal)
.resizable()
.frame(width: 100, height: 100)
.environment(\.animationGroup, "A")
AnimatedImage(url: imageURL)
.customLoopCount(0)
.playbackMode(.reverse)
.resizable()
.frame(width: 100, height: 100)
.environment(\.animationGroup, "B")
}
}
}
}
}
}

// Test Switching nil url
struct ContentView3: View {
@State var isOn = false
Expand Down Expand Up @@ -107,7 +134,7 @@ struct ContentView2: View {
}
}

struct ContentView: View {
struct ContentView4: View {
@State var imageURLs = [
"http://assets.sbnation.com/assets/2512203/dogflops.gif",
"https://raw.githubusercontent.com/liyong03/YLGIFImage/master/YLGIFImageDemo/YLGIFImageDemo/joy.gif",
Expand Down
11 changes: 6 additions & 5 deletions SDWebImageSwiftUI/Classes/AnimatedImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ public struct AnimatedImage : PlatformViewRepresentable {

func makeView(context: Context) -> AnimatedImageViewWrapper {
let view = AnimatedImageViewWrapper()
if let group = context.environment.animationGroup {
view.wrapped.animationGroup = group
}
if let viewCreateBlock = imageHandler.viewCreateBlock {
viewCreateBlock(view.wrapped, context)
}
Expand Down Expand Up @@ -347,6 +350,9 @@ public struct AnimatedImage : PlatformViewRepresentable {
}

func updateView(_ view: AnimatedImageViewWrapper, context: Context) {
if let group = context.environment.animationGroup {
view.wrapped.animationGroup = group
}
// Refresh image, imageModel is the Source of Truth, switch the type
// Although we have Source of Truth, we can check the previous value, to avoid re-generate SDAnimatedImage, which is performance-cost.
let kind = imageModel.kind
Expand Down Expand Up @@ -384,11 +390,6 @@ public struct AnimatedImage : PlatformViewRepresentable {

static func dismantleView(_ view: AnimatedImageViewWrapper, coordinator: Coordinator) {
view.wrapped.sd_cancelCurrentImageLoad()
#if os(macOS)
view.wrapped.animates = false
#else
view.wrapped.stopAnimating()
#endif
if let viewDestroyBlock = viewDestroyBlock {
viewDestroyBlock(view.wrapped, coordinator)
}
Expand Down
20 changes: 20 additions & 0 deletions SDWebImageSwiftUI/Classes/Environment.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* This file is part of the SDWebImage package.
* (c) DreamPiggy <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import SwiftUI

public struct AnimationGroup: EnvironmentKey {
public static var defaultValue: String? { nil }
}

extension EnvironmentValues {
public var animationGroup: String? {
get { self[AnimationGroup.self] }
set { self[AnimationGroup.self] = newValue }
}
}

0 comments on commit 16211f6

Please sign in to comment.