LoopView.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: (!closedLoop || manualTempBasal) ? "circle.and.line.horizontal" : "circle")
  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. .font(.callout).fontWeight(.bold).fontDesign(.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. }
  83. extension View {
  84. func animateForever(
  85. using animation: Animation = Animation.easeInOut(duration: 1),
  86. autoreverses: Bool = false,
  87. _ action: @escaping () -> Void
  88. ) -> some View {
  89. let repeated = animation.repeatForever(autoreverses: autoreverses)
  90. return onAppear {
  91. withAnimation(repeated) {
  92. action()
  93. }
  94. }
  95. }
  96. }