LoopView.swift 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 minutesAgo = TimeAgoFormatter.minutesAgoValue(from: lastLoopDate)
  53. if minutesAgo > 1440 {
  54. return "--"
  55. } else {
  56. return TimeAgoFormatter.minutesAgo(from: lastLoopDate)
  57. }
  58. }
  59. private var color: Color {
  60. guard determination.first?.timestamp != 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?.timestamp != 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. }
  84. extension View {
  85. func animateForever(
  86. using animation: Animation = Animation.easeInOut(duration: 1),
  87. autoreverses: Bool = false,
  88. _ action: @escaping () -> Void
  89. ) -> some View {
  90. let repeated = animation.repeatForever(autoreverses: autoreverses)
  91. return onAppear {
  92. withAnimation(repeated) {
  93. action()
  94. }
  95. }
  96. }
  97. }