Skip to content

Commit dd1158d

Browse files
committed
var fraction fix
renamed to progress and moved to the top
1 parent fd77126 commit dd1158d

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

Sources/ComponentsKit/ProgressBar/SUProgressBar.swift

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@ public struct SUProgressBar: View {
99

1010
@Binding private var currentValue: CGFloat
1111

12+
private var progress: CGFloat {
13+
let range = self.model.maxValue - self.model.minValue
14+
15+
guard range > 0 else {
16+
return 0
17+
}
18+
19+
let progress = (self.currentValue - self.model.minValue) / range
20+
return max(0, min(1, progress))
21+
}
22+
1223
// MARK: - Initializer
1324

1425
/// Initializer.
@@ -32,13 +43,13 @@ public struct SUProgressBar: View {
3243
HStack(spacing: 4) {
3344
RoundedRectangle(cornerRadius: self.model.computedCornerRadius)
3445
.foregroundStyle(self.model.barColor.color)
35-
.frame(width: geometry.size.width * self.fraction, height: self.model.barHeight)
46+
.frame(width: geometry.size.width * self.progress, height: self.model.barHeight)
3647
Rectangle()
3748
.foregroundStyle(self.model.backgroundColor.color)
38-
.frame(width: geometry.size.width * (1 - self.fraction), height: self.model.barHeight)
49+
.frame(width: geometry.size.width * (1 - self.progress), height: self.model.barHeight)
3950
.cornerRadius(self.model.computedCornerRadius)
4051
}
41-
.animation(.spring, value: self.fraction)
52+
.animation(.spring, value: self.progress)
4253

4354
case .filled:
4455
ZStack(alignment: .leading) {
@@ -48,11 +59,11 @@ public struct SUProgressBar: View {
4859

4960
RoundedRectangle(cornerRadius: self.model.computedCornerRadius)
5061
.foregroundStyle((self.model.color.contrast ?? .foreground).color)
51-
.frame(width: (geometry.size.width - 6) * self.fraction, height: self.model.barHeight - 6)
62+
.frame(width: (geometry.size.width - 6) * self.progress, height: self.model.barHeight - 6)
5263
.padding(.vertical, self.model.contentPaddings.top)
5364
.padding(.horizontal, self.model.contentPaddings.trailing)
5465
}
55-
.animation(.spring, value: self.fraction)
66+
.animation(.spring, value: self.progress)
5667

5768
case .striped:
5869
ZStack(alignment: .leading) {
@@ -62,7 +73,7 @@ public struct SUProgressBar: View {
6273

6374
RoundedRectangle(cornerRadius: self.model.computedCornerRadius)
6475
.foregroundStyle(self.model.color.contrast.color)
65-
.frame(width: (geometry.size.width - 6) * self.fraction, height: self.model.barHeight - 6)
76+
.frame(width: (geometry.size.width - 6) * self.progress, height: self.model.barHeight - 6)
6677
.padding(.vertical, self.model.contentPaddings.top)
6778
.padding(.horizontal, self.model.contentPaddings.trailing)
6879

@@ -72,25 +83,18 @@ public struct SUProgressBar: View {
7283
.cornerRadius(self.model.computedCornerRadius)
7384
.clipped()
7485
}
75-
.animation(.spring, value: self.fraction)
86+
.animation(.spring, value: self.progress)
7687
}
7788
}
7889
.frame(height: self.model.barHeight)
7990
.onAppear {
8091
self.model.validateMinMaxValues()
8192
}
8293
}
83-
84-
// MARK: - Properties
85-
86-
private var fraction: CGFloat {
87-
let range = self.model.maxValue - self.model.minValue
88-
guard range != 0 else { return 0 }
89-
let fraction = (self.currentValue - self.model.minValue) / range
90-
return max(0, min(1, fraction))
91-
}
9294
}
9395

96+
// MARK: - Properties
97+
9498
struct StripesShape: Shape {
9599
var model: ProgressBarVM
96100

0 commit comments

Comments
 (0)