LoopView.swift 3.3 KB

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