LoopView.swift 3.3 KB

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