LoopView.swift 3.6 KB

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