Skip to content

Commit e286ad5

Browse files
authored
Merge pull request #37 from SDWebImage/fix_indicator_create_multiple_times
Fix Indicator ViewBuilder will create multiple times, Fix indicator does not stop on error
2 parents 06ef24d + a00e9cb commit e286ad5

File tree

5 files changed

+62
-3
lines changed

5 files changed

+62
-3
lines changed

Example/SDWebImageSwiftUI.xcodeproj/project.pbxproj

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
32E529682348A10C00EA46FF /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 320CDC2F22FADB44007CF858 /* ContentView.swift */; };
4242
32E529692348A10C00EA46FF /* DetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 326B0D702345C01900D28269 /* DetailView.swift */; };
4343
32E5296A2348A10C00EA46FF /* ProgressBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 321A6BEF2345EC4E00B5BEFC /* ProgressBar.swift */; };
44+
32E7F121236CAAB8001688BC /* ActivityBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32E7F120236CAAB8001688BC /* ActivityBar.swift */; };
45+
32E7F122236CAAB8001688BC /* ActivityBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32E7F120236CAAB8001688BC /* ActivityBar.swift */; };
46+
32E7F123236CAAB8001688BC /* ActivityBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32E7F120236CAAB8001688BC /* ActivityBar.swift */; };
47+
32E7F124236CAAB8001688BC /* ActivityBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 32E7F120236CAAB8001688BC /* ActivityBar.swift */; };
4448
6AE7BEBA0EA1DD0FD6F99D99 /* Pods_SDWebImageSwiftUIDemo_watchOS_WatchKit_Extension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 2B1D3C77C4221915C839DC78 /* Pods_SDWebImageSwiftUIDemo_watchOS_WatchKit_Extension.framework */; };
4549
CECA1658ECBAF54E3FF3EF58 /* Pods_SDWebImageSwiftUIDemo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4A371F81C3B5BD6972F7A0E2 /* Pods_SDWebImageSwiftUIDemo.framework */; };
4650
E8596B8000E7DC96D492333B /* Pods_SDWebImageSwiftUIDemo_tvOS.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 188B93ED6CBDC186E30A49C8 /* Pods_SDWebImageSwiftUIDemo_tvOS.framework */; };
@@ -130,6 +134,7 @@
130134
32E529512348A0DF00EA46FF /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
131135
32E529542348A0DF00EA46FF /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
132136
32E529562348A0DF00EA46FF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
137+
32E7F120236CAAB8001688BC /* ActivityBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ActivityBar.swift; sourceTree = "<group>"; };
133138
33E5ED2426DFF5E06C9A2FAB /* Pods_SDWebImageSwiftUIDemo_macOS.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_SDWebImageSwiftUIDemo_macOS.framework; sourceTree = BUILT_PRODUCTS_DIR; };
134139
3E9F8B5F06960FFFBD1A5F99 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
135140
48BE9486C7BDF4F74C8BA94D /* Pods-SDWebImageSwiftUIDemo-tvOS.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SDWebImageSwiftUIDemo-tvOS.debug.xcconfig"; path = "Target Support Files/Pods-SDWebImageSwiftUIDemo-tvOS/Pods-SDWebImageSwiftUIDemo-tvOS.debug.xcconfig"; sourceTree = "<group>"; };
@@ -220,6 +225,7 @@
220225
320CDC2F22FADB44007CF858 /* ContentView.swift */,
221226
326B0D702345C01900D28269 /* DetailView.swift */,
222227
321A6BEF2345EC4E00B5BEFC /* ProgressBar.swift */,
228+
32E7F120236CAAB8001688BC /* ActivityBar.swift */,
223229
320CDC3122FADB45007CF858 /* Assets.xcassets */,
224230
320CDC3622FADB45007CF858 /* LaunchScreen.storyboard */,
225231
320CDC3922FADB45007CF858 /* Info.plist */,
@@ -803,6 +809,7 @@
803809
326B0D712345C01900D28269 /* DetailView.swift in Sources */,
804810
320CDC2E22FADB44007CF858 /* SceneDelegate.swift in Sources */,
805811
321A6BF02345EC4E00B5BEFC /* ProgressBar.swift in Sources */,
812+
32E7F121236CAAB8001688BC /* ActivityBar.swift in Sources */,
806813
320CDC3022FADB44007CF858 /* ContentView.swift in Sources */,
807814
);
808815
runOnlyForDeploymentPostprocessing = 0;
@@ -814,6 +821,7 @@
814821
32E529622348A10B00EA46FF /* ContentView.swift in Sources */,
815822
32E529632348A10B00EA46FF /* DetailView.swift in Sources */,
816823
32E529642348A10B00EA46FF /* ProgressBar.swift in Sources */,
824+
32E7F122236CAAB8001688BC /* ActivityBar.swift in Sources */,
817825
32E5290C2348A0C700EA46FF /* AppDelegate.swift in Sources */,
818826
);
819827
runOnlyForDeploymentPostprocessing = 0;
@@ -825,6 +833,7 @@
825833
32E529652348A10B00EA46FF /* ContentView.swift in Sources */,
826834
32E529662348A10B00EA46FF /* DetailView.swift in Sources */,
827835
32E529672348A10B00EA46FF /* ProgressBar.swift in Sources */,
836+
32E7F123236CAAB8001688BC /* ActivityBar.swift in Sources */,
828837
32E529232348A0D300EA46FF /* AppDelegate.swift in Sources */,
829838
);
830839
runOnlyForDeploymentPostprocessing = 0;
@@ -837,6 +846,7 @@
837846
32E529692348A10C00EA46FF /* DetailView.swift in Sources */,
838847
32E529502348A0DE00EA46FF /* ExtensionDelegate.swift in Sources */,
839848
32E5296A2348A10C00EA46FF /* ProgressBar.swift in Sources */,
849+
32E7F124236CAAB8001688BC /* ActivityBar.swift in Sources */,
840850
32E529682348A10C00EA46FF /* ContentView.swift in Sources */,
841851
);
842852
runOnlyForDeploymentPostprocessing = 0;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* This file is part of the SDWebImage package.
3+
* (c) DreamPiggy <lizhuoli1126@126.com>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
import SwiftUI
10+
11+
/// A dot circle view that depicts the active status of a task.
12+
struct ActivityBar: View {
13+
private var dotRadius: CGFloat = 5
14+
@State private var isAnimating: Bool = false
15+
16+
var body: some View {
17+
GeometryReader { (geometry: GeometryProxy) in
18+
ForEach(0..<5) { index in
19+
Group {
20+
Circle()
21+
.frame(width: self.dotRadius, height: self.dotRadius)
22+
.scaleEffect(!self.isAnimating ? 1 - CGFloat(index) / 5 : 0.2 + CGFloat(index) / 5)
23+
.offset(y: geometry.size.width / 10 - geometry.size.height / 2)
24+
}
25+
.frame(width: geometry.size.width, height: geometry.size.height)
26+
.rotationEffect(!self.isAnimating ? .degrees(0) : .degrees(360))
27+
.animation(Animation
28+
.timingCurve(0.5, 0.15 + Double(index) / 5, 0.25, 1, duration: 1.5)
29+
.repeatForever(autoreverses: false))
30+
}
31+
}
32+
.aspectRatio(1, contentMode: .fit)
33+
.onAppear {
34+
self.isAnimating = true
35+
}
36+
}
37+
}

Example/SDWebImageSwiftUIDemo/ContentView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,11 @@ struct ContentView: View {
107107
#else
108108
WebImage(url: URL(string:url))
109109
.resizable()
110+
.indicator { _, _ in
111+
ActivityBar()
112+
.foregroundColor(Color.white)
113+
.frame(width: 50, height: 50)
114+
}
110115
.animation(.easeInOut(duration: 0.5))
111116
.transition(.fade)
112117
.scaledToFit()

SDWebImageSwiftUI/Classes/ImageManager.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class ImageManager : ObservableObject {
3434
if currentOperation != nil {
3535
return
3636
}
37+
self.image = nil
3738
self.isLoading = true
3839
currentOperation = manager.loadImage(with: url, options: options, context: context, progress: { [weak self] (receivedSize, expectedSize, _) in
3940
guard let self = self else {

SDWebImageSwiftUI/Classes/Indicator/Indicator.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,28 @@ public struct Indicator<T> where T : View {
2929
struct IndicatorViewModifier<T> : ViewModifier where T : View {
3030
@ObservedObject var imageManager: ImageManager
3131

32-
var indicator: Indicator<T>
32+
let indicatorView: T
3333

3434
func body(content: Content) -> some View {
35-
if (imageManager.image != nil) && !imageManager.isLoading {
35+
if !imageManager.isLoading {
3636
// Disable Indiactor
3737
return AnyView(content)
3838
} else {
3939
// Enable indicator
4040
return AnyView(
4141
ZStack {
4242
content
43-
indicator.builder($imageManager.isLoading, $imageManager.progress)
43+
indicatorView
4444
}
4545
)
4646
}
4747
}
48+
49+
init(imageManager: ImageManager, indicator: Indicator<T>) {
50+
self.imageManager = imageManager
51+
// This syntax looks not Swifty, hope for SwiftUI better design
52+
self.indicatorView = indicator.builder(_imageManager.projectedValue.isLoading, _imageManager.projectedValue.progress)
53+
}
4854
}
4955

5056
#if os(macOS) || os(iOS) || os(tvOS)

0 commit comments

Comments
 (0)