LoopView.swift 3.3 KB

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