Use manager published IndicatorStatus to pass update the indicator
Fix warning
This commit is contained in:
parent
e1c32aea7d
commit
6590afdd3a
|
@ -21,12 +21,10 @@ public final class ImageManager : ObservableObject {
|
|||
@Published public var cacheType: SDImageCacheType = .none
|
||||
/// loading error, you can grab the error code and reason listed in `SDWebImageErrorDomain`, to provide a user interface about the error reason
|
||||
@Published public var error: Error?
|
||||
/// whether network is loading or cache is querying, should only be used for indicator binding
|
||||
@Published public var isLoading: Bool = false
|
||||
/// network progress, should only be used for indicator binding
|
||||
@Published public var progress: Double = 0
|
||||
/// true means during incremental loading
|
||||
@Published public var isIncremental: Bool = false
|
||||
/// A observed object to pass through the image manager loading status to indicator
|
||||
@Published public var indicatorStatus = IndicatorStatus()
|
||||
|
||||
weak var currentOperation: SDWebImageOperation? = nil
|
||||
|
||||
|
@ -50,7 +48,7 @@ public final class ImageManager : ObservableObject {
|
|||
if currentOperation != nil {
|
||||
return
|
||||
}
|
||||
self.isLoading = true
|
||||
self.indicatorStatus.isLoading = true
|
||||
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: { [weak self] (receivedSize, expectedSize, _) in
|
||||
guard let self = self else {
|
||||
return
|
||||
|
@ -62,7 +60,7 @@ public final class ImageManager : ObservableObject {
|
|||
progress = 0
|
||||
}
|
||||
DispatchQueue.main.async {
|
||||
self.progress = progress
|
||||
self.indicatorStatus.progress = progress
|
||||
}
|
||||
self.progressBlock?(receivedSize, expectedSize)
|
||||
}) { [weak self] (image, data, error, cacheType, finished, _) in
|
||||
|
@ -82,8 +80,8 @@ public final class ImageManager : ObservableObject {
|
|||
if finished {
|
||||
self.imageData = data
|
||||
self.cacheType = cacheType
|
||||
self.isLoading = false
|
||||
self.progress = 1
|
||||
self.indicatorStatus.isLoading = false
|
||||
self.indicatorStatus.progress = 1
|
||||
if let image = image {
|
||||
self.successBlock?(image, data, cacheType)
|
||||
} else {
|
||||
|
@ -98,8 +96,8 @@ public final class ImageManager : ObservableObject {
|
|||
if let operation = currentOperation {
|
||||
operation.cancel()
|
||||
currentOperation = nil
|
||||
isLoading = false
|
||||
}
|
||||
indicatorStatus.isLoading = false
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -46,9 +46,13 @@ class PlatformAppearView: PlatformView {
|
|||
#if os(iOS) || os(tvOS)
|
||||
override func willMove(toWindow newWindow: UIWindow?) {
|
||||
if newWindow != nil {
|
||||
appearAction()
|
||||
DispatchQueue.main.async {
|
||||
self.appearAction()
|
||||
}
|
||||
} else {
|
||||
disappearAction()
|
||||
DispatchQueue.main.async {
|
||||
self.disappearAction()
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -56,9 +60,13 @@ class PlatformAppearView: PlatformView {
|
|||
#if os(macOS)
|
||||
override func viewWillMove(toWindow newWindow: NSWindow?) {
|
||||
if newWindow != nil {
|
||||
appearAction()
|
||||
DispatchQueue.main.async {
|
||||
self.appearAction()
|
||||
}
|
||||
} else {
|
||||
disappearAction()
|
||||
DispatchQueue.main.async {
|
||||
self.disappearAction()
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
@ -76,7 +84,7 @@ extension View {
|
|||
/// - Returns: Some view
|
||||
func onPlatformAppear(appear: @escaping () -> Void = {}, disappear: @escaping () -> Void = {}) -> some View {
|
||||
#if os(iOS) || os(tvOS) || os(macOS)
|
||||
return self.background(PlatformAppear(appearAction: appear, disappearAction: disappear))
|
||||
return self.overlay(PlatformAppear(appearAction: appear, disappearAction: disappear))
|
||||
#else
|
||||
return self.onAppear(perform: appear).onDisappear(perform: disappear)
|
||||
#endif
|
||||
|
|
|
@ -61,9 +61,6 @@ public struct WebImage : View {
|
|||
/// A observed object to pass through the image configuration to player
|
||||
@ObservedObject var imageConfiguration = WebImageConfiguration()
|
||||
|
||||
/// A observed object to pass through the image manager loading status to indicator
|
||||
@ObservedObject var indicatorStatus = IndicatorStatus()
|
||||
|
||||
@ObservedObject var imagePlayer = ImagePlayer()
|
||||
|
||||
// FIXME: Use SwiftUI StateObject and remove onPlatformAppear once drop iOS 13 support
|
||||
|
@ -142,10 +139,7 @@ public struct WebImage : View {
|
|||
if self.imageManager.image == nil && !self.imageManager.isIncremental {
|
||||
self.imageManager.cancel()
|
||||
}
|
||||
}).onReceive(imageManager.objectWillChange) { _ in
|
||||
indicatorStatus.isLoading = imageManager.isLoading
|
||||
indicatorStatus.progress = imageManager.progress
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -225,7 +219,7 @@ public struct WebImage : View {
|
|||
// Don't use `Group` because it will trigger `.onAppear` and `.onDisappear` when condition view removed, treat placeholder as an entire component
|
||||
if let placeholder = placeholder {
|
||||
// If use `.delayPlaceholder`, the placeholder is applied after loading failed, hide during loading :)
|
||||
if imageModel.webOptions.contains(.delayPlaceholder) && imageManager.isLoading {
|
||||
if imageModel.webOptions.contains(.delayPlaceholder) && imageManager.indicatorStatus.isLoading {
|
||||
return AnyView(configure(image: .empty))
|
||||
} else {
|
||||
return placeholder
|
||||
|
@ -352,7 +346,7 @@ extension WebImage {
|
|||
/// Associate a indicator when loading image with url
|
||||
/// - Parameter indicator: The indicator type, see `Indicator`
|
||||
public func indicator<T>(_ indicator: Indicator<T>) -> some View where T : View {
|
||||
return self.modifier(IndicatorViewModifier(status: indicatorStatus, indicator: indicator))
|
||||
return self.modifier(IndicatorViewModifier(status: imageManager.indicatorStatus, indicator: indicator))
|
||||
}
|
||||
|
||||
/// Associate a indicator when loading image with url, convenient method with block
|
||||
|
|
Loading…
Reference in New Issue