LoopView.swift 3.2 KB

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