LoopView.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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: 14, height: 14)
  21. var body: some View {
  22. HStack(alignment: .center) {
  23. Rectangle().frame(width: UIScreen.main.bounds.width / 2.5, height: 2, alignment: .leading).foregroundColor(color)
  24. // ZStack {
  25. // Image(systemName: "circle.fill")
  26. // .resizable()
  27. // .frame(width: rect.width, height: rect.height, alignment: .center)
  28. // .foregroundColor(color)
  29. // .mask(mask(in: rect).fill(style: FillStyle(eoFill: true)))
  30. if isLooping {
  31. ProgressView()
  32. .foregroundColor(Color.loopGreen)
  33. }
  34. // }
  35. if isLooping {
  36. Text("looping").font(.caption).fontWeight(.bold)
  37. } else if manualTempBasal {
  38. Text("Manual").font(.caption).fontWeight(.bold)
  39. } else if actualSuggestion?.timestamp != nil {
  40. Text(timeString).font(.caption).fontWeight(.bold)
  41. } else {
  42. Text("--").font(.caption).fontWeight(.bold)
  43. }
  44. Rectangle().frame(width: UIScreen.main.bounds.width / 2.5, height: 2, alignment: .trailing).foregroundColor(color)
  45. }
  46. }
  47. private var timeString: String {
  48. let minAgo = Int((timerDate.timeIntervalSince(lastLoopDate) - Config.lag) / 60) + 1
  49. if minAgo > 1440 {
  50. return "--"
  51. }
  52. return "\(minAgo) " + NSLocalizedString("min", comment: "Minutes ago since last loop")
  53. }
  54. private var color: Color {
  55. guard actualSuggestion?.timestamp != nil else {
  56. return .loopGray
  57. }
  58. guard manualTempBasal == false else {
  59. return .loopManualTemp
  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 - 5, width: rect.width, height: 10)))
  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. }