Skip to content

Commit 776e18f

Browse files
committed
Update the watchOS Example with latest Espera
1 parent c710df6 commit 776e18f

File tree

2 files changed

+79
-31
lines changed

2 files changed

+79
-31
lines changed

Example/SDWebImageSwiftUIDemo/ContentView.swift

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,47 @@ class UserSettings: ObservableObject {
2020

2121
#if os(watchOS)
2222
// watchOS does not provide built-in indicator, use Espera's custom indicator
23-
extension Indicator where T == LoadingFlowerView {
23+
struct ActivityIndicator : View {
24+
@Binding var isAnimating: Bool
25+
var body: some View {
26+
if isAnimating {
27+
return AnyView(LoadingFlowerView()
28+
.frame(width: 30, height: 30))
29+
} else {
30+
return AnyView(EmptyView()
31+
.frame(width: 30, height: 30))
32+
}
33+
}
34+
}
35+
36+
struct ProgressIndicator : View {
37+
@Binding var isAnimating: Bool
38+
@Binding var progress: Double
39+
var body: some View {
40+
if isAnimating {
41+
return AnyView(StretchProgressView(progress: $progress)
42+
.frame(width: 140, height: 10))
43+
} else {
44+
return AnyView(EmptyView()
45+
.frame(width: 140, height: 10))
46+
}
47+
}
48+
}
49+
50+
extension Indicator where T == ActivityIndicator {
2451
/// Activity Indicator
2552
static var activity: Indicator {
2653
Indicator { isAnimating, _ in
27-
LoadingFlowerView()
54+
ActivityIndicator(isAnimating: isAnimating)
2855
}
2956
}
3057
}
3158

32-
extension Indicator where T == StretchProgressView {
59+
extension Indicator where T == ProgressIndicator {
3360
/// Progress Indicator
3461
static var progress: Indicator {
3562
Indicator { isAnimating, progress in
36-
StretchProgressView(progress: progress)
63+
ProgressIndicator(isAnimating: isAnimating, progress: progress)
3764
}
3865
}
3966
}

Example/SDWebImageSwiftUIDemo/Espera.swift

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public struct RotatingCircleWithGap: View {
3636
private struct LoadingCircle: View {
3737
let circleColor: Color
3838
let scale: CGFloat
39-
private let circleWidth: CGFloat = 8
39+
let circleWidth: CGFloat
4040

4141
var body: some View {
4242
Circle()
@@ -47,6 +47,7 @@ private struct LoadingCircle: View {
4747
}
4848

4949
public struct LoadingFlowerView: View {
50+
5051
private let animationDuration: Double = 0.6
5152
private var singleCircleAnimationDuration: Double {
5253
return animationDuration/3
@@ -56,37 +57,58 @@ public struct LoadingFlowerView: View {
5657
.repeatForever(autoreverses: true)
5758
}
5859

59-
@State private var color: Color = .init(white: 0.3)
60-
@State private var scale: CGFloat = 0.98
60+
private let originalColor: Color
61+
public init(color: Color = .white) {
62+
self.originalColor = color
63+
self.color = color.opacity(0.3)
64+
}
6165

62-
public init() { }
66+
@State private var color = Color.white.opacity(0.3)
67+
@State private var scale: CGFloat = 0.98
6368

6469
public var body: some View {
65-
HStack(spacing: 1) {
66-
VStack(spacing: 2) {
67-
LoadingCircle(circleColor: color, scale: scale)
68-
.animation(foreverAnimation.delay(singleCircleAnimationDuration*5))
69-
LoadingCircle(circleColor: color, scale: scale)
70-
.animation(foreverAnimation.delay(singleCircleAnimationDuration*4))
71-
}
72-
VStack(alignment: .center, spacing: 1) {
73-
LoadingCircle(circleColor: color, scale: scale)
74-
.animation(foreverAnimation)
75-
LoadingCircle(circleColor: .clear, scale: 1)
76-
LoadingCircle(circleColor: color, scale: scale)
77-
.animation(foreverAnimation.delay(singleCircleAnimationDuration*3))
78-
}
79-
VStack(alignment: .center, spacing: 2) {
80-
LoadingCircle(circleColor: color, scale: scale)
81-
.animation(foreverAnimation.delay(singleCircleAnimationDuration*1))
82-
LoadingCircle(circleColor: color, scale: scale)
83-
.animation(foreverAnimation.delay(singleCircleAnimationDuration*2))
84-
}
70+
GeometryReader { [color, scale, singleCircleAnimationDuration, foreverAnimation] reader -> AnyView in
71+
72+
let minLength = min(reader.size.width, reader.size.height)
73+
let thirdOfMinLength = minLength / 3
74+
75+
let proportionalSpacing: CGFloat = 1 / 26
76+
let spacing = minLength * proportionalSpacing
77+
78+
// THIS IS FINE :D
79+
// Fix later, ok?
80+
let leafDiameter = thirdOfMinLength - (spacing - proportionalSpacing * thirdOfMinLength)
81+
82+
return AnyView(
83+
HStack(spacing: spacing) {
84+
VStack(spacing: spacing) {
85+
LoadingCircle(circleColor: color, scale: scale, circleWidth: leafDiameter)
86+
.animation(foreverAnimation.delay(singleCircleAnimationDuration*5))
87+
LoadingCircle(circleColor: color, scale: scale, circleWidth: leafDiameter)
88+
.animation(foreverAnimation.delay(singleCircleAnimationDuration*4))
89+
}
90+
VStack(alignment: .center, spacing: spacing) {
91+
LoadingCircle(circleColor: color, scale: scale, circleWidth: leafDiameter)
92+
.animation(foreverAnimation)
93+
LoadingCircle(circleColor: .clear, scale: 1, circleWidth: leafDiameter)
94+
LoadingCircle(circleColor: color, scale: scale, circleWidth: leafDiameter)
95+
.animation(foreverAnimation.delay(singleCircleAnimationDuration*3))
96+
}
97+
VStack(alignment: .center, spacing: spacing) {
98+
LoadingCircle(circleColor: color, scale: scale, circleWidth: leafDiameter)
99+
.animation(foreverAnimation.delay(singleCircleAnimationDuration*1))
100+
LoadingCircle(circleColor: color, scale: scale, circleWidth: leafDiameter)
101+
.animation(foreverAnimation.delay(singleCircleAnimationDuration*2))
102+
}
103+
}
104+
)
85105
}
86106
.onAppear {
87-
self.color = .white
88-
self.scale = 1.02
107+
self.color = self.originalColor
108+
self.scale = 1
89109
}
110+
.aspectRatio(contentMode: .fit)
111+
.frame(idealWidth: 26)
90112
}
91113
}
92114

@@ -198,7 +220,6 @@ public struct StretchProgressView: View {
198220

199221
public var body: some View {
200222
StretchyShape(progress: progress, mode: .stretchy)
201-
.frame(width: 140, height: 10)
202223
}
203224
}
204225

0 commit comments

Comments
 (0)