LoopView.swift 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import SwiftDate
  2. import SwiftUI
  3. import UIKit
  4. struct LoopView: View {
  5. @Binding var suggestion: Suggestion?
  6. private var dateFormatter: DateFormatter {
  7. let formatter = DateFormatter()
  8. formatter.timeStyle = .short
  9. return formatter
  10. }
  11. var body: some View {
  12. VStack {
  13. Circle().strokeBorder(color, lineWidth: 6).frame(width: 38, height: 38)
  14. Spacer()
  15. if let date = suggestion?.deliverAt {
  16. Text(dateFormatter.string(from: date)).font(.caption)
  17. } else {
  18. Text("--").font(.caption)
  19. }
  20. }
  21. }
  22. var color: Color {
  23. guard let lastDate = suggestion?.deliverAt else {
  24. return Color(UIColor(named: "LoopGrey")!)
  25. }
  26. let delta = Date().timeIntervalSince(lastDate)
  27. if delta <= 5.minutes.timeInterval {
  28. return Color(UIColor(named: "LoopGreen")!)
  29. } else if delta <= 10.minutes.timeInterval {
  30. return Color(UIColor(named: "LoopYellow")!)
  31. } else {
  32. return Color(UIColor(named: "LoopRed")!)
  33. }
  34. }
  35. }