ProgressView.swift 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. //
  2. // ProgressView.swift
  3. // DashKitUI
  4. //
  5. // Created by Pete Schwamb on 3/2/20.
  6. // Copyright © 2020 LoopKit Authors. All rights reserved.
  7. //
  8. import SwiftUI
  9. // TODO: SwiftUI now has built in ProgressView to replace this
  10. public struct ProgressView: View {
  11. private let progress: CGFloat
  12. private let barHeight: CGFloat = 8
  13. public init(progress: CGFloat) {
  14. self.progress = progress
  15. }
  16. public var body: some View {
  17. return GeometryReader { geometry in
  18. ZStack(alignment: .leading) {
  19. Rectangle()
  20. .opacity(0.1)
  21. .cornerRadius(self.barHeight/2)
  22. .animation(nil)
  23. Rectangle()
  24. .foregroundColor(Color.accentColor)
  25. .frame(width: geometry.size.width * self.progress)
  26. .cornerRadius(self.barHeight/2)
  27. }
  28. }
  29. .frame(height: barHeight)
  30. }
  31. }
  32. struct ProgressTestView: View {
  33. @State var showDetail: Bool = false
  34. @State var madeProgress: Bool = false
  35. var body: some View {
  36. VStack {
  37. ProgressView(progress: madeProgress ? 0.9 : 0.5)
  38. .animation(.linear(duration: 2))
  39. .padding()
  40. .opacity(showDetail ? 1 : 0)
  41. .animation(.linear(duration: 0.2))
  42. Button("Test") {
  43. self.showDetail.toggle()
  44. self.madeProgress.toggle()
  45. }
  46. }
  47. .animation(.linear(duration: 0.2))
  48. }
  49. }
  50. struct ProgressView_Previews: PreviewProvider {
  51. @State var showDetail: Bool = false
  52. static var previews: some View {
  53. ProgressTestView()
  54. }
  55. }