LoopView.swift 3.5 KB

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