Skip to content

Commit 4a31807

Browse files
add currentValue to ProgressBarVM
1 parent 080603c commit 4a31807

File tree

4 files changed

+61
-35
lines changed

4 files changed

+61
-35
lines changed

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/ProgressBarPreview.swift

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ import UIKit
44

55
struct ProgressBarPreview: View {
66
@State private var model = Self.initialModel
7-
@State private var currentValue: CGFloat = Self.initialValue
8-
9-
private let progressBar = UKProgressBar(initialValue: Self.initialValue, model: Self.initialModel)
10-
7+
8+
private let progressBar = UKProgressBar(model: Self.initialModel)
9+
1110
private let timer = Timer
1211
.publish(every: 0.5, on: .main, in: .common)
1312
.autoconnect()
@@ -18,15 +17,14 @@ struct ProgressBarPreview: View {
1817
self.progressBar
1918
.preview
2019
.onAppear {
21-
self.progressBar.currentValue = self.currentValue
2220
self.progressBar.model = Self.initialModel
2321
}
2422
.onChange(of: self.model) { newValue in
2523
self.progressBar.model = newValue
2624
}
2725
}
2826
PreviewWrapper(title: "SwiftUI") {
29-
SUProgressBar(currentValue: self.currentValue, model: self.model)
27+
SUProgressBar(model: self.model)
3028
}
3129
Form {
3230
ComponentColorPicker(selection: self.$model.color)
@@ -42,25 +40,20 @@ struct ProgressBarPreview: View {
4240
}
4341
}
4442
.onReceive(self.timer) { _ in
45-
if self.currentValue < self.model.maxValue {
43+
if self.model.currentValue < self.model.maxValue {
4644
let step = (self.model.maxValue - self.model.minValue) / 100
47-
self.currentValue = min(
45+
self.model.currentValue = min(
4846
self.model.maxValue,
49-
self.currentValue + CGFloat(Int.random(in: 1...20)) * step
47+
self.model.currentValue + CGFloat(Int.random(in: 1...20)) * step
5048
)
5149
} else {
52-
self.currentValue = self.model.minValue
50+
self.model.currentValue = self.model.minValue
5351
}
54-
55-
self.progressBar.currentValue = self.currentValue
5652
}
5753
}
5854

5955
// MARK: - Helpers
6056

61-
private static var initialValue: Double {
62-
return 0.0
63-
}
6457
private static var initialModel: ProgressBarVM {
6558
return .init()
6659
}

Sources/ComponentsKit/Components/ProgressBar/Models/ProgressBarVM.swift

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,29 @@ public struct ProgressBarVM: ComponentVM {
77
/// Defaults to `.accent`.
88
public var color: ComponentColor = .accent
99

10-
/// The visual style of the progress bar component.
11-
///
12-
/// Defaults to `.striped`.
13-
public var style: Style = .striped
14-
15-
/// The size of the progress bar.
10+
/// The corner radius of the progress bar.
1611
///
1712
/// Defaults to `.medium`.
18-
public var size: ComponentSize = .medium
13+
public var cornerRadius: ComponentRadius = .medium
1914

20-
/// The minimum value of the progress bar.
21-
public var minValue: CGFloat = 0
15+
/// The current value of the progress bar.
16+
public var currentValue: CGFloat = 0
2217

2318
/// The maximum value of the progress bar.
2419
public var maxValue: CGFloat = 100
2520

26-
/// The corner radius of the progress bar.
21+
/// The minimum value of the progress bar.
22+
public var minValue: CGFloat = 0
23+
24+
/// The size of the progress bar.
2725
///
2826
/// Defaults to `.medium`.
29-
public var cornerRadius: ComponentRadius = .medium
27+
public var size: ComponentSize = .medium
28+
29+
/// The visual style of the progress bar component.
30+
///
31+
/// Defaults to `.striped`.
32+
public var style: Style = .striped
3033

3134
/// Initializes a new instance of `ProgressBarVM` with default values.
3235
public init() {}
@@ -139,6 +142,13 @@ extension ProgressBarVM {
139142
}
140143

141144
extension ProgressBarVM {
145+
var progress: CGFloat {
146+
let range = self.maxValue - self.minValue
147+
guard range > 0 else { return 0 }
148+
let normalized = (self.currentValue - self.minValue) / range
149+
return max(0, min(1, normalized))
150+
}
151+
142152
func progress(for currentValue: CGFloat) -> CGFloat {
143153
let range = self.maxValue - self.minValue
144154
guard range > 0 else { return 0 }

Sources/ComponentsKit/Components/ProgressBar/SUProgressBar.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import SwiftUI
22

3-
/// A SwiftUI component that displays a progress bar.
3+
/// A SwiftUI component that visually represents the progress of a task or process using a horizontal bar.
44
public struct SUProgressBar: View {
55
// MARK: - Properties
66

77
/// A model that defines the appearance properties.
88
public var model: ProgressBarVM
99
/// The current progress value.
10-
public var currentValue: CGFloat
10+
public var currentValue: CGFloat?
1111

1212
private var progress: CGFloat {
13-
self.model.progress(for: self.currentValue)
13+
self.currentValue.map { self.model.progress(for: $0) } ?? self.model.progress
1414
}
1515

1616
// MARK: - Initializer
@@ -19,6 +19,7 @@ public struct SUProgressBar: View {
1919
/// - Parameters:
2020
/// - currentValue: The current progress value.
2121
/// - model: A model that defines the appearance properties.
22+
@available(*, deprecated, message: "Set `currentValue` in the model instead.")
2223
public init(
2324
currentValue: CGFloat,
2425
model: ProgressBarVM = .init()
@@ -27,6 +28,15 @@ public struct SUProgressBar: View {
2728
self.model = model
2829
}
2930

31+
/// Initializer.
32+
/// - Parameters:
33+
/// - model: A model that defines the appearance properties.
34+
public init(
35+
model: ProgressBarVM = .init()
36+
) {
37+
self.model = model
38+
}
39+
3040
// MARK: - Body
3141

3242
public var body: some View {

Sources/ComponentsKit/Components/ProgressBar/UKProgressBar.swift

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import AutoLayout
22
import UIKit
33

4-
/// A UIKit component that displays a progress bar.
4+
/// A UIKit component that visually represents the progress of a task or process using a horizontal bar.
55
open class UKProgressBar: UIView, UKComponent {
6-
// MARK: - Properties
6+
// MARK: - Public Properties
77

88
/// A model that defines the appearance properties.
99
public var model: ProgressBarVM {
@@ -13,7 +13,7 @@ open class UKProgressBar: UIView, UKComponent {
1313
}
1414

1515
/// The current progress value for the progress bar.
16-
public var currentValue: CGFloat {
16+
public var currentValue: CGFloat? {
1717
didSet {
1818
self.updateProgressWidthAndAppearance()
1919
}
@@ -39,7 +39,7 @@ open class UKProgressBar: UIView, UKComponent {
3939
// MARK: - Private Properties
4040

4141
private var progress: CGFloat {
42-
self.model.progress(for: self.currentValue)
42+
self.currentValue.map { self.model.progress(for: $0) } ?? self.model.progress
4343
}
4444

4545
// MARK: - UIView Properties
@@ -54,6 +54,7 @@ open class UKProgressBar: UIView, UKComponent {
5454
/// - Parameters:
5555
/// - initialValue: The initial progress value. Defaults to `0`.
5656
/// - model: A model that defines the appearance properties.
57+
@available(*, deprecated, message: "Set `currentValue` in the model instead.")
5758
public init(
5859
initialValue: CGFloat = 0,
5960
model: ProgressBarVM = .init()
@@ -67,6 +68,18 @@ open class UKProgressBar: UIView, UKComponent {
6768
self.layout()
6869
}
6970

71+
/// Initializer.
72+
/// - Parameters:
73+
/// - model: A model that defines the appearance properties.
74+
public init(model: ProgressBarVM = .init()) {
75+
self.model = model
76+
super.init(frame: .zero)
77+
78+
self.setup()
79+
self.style()
80+
self.layout()
81+
}
82+
7083
public required init?(coder: NSCoder) {
7184
fatalError("init(coder:) has not been implemented")
7285
}
@@ -138,9 +151,9 @@ open class UKProgressBar: UIView, UKComponent {
138151
self.setNeedsLayout()
139152
}
140153

141-
UIView.performWithoutAnimation {
154+
// UIView.performWithoutAnimation {
142155
self.updateProgressWidthAndAppearance()
143-
}
156+
// }
144157
}
145158

146159
private func updateProgressWidthAndAppearance() {

0 commit comments

Comments
 (0)