Skip to content

Commit 5f8a8ac

Browse files
committed
Trying to move the initial state setup before onAppear to fix the watchOS switching url or any other state issue
This maybe a behavior changes, need testing
1 parent 8b26cb7 commit 5f8a8ac

File tree

5 files changed

+68
-126
lines changed

5 files changed

+68
-126
lines changed

SDWebImageSwiftUI.xcodeproj/project.pbxproj

-12
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
3243AFE62AA37EFF0049A43B /* SwiftUICompatibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B79C9428DB40430088C432 /* SwiftUICompatibility.swift */; };
1110
3243AFE72AA37EFF0049A43B /* WebImage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32C43DDE22FD54C600BE87F5 /* WebImage.swift */; };
1211
3243AFE82AA37EFF0049A43B /* ImagePlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32CBA77E25E4D7D800C6A8DC /* ImagePlayer.swift */; };
1312
3243AFE92AA37EFF0049A43B /* ImageViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 326E480923431C0F00C633E9 /* ImageViewWrapper.swift */; };
@@ -26,10 +25,6 @@
2625
326E480C23431C0F00C633E9 /* ImageViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 326E480923431C0F00C633E9 /* ImageViewWrapper.swift */; };
2726
326E480D23431C0F00C633E9 /* ImageViewWrapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 326E480923431C0F00C633E9 /* ImageViewWrapper.swift */; };
2827
329885EE2AA37FCB0071F2BA /* SDWebImage.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 329885ED2AA37FCB0071F2BA /* SDWebImage.framework */; };
29-
32B79C9528DB40430088C432 /* SwiftUICompatibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B79C9428DB40430088C432 /* SwiftUICompatibility.swift */; };
30-
32B79C9628DB40430088C432 /* SwiftUICompatibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B79C9428DB40430088C432 /* SwiftUICompatibility.swift */; };
31-
32B79C9728DB40430088C432 /* SwiftUICompatibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B79C9428DB40430088C432 /* SwiftUICompatibility.swift */; };
32-
32B79C9828DB40430088C432 /* SwiftUICompatibility.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B79C9428DB40430088C432 /* SwiftUICompatibility.swift */; };
3328
32B933E523659A1900BB7CAD /* Transition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B933E423659A1900BB7CAD /* Transition.swift */; };
3429
32B933E623659A1900BB7CAD /* Transition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B933E423659A1900BB7CAD /* Transition.swift */; };
3530
32B933E723659A1900BB7CAD /* Transition.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32B933E423659A1900BB7CAD /* Transition.swift */; };
@@ -83,7 +78,6 @@
8378
326B84812363350C0011BDFB /* Indicator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Indicator.swift; sourceTree = "<group>"; };
8479
326E480923431C0F00C633E9 /* ImageViewWrapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageViewWrapper.swift; sourceTree = "<group>"; };
8580
329885ED2AA37FCB0071F2BA /* SDWebImage.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SDWebImage.framework; path = Carthage/Build/visionOS/SDWebImage.framework; sourceTree = "<group>"; };
86-
32B79C9428DB40430088C432 /* SwiftUICompatibility.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftUICompatibility.swift; sourceTree = "<group>"; };
8781
32B933E423659A1900BB7CAD /* Transition.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Transition.swift; sourceTree = "<group>"; };
8882
32C43DCC22FD540D00BE87F5 /* SDWebImageSwiftUI.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SDWebImageSwiftUI.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8983
32C43DDC22FD54C600BE87F5 /* ImageManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageManager.swift; sourceTree = "<group>"; };
@@ -221,7 +215,6 @@
221215
32C43DDE22FD54C600BE87F5 /* WebImage.swift */,
222216
32C43DDF22FD54C600BE87F5 /* AnimatedImage.swift */,
223217
32C43E3122FD5DE100BE87F5 /* SDWebImageSwiftUI.swift */,
224-
32B79C9428DB40430088C432 /* SwiftUICompatibility.swift */,
225218
326E480923431C0F00C633E9 /* ImageViewWrapper.swift */,
226219
32D26A012446B546005905DA /* Image.swift */,
227220
);
@@ -491,7 +484,6 @@
491484
3243AFEB2AA37EFF0049A43B /* AnimatedImage.swift in Sources */,
492485
3243AFE82AA37EFF0049A43B /* ImagePlayer.swift in Sources */,
493486
3243AFED2AA37EFF0049A43B /* SDWebImageSwiftUI.swift in Sources */,
494-
3243AFE62AA37EFF0049A43B /* SwiftUICompatibility.swift in Sources */,
495487
3243AFEE2AA37F010049A43B /* Indicator.swift in Sources */,
496488
3243AFEA2AA37EFF0049A43B /* Image.swift in Sources */,
497489
);
@@ -507,7 +499,6 @@
507499
326B84822363350C0011BDFB /* Indicator.swift in Sources */,
508500
32C43E3222FD5DE100BE87F5 /* SDWebImageSwiftUI.swift in Sources */,
509501
326E480A23431C0F00C633E9 /* ImageViewWrapper.swift in Sources */,
510-
32B79C9528DB40430088C432 /* SwiftUICompatibility.swift in Sources */,
511502
32C43E1622FD583700BE87F5 /* ImageManager.swift in Sources */,
512503
32C43E1822FD583700BE87F5 /* AnimatedImage.swift in Sources */,
513504
32D26A022446B546005905DA /* Image.swift in Sources */,
@@ -524,7 +515,6 @@
524515
326B84832363350C0011BDFB /* Indicator.swift in Sources */,
525516
32C43E3322FD5DF400BE87F5 /* SDWebImageSwiftUI.swift in Sources */,
526517
326E480B23431C0F00C633E9 /* ImageViewWrapper.swift in Sources */,
527-
32B79C9628DB40430088C432 /* SwiftUICompatibility.swift in Sources */,
528518
32C43E1922FD583700BE87F5 /* ImageManager.swift in Sources */,
529519
32C43E1B22FD583700BE87F5 /* AnimatedImage.swift in Sources */,
530520
32D26A032446B546005905DA /* Image.swift in Sources */,
@@ -541,7 +531,6 @@
541531
326B84842363350C0011BDFB /* Indicator.swift in Sources */,
542532
32C43E3422FD5DF400BE87F5 /* SDWebImageSwiftUI.swift in Sources */,
543533
326E480C23431C0F00C633E9 /* ImageViewWrapper.swift in Sources */,
544-
32B79C9728DB40430088C432 /* SwiftUICompatibility.swift in Sources */,
545534
32C43E1C22FD583800BE87F5 /* ImageManager.swift in Sources */,
546535
32C43E1E22FD583800BE87F5 /* AnimatedImage.swift in Sources */,
547536
32D26A042446B546005905DA /* Image.swift in Sources */,
@@ -558,7 +547,6 @@
558547
326B84852363350C0011BDFB /* Indicator.swift in Sources */,
559548
32C43E3522FD5DF400BE87F5 /* SDWebImageSwiftUI.swift in Sources */,
560549
326E480D23431C0F00C633E9 /* ImageViewWrapper.swift in Sources */,
561-
32B79C9828DB40430088C432 /* SwiftUICompatibility.swift in Sources */,
562550
32C43E1F22FD583800BE87F5 /* ImageManager.swift in Sources */,
563551
32C43E2122FD583800BE87F5 /* AnimatedImage.swift in Sources */,
564552
32D26A052446B546005905DA /* Image.swift in Sources */,

SDWebImageSwiftUI/Classes/ImageManager.swift

+39-11
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,47 @@ import SDWebImage
1515
@available(iOS 14.0, OSX 11.0, tvOS 14.0, watchOS 7.0, *)
1616
public final class ImageManager : ObservableObject {
1717
/// loaded image, note when progressive loading, this will published multiple times with different partial image
18-
@Published public var image: PlatformImage?
18+
public var image: PlatformImage? {
19+
didSet {
20+
DispatchQueue.main.async {
21+
self.objectWillChange.send()
22+
}
23+
}
24+
}
1925
/// loaded image data, may be nil if hit from memory cache. This will only published once even on incremental image loading
20-
@Published public var imageData: Data?
26+
public var imageData: Data? {
27+
didSet {
28+
DispatchQueue.main.async {
29+
self.objectWillChange.send()
30+
}
31+
}
32+
}
2133
/// loaded image cache type, .none means from network
22-
@Published public var cacheType: SDImageCacheType = .none
34+
public var cacheType: SDImageCacheType = .none {
35+
didSet {
36+
DispatchQueue.main.async {
37+
self.objectWillChange.send()
38+
}
39+
}
40+
}
2341
/// loading error, you can grab the error code and reason listed in `SDWebImageErrorDomain`, to provide a user interface about the error reason
24-
@Published public var error: Error?
42+
public var error: Error? {
43+
didSet {
44+
DispatchQueue.main.async {
45+
self.objectWillChange.send()
46+
}
47+
}
48+
}
2549
/// true means during incremental loading
26-
@Published public var isIncremental: Bool = false
50+
public var isIncremental: Bool = false {
51+
didSet {
52+
DispatchQueue.main.async {
53+
self.objectWillChange.send()
54+
}
55+
}
56+
}
2757
/// A observed object to pass through the image manager loading status to indicator
28-
@Published public var indicatorStatus = IndicatorStatus()
58+
public var indicatorStatus = IndicatorStatus()
2959

3060
weak var currentOperation: SDWebImageOperation? = nil
3161

@@ -51,8 +81,8 @@ public final class ImageManager : ObservableObject {
5181
return
5282
}
5383
currentURL = url
54-
indicatorStatus.isLoading = true
55-
indicatorStatus.progress = 0
84+
self.indicatorStatus.isLoading = true
85+
self.indicatorStatus.progress = 0
5686
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: { [weak self] (receivedSize, expectedSize, _) in
5787
guard let self = self else {
5888
return
@@ -63,9 +93,7 @@ public final class ImageManager : ObservableObject {
6393
} else {
6494
progress = 0
6595
}
66-
DispatchQueue.main.async {
67-
self.indicatorStatus.progress = progress
68-
}
96+
self.indicatorStatus.progress = progress
6997
self.progressBlock?(receivedSize, expectedSize)
7098
}) { [weak self] (image, data, error, cacheType, finished, _) in
7199
guard let self = self else {

SDWebImageSwiftUI/Classes/Indicator/Indicator.swift

+14-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,21 @@ public struct Indicator<T> where T : View {
2828
@available(iOS 14.0, OSX 11.0, tvOS 14.0, watchOS 7.0, *)
2929
public class IndicatorStatus : ObservableObject {
3030
/// whether indicator is loading or not
31-
@Published var isLoading: Bool = false
31+
var isLoading: Bool = false {
32+
didSet {
33+
DispatchQueue.main.async {
34+
self.objectWillChange.send()
35+
}
36+
}
37+
}
3238
/// indicator progress, should only be used for indicator binding, value between [0.0, 1.0]
33-
@Published var progress: Double = 0
39+
var progress: Double = 0 {
40+
didSet {
41+
DispatchQueue.main.async {
42+
self.objectWillChange.send()
43+
}
44+
}
45+
}
3446
}
3547

3648
/// A implementation detail View Modifier with indicator

SDWebImageSwiftUI/Classes/SwiftUICompatibility.swift

-92
This file was deleted.

SDWebImageSwiftUI/Classes/WebImage.swift

+15-9
Original file line numberDiff line numberDiff line change
@@ -163,26 +163,22 @@ public struct WebImage<Content> : View where Content: View {
163163
}
164164
} else {
165165
content((imageManager.error != nil) ? .failure(imageManager.error!) : .empty)
166-
setupPlaceholder()
166+
setupInitialState()
167167
// Load Logic
168-
.onPlatformAppear(appear: {
169-
self.setupManager()
170-
if (self.imageManager.error == nil) {
171-
// Load remote image when first appear
172-
self.imageManager.load(url: imageModel.url, options: imageModel.options, context: imageModel.context)
173-
}
168+
.onAppear {
174169
guard self.imageConfiguration.retryOnAppear else { return }
175170
// When using prorgessive loading, the new partial image will cause onAppear. Filter this case
176171
if self.imageManager.error != nil && !self.imageManager.isIncremental {
177172
self.imageManager.load(url: imageModel.url, options: imageModel.options, context: imageModel.context)
178173
}
179-
}, disappear: {
174+
}
175+
.onDisappear {
180176
guard self.imageConfiguration.cancelOnDisappear else { return }
181177
// When using prorgessive loading, the previous partial image will cause onDisappear. Filter this case
182178
if self.imageManager.error != nil && !self.imageManager.isIncremental {
183179
self.imageManager.cancel()
184180
}
185-
})
181+
}
186182
}
187183
}
188184
}
@@ -328,6 +324,16 @@ public struct WebImage<Content> : View where Content: View {
328324
}
329325
}
330326

327+
/// Initial state management (update when imageModel.url changed)
328+
func setupInitialState() -> some View {
329+
self.setupManager()
330+
if (self.imageManager.error == nil) {
331+
// Load remote image when first appear
332+
self.imageManager.load(url: imageModel.url, options: imageModel.options, context: imageModel.context)
333+
}
334+
return setupPlaceholder()
335+
}
336+
331337
/// Placeholder View Support
332338
func setupPlaceholder() -> some View {
333339
let result = content((imageManager.error != nil) ? .failure(imageManager.error!) : .empty)

0 commit comments

Comments
 (0)