Jotley / app · jotley.ai / Shared / Views / SummaryStreamingDots.swift
| 1 | import SwiftUI |
| 2 | |
| 3 | private enum SummaryLoadingTimerFormatting { |
| 4 | static func string(from interval: TimeInterval) -> String { |
| 5 | let totalSeconds = max(Int(interval.rounded(.down)), 0) |
| 6 | let minutes = totalSeconds / 60 |
| 7 | let seconds = totalSeconds % 60 |
| 8 | return String(format: "%d:%02d", minutes, seconds) |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | struct SummaryLoadingTimerView: View { |
| 13 | let startedAt: Date |
| 14 | var timeoutInterval: TimeInterval? |
| 15 | |
| 16 | private func progress(for elapsed: TimeInterval) -> Double? { |
| 17 | guard let timeoutInterval, timeoutInterval > 0 else { return nil } |
| 18 | return min(max(elapsed / timeoutInterval, 0), 1) |
| 19 | } |
| 20 | |
| 21 | private func progressColor(for progress: Double?) -> Color { |
| 22 | guard let progress else { return Color.themePrimaryAccent } |
| 23 | return progress >= 0.85 ? .orange : Color.themePrimaryAccent |
| 24 | } |
| 25 | |
| 26 | var body: some View { |
| 27 | TimelineView(.periodic(from: startedAt, by: 1)) { context in |
| 28 | let elapsed = max(context.date.timeIntervalSince(startedAt), 0) |
| 29 | let timeoutProgress = progress(for: elapsed) |
| 30 | |
| 31 | VStack(spacing: 8) { |
| 32 | Label("\(SummaryLoadingTimerFormatting.string(from: elapsed)) elapsed", systemImage: "timer") |
| 33 | .font(.caption.monospacedDigit().weight(.semibold)) |
| 34 | .foregroundColor(Color.themeTextSecondary) |
| 35 | |
| 36 | if timeoutInterval ?? 0 > 0, let timeoutProgress { |
| 37 | ProgressView(value: timeoutProgress) |
| 38 | .tint(progressColor(for: timeoutProgress)) |
| 39 | .frame(maxWidth: 220) |
| 40 | } |
| 41 | } |
| 42 | .frame(maxWidth: .infinity) |
| 43 | } |
| 44 | } |
| 45 | } |