@@ -3,19 +3,17 @@ import SwiftUI
33import UIKit
44
55struct 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 {
0 commit comments