LoopView.swift 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. private var dateFormatter: DateFormatter {
  15. let formatter = DateFormatter()
  16. formatter.timeStyle = .short
  17. return formatter
  18. }
  19. private let rect = CGRect(x: 0, y: 0, width: 32, height: 32)
  20. var body: some View {
  21. VStack(alignment: .center) {
  22. ZStack {
  23. Circle()
  24. .strokeBorder(color, lineWidth: 6)
  25. .frame(width: rect.width, height: rect.height)
  26. .mask(mask(in: rect).fill(style: FillStyle(eoFill: true)))
  27. if isLooping {
  28. ProgressView()
  29. }
  30. }
  31. if isLooping {
  32. Text("looping").font(.caption2)
  33. } else if actualSuggestion?.timestamp != nil {
  34. Text(timeString).font(.caption2)
  35. .foregroundColor(.secondary)
  36. } else {
  37. Text("--").font(.caption2).foregroundColor(.secondary)
  38. }
  39. }
  40. }
  41. private var timeString: String {
  42. let minAgo = Int((timerDate.timeIntervalSince(lastLoopDate) - Config.lag) / 60) + 1
  43. if minAgo > 1440 {
  44. return "--"
  45. }
  46. return "\(minAgo) min ago"
  47. }
  48. private var color: Color {
  49. guard actualSuggestion?.timestamp != nil else {
  50. return .loopGray
  51. }
  52. let delta = timerDate.timeIntervalSince(lastLoopDate) - Config.lag
  53. if delta <= 5.minutes.timeInterval {
  54. guard actualSuggestion?.deliverAt != nil else {
  55. return .loopYellow
  56. }
  57. return .loopGreen
  58. } else if delta <= 10.minutes.timeInterval {
  59. return .loopYellow
  60. } else {
  61. return .loopRed
  62. }
  63. }
  64. func mask(in rect: CGRect) -> Path {
  65. var path = Rectangle().path(in: rect)
  66. if !closedLoop {
  67. path.addPath(Rectangle().path(in: CGRect(x: rect.minX, y: rect.midY - 5, width: rect.width, height: 10)))
  68. }
  69. return path
  70. }
  71. private var actualSuggestion: Suggestion? {
  72. if closedLoop, suggestion?.rate != nil || suggestion?.units != nil {
  73. return enactedSuggestion ?? suggestion
  74. } else {
  75. return suggestion
  76. }
  77. }
  78. }
  79. extension View {
  80. func animateForever(
  81. using animation: Animation = Animation.easeInOut(duration: 1),
  82. autoreverses: Bool = false,
  83. _ action: @escaping () -> Void
  84. ) -> some View {
  85. let repeated = animation.repeatForever(autoreverses: autoreverses)
  86. return onAppear {
  87. withAnimation(repeated) {
  88. action()
  89. }
  90. }
  91. }
  92. }