Skip to content

Commit 5bcdaf6

Browse files
committed
Fix the issue when image load success, some bad-written indicator will still exist. Now we remove all the view when finished. Fix the example
1 parent 057eb27 commit 5bcdaf6

File tree

3 files changed

+30
-35
lines changed

3 files changed

+30
-35
lines changed

Example/SDWebImageSwiftUIDemo/DetailView.swift

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,10 @@ import SDWebImageSwiftUI
1212
struct DetailView: View {
1313
let url: String
1414
let animated: Bool
15-
@State var progress: CGFloat = 1
1615
@State var isAnimating: Bool = true
1716

1817
var body: some View {
1918
VStack {
20-
HStack {
21-
ProgressBar(value: $progress)
22-
.foregroundColor(.blue)
23-
.frame(maxHeight: 6)
24-
}
25-
Spacer()
2619
#if os(iOS) || os(tvOS)
2720
if animated {
2821
contentView()
@@ -45,24 +38,22 @@ struct DetailView: View {
4538
contentView()
4639
}
4740
#endif
48-
Spacer()
4941
}
5042
}
5143

5244
func contentView() -> some View {
5345
HStack {
5446
if animated {
47+
#if os(macOS) || os(iOS) || os(tvOS)
5548
AnimatedImage(url: URL(string:url), options: [.progressiveLoad], isAnimating: $isAnimating)
56-
.onProgress { receivedSize, expectedSize in
57-
// SwiftUI engine itself ensure the main queue dispatch
58-
if (expectedSize > 0) {
59-
self.progress = CGFloat(receivedSize) / CGFloat(expectedSize)
60-
} else {
61-
self.progress = 1
62-
}
63-
}
49+
.indicator(SDWebImageProgressIndicator.default)
6450
.resizable()
6551
.scaledToFit()
52+
#else
53+
AnimatedImage(url: URL(string:url), options: [.progressiveLoad], isAnimating: $isAnimating)
54+
.resizable()
55+
.scaledToFit()
56+
#endif
6657
} else {
6758
#if os(macOS) || os(iOS) || os(tvOS)
6859
WebImage(url: URL(string:url), options: [.progressiveLoad])
@@ -73,13 +64,10 @@ struct DetailView: View {
7364
.scaledToFit()
7465
#else
7566
WebImage(url: URL(string:url), options: [.progressiveLoad])
76-
.onProgress { receivedSize, expectedSize in
77-
// SwiftUI engine itself ensure the main queue dispatch
78-
if (expectedSize > 0) {
79-
self.progress = CGFloat(receivedSize) / CGFloat(expectedSize)
80-
} else {
81-
self.progress = 1
82-
}
67+
.indicator { isAnimating, progress in
68+
ProgressBar(value: progress)
69+
.foregroundColor(.blue)
70+
.frame(maxHeight: 6)
8371
}
8472
.resizable()
8573
.scaledToFit()

Example/SDWebImageSwiftUIDemo/ProgressBar.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ public struct ProgressBar: View {
1414

1515
public var body: some View {
1616
GeometryReader { geometry in
17-
ZStack(alignment: .topLeading) {
18-
Capsule()
19-
.frame(width: geometry.size.width)
17+
ZStack(alignment: .leading) {
18+
Rectangle()
19+
.frame(width: geometry.size.width)
2020
.opacity(0.3)
2121
Rectangle()
2222
.frame(width: geometry.size.width * self.value)
23+
.opacity(0.6)
2324
}
2425
}
25-
.clipShape(Capsule())
26-
.opacity(self.value < 1 ? 1 : 0)
26+
.cornerRadius(2)
2727
}
2828
}

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public struct WebImage : View {
2121
@ObservedObject var imageManager: ImageManager
2222
@State var progress: CGFloat = 0
2323
@State var isLoading: Bool = false
24+
var isFinished: Bool {
25+
!isLoading && (imageManager.image != nil)
26+
}
2427

2528
/// Create a web image with url, placeholder, custom options and context.
2629
/// - Parameter url: The image url
@@ -82,12 +85,16 @@ public struct WebImage : View {
8285
#endif
8386
}
8487
if let indicator = indicator {
85-
return AnyView(
86-
ZStack {
87-
view
88-
indicator.builder($isLoading, $progress)
89-
}
90-
)
88+
if isFinished {
89+
return AnyView(view)
90+
} else {
91+
return AnyView(
92+
ZStack {
93+
view
94+
indicator.builder($isLoading, $progress)
95+
}
96+
)
97+
}
9198
} else {
9299
return AnyView(view)
93100
}
@@ -166,7 +173,7 @@ extension WebImage {
166173

167174
/// Associate a indicator when loading image with url
168175
/// - Parameter builder: builder description
169-
/// - Parameter isAnimating: A Binding to control the animation. If image is loading, the value is true, else false.
176+
/// - Parameter isAnimating: A Binding to control the animation. If image is during loading, the value is true, else (like start loading) the value is false.
170177
/// - Parameter progress: A Binding to control the progress during loading. If no progress can be reported, the value is 0.
171178
public func indicator<T>(_ builder: @escaping (_ isAnimating: Binding<Bool>, _ progress: Binding<CGFloat>) -> T) -> WebImage where T : View {
172179
var result = self

0 commit comments

Comments
 (0)