LoopView.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 suggestion: Suggestion?
  10. @Binding var enactedSuggestion: Suggestion?
  11. @Binding var closedLoop: Bool
  12. @Binding var timerDate: Date
  13. @Binding var isLooping: Bool
  14. @Binding var lastLoopDate: Date
  15. @Binding var manualTempBasal: Bool
  16. private var dateFormatter: DateFormatter {
  17. let formatter = DateFormatter()
  18. formatter.timeStyle = .short
  19. return formatter
  20. }
  21. private let rect = CGRect(x: 0, y: 0, width: 18, height: 18)
  22. var body: some View {
  23. HStack(alignment: .center) {
  24. ZStack {
  25. Image(systemName: "circle")
  26. .mask(mask(in: rect).fill(style: FillStyle(eoFill: true)))
  27. if isLooping {
  28. ProgressView()
  29. }
  30. }
  31. if isLooping {
  32. Text("looping")
  33. } else if manualTempBasal {
  34. Text("Manual")
  35. } else if actualSuggestion?.timestamp != nil {
  36. Text(timeString)
  37. } else {
  38. Text("--")
  39. }
  40. }
  41. .strikethrough(!closedLoop || manualTempBasal, pattern: .solid, color: color)
  42. .font(.system(size: 16, weight: .bold, design: .rounded))
  43. .foregroundColor(color)
  44. }
  45. private var timeString: String {
  46. let minAgo = Int((timerDate.timeIntervalSince(lastLoopDate) - Config.lag) / 60) + 1
  47. if minAgo > 1440 {
  48. return "--"
  49. }
  50. return "\(minAgo) " + NSLocalizedString("min", comment: "Minutes ago since last loop")
  51. }
  52. private var color: Color {
  53. guard actualSuggestion?.timestamp != nil else {
  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 actualSuggestion?.deliverAt != 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. private var actualSuggestion: Suggestion? {
  82. if closedLoop, enactedSuggestion?.recieved == true {
  83. return enactedSuggestion ?? suggestion
  84. } else {
  85. return suggestion
  86. }
  87. }
  88. }
  89. extension View {
  90. func animateForever(
  91. using animation: Animation = Animation.easeInOut(duration: 1),
  92. autoreverses: Bool = false,
  93. _ action: @escaping () -> Void
  94. ) -> some View {
  95. let repeated = animation.repeatForever(autoreverses: autoreverses)
  96. return onAppear {
  97. withAnimation(repeated) {
  98. action()
  99. }
  100. }
  101. }
  102. }