Skip to content

Commit 41d4d84

Browse files
improve CircularProgress and ProgressBar previews
1 parent 926b876 commit 41d4d84

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

Examples/DemosApp/DemosApp/ComponentsPreview/PreviewPages/CircularProgressPreview.swift

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,17 @@ import SwiftUI
33
import UIKit
44

55
struct CircularProgressPreview: View {
6-
@State private var model = CircularProgressVM {
7-
$0.label = "0"
8-
$0.style = .light
9-
$0.minValue = 0
10-
$0.maxValue = 100
11-
}
12-
13-
@State private var progress: CGFloat = 0
6+
@State private var model = Self.initialModel
7+
@State private var currentValue: CGFloat = Self.initialValue
8+
9+
private let timer = Timer
10+
.publish(every: 0.5, on: .main, in: .common)
11+
.autoconnect()
1412

1513
var body: some View {
1614
VStack {
1715
PreviewWrapper(title: "SwiftUI") {
18-
SUCircularProgress(currentValue: self.progress, model: self.model)
16+
SUCircularProgress(currentValue: self.currentValue, model: self.model)
1917
}
2018
Form {
2119
ComponentColorPicker(selection: self.$model.color)
@@ -37,18 +35,31 @@ struct CircularProgressPreview: View {
3735
Text("Light").tag(CircularProgressVM.Style.light)
3836
Text("Striped").tag(CircularProgressVM.Style.striped)
3937
}
40-
HStack {
41-
Text("Min")
42-
Slider(value: self.$progress, in: self.model.minValue...self.model.maxValue, step: 1) {
43-
}
44-
Text("Max")
45-
.onChange(of: self.progress) { newValue in
46-
self.model.label = "\(Int(newValue))"
47-
}
38+
}
39+
.onReceive(self.timer) { _ in
40+
if self.currentValue < self.model.maxValue {
41+
let step = (self.model.maxValue - self.model.minValue) / 100
42+
self.currentValue = min(
43+
self.model.maxValue,
44+
self.currentValue + CGFloat(Int.random(in: 1...20)) * step
45+
)
46+
} else {
47+
self.currentValue = self.model.minValue
4848
}
49+
self.model.label = "\(Int(self.currentValue))%"
4950
}
5051
}
5152
}
53+
54+
// MARK: - Helpers
55+
56+
private static var initialValue: Double {
57+
return 0.0
58+
}
59+
private static var initialModel = CircularProgressVM {
60+
$0.label = "0"
61+
$0.style = .light
62+
}
5263
}
5364

5465
#Preview {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ struct ProgressBarPreview: View {
99
private let progressBar = UKProgressBar(initialValue: Self.initialValue, model: Self.initialModel)
1010

1111
private let timer = Timer
12-
.publish(every: 0.1, on: .main, in: .common)
12+
.publish(every: 0.5, on: .main, in: .common)
1313
.autoconnect()
1414

1515
var body: some View {
@@ -43,7 +43,11 @@ struct ProgressBarPreview: View {
4343
}
4444
.onReceive(self.timer) { _ in
4545
if self.currentValue < self.model.maxValue {
46-
self.currentValue += (self.model.maxValue - self.model.minValue) / 100
46+
let step = (self.model.maxValue - self.model.minValue) / 100
47+
self.currentValue = min(
48+
self.model.maxValue,
49+
self.currentValue + CGFloat(Int.random(in: 1...20)) * step
50+
)
4751
} else {
4852
self.currentValue = self.model.minValue
4953
}

0 commit comments

Comments
 (0)