RemainingTimeText.swift 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // LoopFollow
  2. // RemainingTimeText.swift
  3. import SwiftUI
  4. /// Remaining time for an active override / temp target: a live countdown when
  5. /// 2 hours or less remain, otherwise a static "29d 23h"-style duration —
  6. /// a multi-day ticker reads poorly.
  7. struct RemainingTimeText: View {
  8. let endAt: TimeInterval
  9. static let tickerMaxRemaining: TimeInterval = 2 * 3600
  10. private static let longFormatter: DateComponentsFormatter = {
  11. let f = DateComponentsFormatter()
  12. f.unitsStyle = .abbreviated
  13. f.allowedUnits = [.day, .hour, .minute]
  14. f.maximumUnitCount = 2
  15. return f
  16. }()
  17. var body: some View {
  18. let end = Date(timeIntervalSince1970: endAt)
  19. let remaining = end.timeIntervalSinceNow
  20. if remaining <= 0 {
  21. EmptyView()
  22. } else if remaining <= Self.tickerMaxRemaining {
  23. Text(timerInterval: Date() ... end, countsDown: true)
  24. .monospacedDigit()
  25. } else {
  26. Text(Self.longFormatter.string(from: remaining) ?? "")
  27. }
  28. }
  29. }