Skip to content

Commit 63b6da1

Browse files
committed
Changing the indicator API to use the dot syntax, instead of that closure syntax, which is more convenient and swifty
1 parent 896627d commit 63b6da1

File tree

6 files changed

+43
-29
lines changed

6 files changed

+43
-29
lines changed

Example/SDWebImageSwiftUIDemo/ContentView.swift

+1-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,7 @@ struct ContentView: View {
9696
} else {
9797
#if os(macOS) || os(iOS) || os(tvOS)
9898
WebImage(url: URL(string:url))
99-
.indicator { isAnimating, _ in
100-
ActivityIndicator(isAnimating)
101-
}
99+
.indicator(.activity)
102100
.resizable()
103101
.scaledToFit()
104102
.frame(width: CGFloat(100), height: CGFloat(100), alignment: .center)

Example/SDWebImageSwiftUIDemo/DetailView.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ struct DetailView: View {
5757
} else {
5858
#if os(macOS) || os(iOS) || os(tvOS)
5959
WebImage(url: URL(string:url), options: [.progressiveLoad])
60-
.indicator { isAnimating, progress in
61-
ProgressIndicator(isAnimating, progress: progress)
62-
}
60+
.indicator(.progress)
6361
.resizable()
6462
.scaledToFit()
6563
#else
6664
WebImage(url: URL(string:url), options: [.progressiveLoad])
67-
.indicator { isAnimating, progress in
68-
ProgressBar(value: progress)
69-
.foregroundColor(.blue)
70-
.frame(maxHeight: 6)
71-
}
65+
.indicator(
66+
Indicator { isAnimating, progress in
67+
ProgressBar(value: progress)
68+
.foregroundColor(.blue)
69+
.frame(maxHeight: 6)
70+
}
71+
)
7272
.resizable()
7373
.scaledToFit()
7474
#endif

Example/SDWebImageSwiftUIDemo/ProgressBar.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public struct ProgressBar: View {
1616
GeometryReader { geometry in
1717
ZStack(alignment: .leading) {
1818
Rectangle()
19-
.frame(width: geometry.size.width)
19+
.frame(width: geometry.size.width)
2020
.opacity(0.3)
2121
Rectangle()
2222
.frame(width: geometry.size.width * self.value)

README.md

+4-6
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ let package = Package(
6161

6262
### Using `WebImage` to load network image
6363

64-
- [x] Supports the placeholder and detail options control for image loading as SDWebImage
65-
- [x] Supports the success/failure/progress changes event for custom handling
66-
- [x] Supports the indicator with activity/progress indicator and customization
64+
- [x] Supports placeholder and detail options control for image loading as SDWebImage
65+
- [x] Supports success/failure/progress changes event for custom handling
66+
- [x] Supports indicator with activity/progress indicator and customization
6767

6868
Note: This `WebImage` using `Image` for internal implementation, which is the best compatible for SwiftUI layout and animation system. But it supports static image format only, because unlike `UIImageView` in UIKit, SwiftUI's `Image` does not support animation.
6969

@@ -73,9 +73,7 @@ var body: some View {
7373
.onSuccess { image, cacheType in
7474
// Success
7575
}
76-
.indicator { isAnimating, _ in
77-
ActivityIndicator(isAnimating) // Activity Indicator
78-
}
76+
.indicator(.activity) // Activity Indicator
7977
.resizable()
8078
.scaledToFit()
8179
.frame(width: 300, height: 300, alignment: .center)

SDWebImageSwiftUI/Classes/Indicator/Indicator.swift

+26-6
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,36 @@
99
import Foundation
1010
import SwiftUI
1111

12-
/// A container view to hold the indicator builder
13-
public struct Indicator : View {
12+
/// A type to build the indicator
13+
public struct Indicator {
1414
var builder: (Binding<Bool>, Binding<CGFloat>) -> AnyView
15-
public typealias Body = Never
16-
public var body: Never {
17-
fatalError()
18-
}
15+
16+
/// Create a indicator with builder
17+
/// - Parameter builder: A builder to build indicator
18+
/// - 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.
19+
/// - Parameter progress: A Binding to control the progress during loading. If no progress can be reported, the value is 0.
20+
/// Associate a indicator when loading image with url
1921
public init<T>(@ViewBuilder builder: @escaping (_ isAnimating: Binding<Bool>, _ progress: Binding<CGFloat>) -> T) where T : View {
2022
self.builder = { isAnimating, progress in
2123
AnyView(builder(isAnimating, progress))
2224
}
2325
}
2426
}
27+
28+
#if os(macOS) || os(iOS) || os(iOS)
29+
extension Indicator {
30+
/// Activity Indicator
31+
public static var activity: Indicator {
32+
Indicator { isAnimating, _ in
33+
ActivityIndicator(isAnimating)
34+
}
35+
}
36+
37+
/// Progress Indicator
38+
public static var progress: Indicator {
39+
Indicator { isAnimating, progress in
40+
ProgressIndicator(isAnimating, progress: progress)
41+
}
42+
}
43+
}
44+
#endif

SDWebImageSwiftUI/Classes/WebImage.swift

+3-5
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,10 @@ extension WebImage {
172172
extension WebImage {
173173

174174
/// Associate a indicator when loading image with url
175-
/// - Parameter builder: builder description
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.
177-
/// - Parameter progress: A Binding to control the progress during loading. If no progress can be reported, the value is 0.
178-
public func indicator<T>(_ builder: @escaping (_ isAnimating: Binding<Bool>, _ progress: Binding<CGFloat>) -> T) -> WebImage where T : View {
175+
/// - Parameter indicator: The indicator type, see `Indicator`
176+
public func indicator(_ indicator: Indicator) -> WebImage {
179177
var result = self
180-
result.indicator = Indicator(builder: builder)
178+
result.indicator = indicator
181179
return result
182180
}
183181
}

0 commit comments

Comments
 (0)