LoopView.swift 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import SwiftDate
  2. import SwiftUI
  3. import UIKit
  4. struct LoopView: View {
  5. private enum Config {
  6. static let lag: TimeInterval = 30
  7. }
  8. @Binding var suggestion: Suggestion?
  9. @Binding var enactedSuggestion: Suggestion?
  10. @Binding var closedLoop: Bool
  11. @Binding var timerDate: Date
  12. @Binding var isLooping: Bool
  13. private var dateFormatter: DateFormatter {
  14. let formatter = DateFormatter()
  15. formatter.timeStyle = .short
  16. return formatter
  17. }
  18. private let rect = CGRect(x: 0, y: 0, width: 38, height: 38)
  19. var body: some View {
  20. VStack(alignment: .center) {
  21. ZStack {
  22. Circle()
  23. .strokeBorder(color, lineWidth: 6)
  24. .frame(width: rect.width, height: rect.height)
  25. .mask(mask(in: rect).fill(style: FillStyle(eoFill: true)))
  26. if isLooping {
  27. ProgressView()
  28. }
  29. }
  30. Spacer()
  31. if isLooping {
  32. Text("looping").font(.caption2)
  33. } else if let date = actualSuggestion?.timestamp {
  34. Text("\(Int((timerDate.timeIntervalSince(date) - Config.lag) / 60) + 1) min").font(.caption)
  35. } else {
  36. Text("--").font(.caption)
  37. }
  38. }
  39. }
  40. private var color: Color {
  41. guard let lastDate = actualSuggestion?.timestamp else {
  42. return Color(UIColor(named: "LoopGray")!)
  43. }
  44. let delta = timerDate.timeIntervalSince(lastDate) - Config.lag
  45. if delta <= 5.minutes.timeInterval {
  46. return Color(UIColor(named: "LoopGreen")!)
  47. } else if delta <= 10.minutes.timeInterval {
  48. return Color(UIColor(named: "LoopYellow")!)
  49. } else {
  50. return Color(UIColor(named: "LoopRed")!)
  51. }
  52. }
  53. func mask(in rect: CGRect) -> Path {
  54. var path = Rectangle().path(in: rect)
  55. if !closedLoop {
  56. path.addPath(Rectangle().path(in: CGRect(x: rect.minX, y: rect.midY - 5, width: rect.width, height: 10)))
  57. }
  58. return path
  59. }
  60. private var actualSuggestion: Suggestion? {
  61. if closedLoop, suggestion?.rate != nil || suggestion?.units != nil {
  62. return enactedSuggestion
  63. } else {
  64. return suggestion
  65. }
  66. }
  67. }
  68. extension View {
  69. func animateForever(
  70. using animation: Animation = Animation.easeInOut(duration: 1),
  71. autoreverses: Bool = false,
  72. _ action: @escaping () -> Void
  73. ) -> some View {
  74. let repeated = animation.repeatForever(autoreverses: autoreverses)
  75. return onAppear {
  76. withAnimation(repeated) {
  77. action()
  78. }
  79. }
  80. }
  81. }