@@ -3,19 +3,17 @@ import SwiftUI
3
3
import UIKit
4
4
5
5
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 ( )
14
12
15
13
var body : some View {
16
14
VStack {
17
15
PreviewWrapper ( title: " SwiftUI " ) {
18
- SUCircularProgress ( currentValue: self . progress , model: self . model)
16
+ SUCircularProgress ( currentValue: self . currentValue , model: self . model)
19
17
}
20
18
Form {
21
19
ComponentColorPicker ( selection: self . $model. color)
@@ -37,18 +35,31 @@ struct CircularProgressPreview: View {
37
35
Text ( " Light " ) . tag ( CircularProgressVM . Style. light)
38
36
Text ( " Striped " ) . tag ( CircularProgressVM . Style. striped)
39
37
}
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
48
48
}
49
+ self . model. label = " \( Int ( self . currentValue) ) % "
49
50
}
50
51
}
51
52
}
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
+ }
52
63
}
53
64
54
65
#Preview {
0 commit comments