LoopView.swift 3.2 KB

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