Skip to content

Commit 6590afd

Browse files
committed
Use manager published IndicatorStatus to pass update the indicator
Fix warning
1 parent e1c32ae commit 6590afd

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

SDWebImageSwiftUI/Classes/ImageManager.swift

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@ public final class ImageManager : ObservableObject {
2121
@Published public var cacheType: SDImageCacheType = .none
2222
/// loading error, you can grab the error code and reason listed in `SDWebImageErrorDomain`, to provide a user interface about the error reason
2323
@Published public var error: Error?
24-
/// whether network is loading or cache is querying, should only be used for indicator binding
25-
@Published public var isLoading: Bool = false
26-
/// network progress, should only be used for indicator binding
27-
@Published public var progress: Double = 0
2824
/// true means during incremental loading
2925
@Published public var isIncremental: Bool = false
26+
/// A observed object to pass through the image manager loading status to indicator
27+
@Published public var indicatorStatus = IndicatorStatus()
3028

3129
weak var currentOperation: SDWebImageOperation? = nil
3230

@@ -50,7 +48,7 @@ public final class ImageManager : ObservableObject {
5048
if currentOperation != nil {
5149
return
5250
}
53-
self.isLoading = true
51+
self.indicatorStatus.isLoading = true
5452
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: { [weak self] (receivedSize, expectedSize, _) in
5553
guard let self = self else {
5654
return
@@ -62,7 +60,7 @@ public final class ImageManager : ObservableObject {
6260
progress = 0
6361
}
6462
DispatchQueue.main.async {
65-
self.progress = progress
63+
self.indicatorStatus.progress = progress
6664
}
6765
self.progressBlock?(receivedSize, expectedSize)
6866
}) { [weak self] (image, data, error, cacheType, finished, _) in
@@ -82,8 +80,8 @@ public final class ImageManager : ObservableObject {
8280
if finished {
8381
self.imageData = data
8482
self.cacheType = cacheType
85-
self.isLoading = false
86-
self.progress = 1
83+
self.indicatorStatus.isLoading = false
84+
self.indicatorStatus.progress = 1
8785
if let image = image {
8886
self.successBlock?(image, data, cacheType)
8987
} else {
@@ -98,8 +96,8 @@ public final class ImageManager : ObservableObject {
9896
if let operation = currentOperation {
9997
operation.cancel()
10098
currentOperation = nil
101-
isLoading = false
10299
}
100+
indicatorStatus.isLoading = false
103101
}
104102

105103
}

SDWebImageSwiftUI/Classes/SwiftUICompatibility.swift

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,19 +46,27 @@ class PlatformAppearView: PlatformView {
4646
#if os(iOS) || os(tvOS)
4747
override func willMove(toWindow newWindow: UIWindow?) {
4848
if newWindow != nil {
49-
appearAction()
49+
DispatchQueue.main.async {
50+
self.appearAction()
51+
}
5052
} else {
51-
disappearAction()
53+
DispatchQueue.main.async {
54+
self.disappearAction()
55+
}
5256
}
5357
}
5458
#endif
5559

5660
#if os(macOS)
5761
override func viewWillMove(toWindow newWindow: NSWindow?) {
5862
if newWindow != nil {
59-
appearAction()
63+
DispatchQueue.main.async {
64+
self.appearAction()
65+
}
6066
} else {
61-
disappearAction()
67+
DispatchQueue.main.async {
68+
self.disappearAction()
69+
}
6270
}
6371
}
6472
#endif
@@ -76,7 +84,7 @@ extension View {
7684
/// - Returns: Some view
7785
func onPlatformAppear(appear: @escaping () -> Void = {}, disappear: @escaping () -> Void = {}) -> some View {
7886
#if os(iOS) || os(tvOS) || os(macOS)
79-
return self.background(PlatformAppear(appearAction: appear, disappearAction: disappear))
87+
return self.overlay(PlatformAppear(appearAction: appear, disappearAction: disappear))
8088
#else
8189
return self.onAppear(perform: appear).onDisappear(perform: disappear)
8290
#endif

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ public struct WebImage : View {
6161
/// A observed object to pass through the image configuration to player
6262
@ObservedObject var imageConfiguration = WebImageConfiguration()
6363

64-
/// A observed object to pass through the image manager loading status to indicator
65-
@ObservedObject var indicatorStatus = IndicatorStatus()
66-
6764
@ObservedObject var imagePlayer = ImagePlayer()
6865

6966
// FIXME: Use SwiftUI StateObject and remove onPlatformAppear once drop iOS 13 support
@@ -142,10 +139,7 @@ public struct WebImage : View {
142139
if self.imageManager.image == nil && !self.imageManager.isIncremental {
143140
self.imageManager.cancel()
144141
}
145-
}).onReceive(imageManager.objectWillChange) { _ in
146-
indicatorStatus.isLoading = imageManager.isLoading
147-
indicatorStatus.progress = imageManager.progress
148-
}
142+
})
149143
}
150144
}
151145
}
@@ -225,7 +219,7 @@ public struct WebImage : View {
225219
// Don't use `Group` because it will trigger `.onAppear` and `.onDisappear` when condition view removed, treat placeholder as an entire component
226220
if let placeholder = placeholder {
227221
// If use `.delayPlaceholder`, the placeholder is applied after loading failed, hide during loading :)
228-
if imageModel.webOptions.contains(.delayPlaceholder) && imageManager.isLoading {
222+
if imageModel.webOptions.contains(.delayPlaceholder) && imageManager.indicatorStatus.isLoading {
229223
return AnyView(configure(image: .empty))
230224
} else {
231225
return placeholder
@@ -352,7 +346,7 @@ extension WebImage {
352346
/// Associate a indicator when loading image with url
353347
/// - Parameter indicator: The indicator type, see `Indicator`
354348
public func indicator<T>(_ indicator: Indicator<T>) -> some View where T : View {
355-
return self.modifier(IndicatorViewModifier(status: indicatorStatus, indicator: indicator))
349+
return self.modifier(IndicatorViewModifier(status: imageManager.indicatorStatus, indicator: indicator))
356350
}
357351

358352
/// Associate a indicator when loading image with url, convenient method with block

0 commit comments

Comments
 (0)