Skip to content

Commit e8bb254

Browse files
authored
Merge pull request #42 from SDWebImage/fix_new_solution_for_indicator
Use the only ZStack one state for indicator, instead of removing that indicator view from hierarchy
2 parents af4569f + e7c6931 commit e8bb254

File tree

3 files changed

+11
-15
lines changed

3 files changed

+11
-15
lines changed

SDWebImageSwiftUI/Classes/ImageManager.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import SDWebImage
1111

1212
class ImageManager : ObservableObject {
1313
@Published var image: PlatformImage? // loaded image, note when progressive loading, this will published multiple times with different partial image
14-
@Published var isLoading: Bool = false // whether network is loading or cache is querying
15-
@Published var progress: CGFloat = 0 // network progress
14+
@Published var isLoading: Bool = false // whether network is loading or cache is querying, should only be used for indicator binding
15+
@Published var progress: CGFloat = 0 // network progress, should only be used for indicator binding
1616

1717
var manager = SDWebImageManager.shared
1818
weak var currentOperation: SDWebImageOperation? = nil
19-
var isFinished: Bool = false // true means request end, load() do nothing
19+
var isSuccess: Bool = false // true means request for this URL is ended forever, load() do nothing
2020
var isIncremental: Bool = false // true means during incremental loading
2121

2222
var url: URL?
@@ -70,7 +70,7 @@ class ImageManager : ObservableObject {
7070
self.isLoading = false
7171
self.progress = 1
7272
if let image = image {
73-
self.isFinished = true
73+
self.isSuccess = true
7474
self.successBlock?(image, cacheType)
7575
} else {
7676
self.failureBlock?(error ?? NSError())

SDWebImageSwiftUI/Classes/Indicator/Indicator.swift

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,12 @@ struct IndicatorViewModifier<T> : ViewModifier where T : View {
3232
var indicator: Indicator<T>
3333

3434
func body(content: Content) -> some View {
35-
Group {
36-
if imageManager.isFinished {
37-
// Disable Indiactor
38-
content
35+
ZStack {
36+
content
37+
if imageManager.isLoading {
38+
indicator.content($imageManager.isLoading, $imageManager.progress)
3939
} else {
40-
// Enable indicator
41-
ZStack {
42-
content
43-
indicator.content($imageManager.isLoading, $imageManager.progress)
44-
}
40+
indicator.content($imageManager.isLoading, $imageManager.progress).hidden()
4541
}
4642
}
4743
}

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@ public struct WebImage : View {
5555
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
5656
.onAppear {
5757
guard self.retryOnAppear else { return }
58-
if !self.imageManager.isFinished {
58+
if !self.imageManager.isSuccess {
5959
self.imageManager.load()
6060
}
6161
}
6262
.onDisappear {
6363
guard self.cancelOnDisappear else { return }
6464
// When using prorgessive loading, the previous partial image will cause onDisappear. Filter this case
65-
if self.imageManager.isLoading && !self.imageManager.isIncremental {
65+
if !self.imageManager.isSuccess && !self.imageManager.isIncremental {
6666
self.imageManager.cancel()
6767
}
6868
}

0 commit comments

Comments
 (0)