|
| 1 | +import ComponentsKit |
| 2 | +import SwiftUI |
| 3 | +import UIKit |
| 4 | + |
| 5 | +struct ProgressBarPreview: View { |
| 6 | + @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 | + |
| 11 | + private let timer = Timer |
| 12 | + .publish(every: 0.1, on: .main, in: .common) |
| 13 | + .autoconnect() |
| 14 | + |
| 15 | + var body: some View { |
| 16 | + VStack { |
| 17 | + PreviewWrapper(title: "UIKit") { |
| 18 | + self.progressBar |
| 19 | + .preview |
| 20 | + .onAppear { |
| 21 | + self.progressBar.currentValue = self.currentValue |
| 22 | + self.progressBar.model = Self.initialModel |
| 23 | + } |
| 24 | + .onChange(of: self.model) { newValue in |
| 25 | + self.progressBar.model = newValue |
| 26 | + } |
| 27 | + } |
| 28 | + PreviewWrapper(title: "SwiftUI") { |
| 29 | + SUProgressBar(currentValue: self.$currentValue, model: self.model) |
| 30 | + } |
| 31 | + Form { |
| 32 | + ComponentColorPicker(selection: self.$model.color) |
| 33 | + ComponentRadiusPicker(selection: self.$model.cornerRadius) { |
| 34 | + Text("Custom: 2px").tag(ComponentRadius.custom(2)) |
| 35 | + } |
| 36 | + SizePicker(selection: self.$model.size) |
| 37 | + Picker("Style", selection: self.$model.style) { |
| 38 | + Text("Light").tag(ProgressBarVM.Style.light) |
| 39 | + Text("Filled").tag(ProgressBarVM.Style.filled) |
| 40 | + Text("Striped").tag(ProgressBarVM.Style.striped) |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + .onReceive(self.timer) { _ in |
| 45 | + if self.currentValue < self.model.maxValue { |
| 46 | + self.currentValue += (self.model.maxValue - self.model.minValue) / 100 |
| 47 | + } else { |
| 48 | + self.currentValue = self.model.minValue |
| 49 | + } |
| 50 | + |
| 51 | + self.progressBar.currentValue = self.currentValue |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + // MARK: - Helpers |
| 56 | + |
| 57 | + private static var initialValue: Double { |
| 58 | + return 0.0 |
| 59 | + } |
| 60 | + private static var initialModel: ProgressBarVM { |
| 61 | + return .init() |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +#Preview { |
| 66 | + ProgressBarPreview() |
| 67 | +} |
0 commit comments