Skip to content

Commit 3d41a65

Browse files
authored
Merge pull request #28 from SDWebImage/feature_indicator_convenience
Supports indicator with style and convenience methods
2 parents d89ccbb + 82fa9ba commit 3d41a65

File tree

4 files changed

+64
-5
lines changed

4 files changed

+64
-5
lines changed

SDWebImageSwiftUI/Classes/Indicator/ActivityIndicator.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ import SwiftUI
1212
/// An activity indicator (system style)
1313
public struct ActivityIndicator: PlatformViewRepresentable {
1414
@Binding var isAnimating: Bool
15+
var style: Style
1516

16-
public init(_ isAnimating: Binding<Bool>) {
17+
public init(_ isAnimating: Binding<Bool>, style: Style = .medium) {
1718
self._isAnimating = isAnimating
19+
self.style = style
1820
}
1921

2022
#if os(macOS)
@@ -25,7 +27,14 @@ public struct ActivityIndicator: PlatformViewRepresentable {
2527

2628
#if os(iOS) || os(tvOS)
2729
public func makeUIView(context: UIViewRepresentableContext<ActivityIndicator>) -> UIActivityIndicatorView {
28-
let indicator = UIActivityIndicatorView(style: .medium)
30+
let activityStyle: UIActivityIndicatorView.Style
31+
switch style {
32+
case .medium:
33+
activityStyle = .medium
34+
case .large:
35+
activityStyle = .large
36+
}
37+
let indicator = UIActivityIndicatorView(style: activityStyle)
2938
indicator.hidesWhenStopped = true
3039
return indicator
3140
}
@@ -37,8 +46,16 @@ public struct ActivityIndicator: PlatformViewRepresentable {
3746

3847
#if os(macOS)
3948
public func makeNSView(context: NSViewRepresentableContext<ActivityIndicator>) -> NSProgressIndicator {
49+
let controlSize: NSControl.ControlSize
50+
switch style {
51+
case .medium:
52+
controlSize = .small
53+
case .large:
54+
controlSize = .regular
55+
}
4056
let indicator = NSProgressIndicator()
4157
indicator.style = .spinning
58+
indicator.controlSize = controlSize
4259
indicator.isDisplayedWhenStopped = false
4360
return indicator
4461
}
@@ -49,4 +66,11 @@ public struct ActivityIndicator: PlatformViewRepresentable {
4966

5067
#endif
5168
}
69+
70+
extension ActivityIndicator {
71+
public enum Style {
72+
case medium
73+
case large
74+
}
75+
}
5276
#endif

SDWebImageSwiftUI/Classes/Indicator/Indicator.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,27 @@ extension Indicator {
3434
}
3535
}
3636

37+
/// Activity Indicator with style
38+
/// - Parameter style: style
39+
public static func activity(style: ActivityIndicator.Style) -> Indicator {
40+
Indicator { isAnimating, _ in
41+
ActivityIndicator(isAnimating, style: style)
42+
}
43+
}
44+
3745
/// Progress Indicator
3846
public static var progress: Indicator {
3947
Indicator { isAnimating, progress in
4048
ProgressIndicator(isAnimating, progress: progress)
4149
}
4250
}
51+
52+
/// Progress Indicator with style
53+
/// - Parameter style: style
54+
public static func progress(style: ProgressIndicator.Style) -> Indicator {
55+
Indicator { isAnimating, progress in
56+
ProgressIndicator(isAnimating, progress: progress, style: style)
57+
}
58+
}
4359
}
4460
#endif

SDWebImageSwiftUI/Classes/Indicator/ProgressIndicator.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,12 @@ import SwiftUI
1313
public struct ProgressIndicator: PlatformViewRepresentable {
1414
@Binding var isAnimating: Bool
1515
@Binding var progress: CGFloat
16+
var style: Style
1617

17-
public init(_ isAnimating: Binding<Bool>, progress: Binding<CGFloat>) {
18+
public init(_ isAnimating: Binding<Bool>, progress: Binding<CGFloat>, style: Style = .default) {
1819
self._isAnimating = isAnimating
1920
self._progress = progress
21+
self.style = style
2022
}
2123

2224
#if os(macOS)
@@ -27,9 +29,18 @@ public struct ProgressIndicator: PlatformViewRepresentable {
2729

2830
#if os(iOS) || os(tvOS)
2931
public func makeUIView(context: UIViewRepresentableContext<ProgressIndicator>) -> ProgressIndicatorWrapper {
32+
let progressStyle: UIProgressView.Style
33+
switch style {
34+
#if os(iOS)
35+
case .bar:
36+
progressStyle = .bar
37+
#endif
38+
default:
39+
progressStyle = .default
40+
}
3041
let uiView = ProgressIndicatorWrapper()
3142
let view = uiView.wrapped
32-
view.progressViewStyle = .default
43+
view.progressViewStyle = progressStyle
3344
return uiView
3445
}
3546

@@ -81,4 +92,11 @@ public struct ProgressIndicator: PlatformViewRepresentable {
8192
}
8293
#endif
8394
}
95+
96+
extension ProgressIndicator {
97+
public enum Style {
98+
case `default`
99+
case bar
100+
}
101+
}
84102
#endif

SDWebImageSwiftUI/Classes/WebImage.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,12 @@ extension WebImage {
169169
}
170170
}
171171

172+
// Indicator
172173
extension WebImage {
173174

174175
/// Associate a indicator when loading image with url
175176
/// - Parameter indicator: The indicator type, see `Indicator`
176-
public func indicator(_ indicator: Indicator) -> WebImage {
177+
public func indicator(_ indicator: Indicator?) -> WebImage {
177178
var result = self
178179
result.indicator = indicator
179180
return result

0 commit comments

Comments
 (0)