LoopView.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. @Binding var lastLoopDate: Date
  14. @Binding var manualTempBasal: Bool
  15. private var dateFormatter: DateFormatter {
  16. let formatter = DateFormatter()
  17. formatter.timeStyle = .short
  18. return formatter
  19. }
  20. private let rect = CGRect(x: 0, y: 0, width: 28, height: 28)
  21. var body: some View {
  22. VStack(alignment: .center) {
  23. ZStack {
  24. Circle()
  25. .strokeBorder(color, lineWidth: 5)
  26. .frame(width: rect.width, height: rect.height, alignment: .bottom)
  27. .mask(mask(in: rect).fill(style: FillStyle(eoFill: true)))
  28. if isLooping {
  29. ProgressView()
  30. }
  31. }
  32. if isLooping {
  33. Text("looping").font(.caption2)
  34. } else if manualTempBasal {
  35. Text("Manual").font(.caption2)
  36. } else if actualSuggestion?.timestamp != nil {
  37. Text(timeString).font(.caption2)
  38. .foregroundColor(.secondary)
  39. } else {
  40. Text("--").font(.caption2).foregroundColor(.secondary)
  41. }
  42. }
  43. }
  44. private var timeString: String {
  45. let minAgo = Int((timerDate.timeIntervalSince(lastLoopDate) - Config.lag) / 60) + 1
  46. if minAgo > 1440 {
  47. return "--"
  48. }
  49. return "\(minAgo) " + NSLocalizedString("min", comment: "Minutes ago since last loop")
  50. }
  51. private var color: Color {
  52. guard actualSuggestion?.timestamp != nil else {
  53. return .loopGray
  54. }
  55. guard manualTempBasal == false else {
  56. return .loopManualTemp
  57. }
  58. let delta = timerDate.timeIntervalSince(lastLoopDate) - Config.lag
  59. if delta <= 5.minutes.timeInterval {
  60. guard actualSuggestion?.deliverAt != nil else {
  61. return .loopYellow
  62. }
  63. return .loopGreen
  64. } else if delta <= 10.minutes.timeInterval {
  65. return .loopYellow
  66. } else {
  67. return .loopRed
  68. }
  69. }
  70. func mask(in rect: CGRect) -> Path {
  71. var path = Rectangle().path(in: rect)
  72. if !closedLoop || manualTempBasal {
  73. path.addPath(Rectangle().path(in: CGRect(x: rect.minX, y: rect.midY - 5, width: rect.width, height: 10)))
  74. }
  75. return path
  76. }
  77. private var actualSuggestion: Suggestion? {
  78. if closedLoop, enactedSuggestion?.recieved == true {
  79. return enactedSuggestion ?? suggestion
  80. } else {
  81. return suggestion
  82. }
  83. }
  84. }
  85. extension View {
  86. func animateForever(
  87. using animation: Animation = Animation.easeInOut(duration: 1),
  88. autoreverses: Bool = false,
  89. _ action: @escaping () -> Void
  90. ) -> some View {
  91. let repeated = animation.repeatForever(autoreverses: autoreverses)
  92. return onAppear {
  93. withAnimation(repeated) {
  94. action()
  95. }
  96. }
  97. }
  98. }