LoopView.swift 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import CoreData
  2. import SwiftDate
  3. import SwiftUI
  4. import UIKit
  5. struct LoopView: View {
  6. @Environment(\.colorScheme) var colorScheme
  7. private enum Config {
  8. static let lag: TimeInterval = 30
  9. }
  10. let closedLoop: Bool
  11. let timerDate: Date
  12. let isLooping: Bool
  13. let lastLoopDate: Date
  14. let manualTempBasal: Bool
  15. let determination: [OrefDetermination]
  16. private let rect = CGRect(x: 0, y: 0, width: 18, height: 18)
  17. var body: some View {
  18. loopStatusWithMinutes
  19. .padding(.vertical, 5)
  20. .padding(.horizontal, 10)
  21. .overlay(
  22. Capsule()
  23. .stroke(color.opacity(0.4), lineWidth: 2)
  24. )
  25. }
  26. private var loopStatusWithMinutes: some View {
  27. HStack(alignment: .center) {
  28. ZStack {
  29. Image(systemName: "circle")
  30. .mask(mask(in: rect).fill(style: FillStyle(eoFill: true)))
  31. if isLooping {
  32. ProgressView()
  33. }
  34. }
  35. if isLooping {
  36. Text("looping")
  37. } else if manualTempBasal {
  38. Text("Manual")
  39. } else if determination.first?
  40. .deliverAt !=
  41. nil
  42. {
  43. // previously the .timestamp property was used here because this only gets updated when the reportenacted function in the aps manager gets called
  44. Text(timeString)
  45. } else {
  46. Text("--")
  47. }
  48. }
  49. .strikethrough(!closedLoop || manualTempBasal, pattern: .solid, color: color)
  50. .font(.callout).fontWeight(.bold).fontDesign(.rounded)
  51. .foregroundColor(color)
  52. }
  53. private var timeString: String {
  54. let minAgo = Int((timerDate.timeIntervalSince(lastLoopDate) - Config.lag) / 60) + 1
  55. if minAgo > 1440 {
  56. return "--"
  57. }
  58. return "\(minAgo) " + NSLocalizedString("min", comment: "Minutes ago since last loop")
  59. }
  60. private var color: Color {
  61. guard determination.first?.timestamp != nil
  62. else {
  63. // previously the .timestamp property was used here because this only gets updated when the reportenacted function in the aps manager gets called
  64. return .secondary
  65. }
  66. guard manualTempBasal == false else {
  67. return .loopManualTemp
  68. }
  69. guard closedLoop == true else {
  70. return .secondary
  71. }
  72. let delta = timerDate.timeIntervalSince(lastLoopDate) - Config.lag
  73. if delta <= 5.minutes.timeInterval {
  74. guard determination.first?.timestamp != nil else {
  75. return .loopYellow
  76. }
  77. return .loopGreen
  78. } else if delta <= 10.minutes.timeInterval {
  79. return .loopYellow
  80. } else {
  81. return .loopRed
  82. }
  83. }
  84. func mask(in rect: CGRect) -> Path {
  85. var path = Rectangle().path(in: rect)
  86. if !closedLoop || manualTempBasal {
  87. path.addPath(Rectangle().path(in: CGRect(x: rect.minX, y: rect.midY - 4, width: rect.width, height: 8)))
  88. }
  89. return path
  90. }
  91. }
  92. extension View {
  93. func animateForever(
  94. using animation: Animation = Animation.easeInOut(duration: 1),
  95. autoreverses: Bool = false,
  96. _ action: @escaping () -> Void
  97. ) -> some View {
  98. let repeated = animation.repeatForever(autoreverses: autoreverses)
  99. return onAppear {
  100. withAnimation(repeated) {
  101. action()
  102. }
  103. }
  104. }
  105. }