Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Packages/LocalChatUI/Sources/Features/Models/DownloadsView.swift3// Description: Live download queue: progress, measured speed, ETA, verification, pause / resume / cancel / retry.4import SwiftUI5import LocalChatCore6import LocalChatModelHub78struct DownloadsView: View {9 @Environment(AppModel.self) private var app1011 var body: some View {12 if app.downloadStates.isEmpty {13 EmptyStateView(symbol: "arrow.down.circle", title: "No downloads",14 message: "Models you download appear here with their progress. Downloads resume where they stopped and every file is verified.")15 } else {16 ScrollView {17 VStack(alignment: .leading, spacing: DS.S.md) {18 HStack {19 let active = app.downloadStates.filter { $0.phase.isActive }20 Pill("\(active.count) active", symbol: "arrow.down", tone: active.isEmpty ? .neutral : .accent)21 let speed = active.compactMap(\.bytesPerSecond).reduce(0, +)22 if speed > 0 { Pill("\(ByteFormat.string(Int64(speed)))/s", symbol: "speedometer") }23 Spacer()24 if app.downloadStates.contains(where: { $0.phase.isFinished }) {25 Button("Clear finished") { Task { await app.downloads.clearFinished() } }.buttonStyle(.lcSecondaryCompact)26 }27 }28 ForEach(app.downloadStates) { DownloadRow(state: $0) }29 }30 .padding(DS.S.lg).animation(DS.M.smooth, value: app.downloadStates.map(\.id))31 }32 }33 }34}3536private struct DownloadRow: View {37 @Environment(AppModel.self) private var app38 let state: DownloadState3940 private var failure: String? { if case .failed(let reason) = state.phase { reason } else { nil } }41 private var phaseLabel: (String, Pill.Tone, String) {42 switch state.phase {43 case .queued: ("Queued", .neutral, "clock")44 case .downloading: ("Downloading", .accent, "arrow.down")45 case .verifying: ("Verifying checksum", .accent, "checkmark.shield")46 case .paused: ("Paused", .warn, "pause.fill")47 case .failed: ("Failed", .danger, "xmark.octagon.fill")48 case .completed: ("Installed", .ok, "checkmark.circle.fill")49 case .cancelled: ("Cancelled", .neutral, "xmark")50 }51 }5253 var body: some View {54 let (label, tone, symbol) = phaseLabel55 VStack(alignment: .leading, spacing: 8) {56 HStack(alignment: .top) {57 VStack(alignment: .leading, spacing: 3) {58 Text(state.name).font(DS.F.headline).foregroundStyle(DS.C.text).lineLimit(1).truncationMode(.middle)59 Pill(label, symbol: symbol, tone: tone)60 }61 Spacer(minLength: 8)62 controls63 }64 if state.phase != .completed && state.phase != .cancelled {65 if state.phase == .verifying && state.bytesTotal == 0 { ProgressView().progressViewStyle(.linear) }66 else { ProgressBar(state.fraction, tint: failure != nil ? DS.C.danger : state.phase == .paused ? DS.C.warn : DS.C.accent, height: 6) }67 }68 HStack(spacing: 10) {69 Text("\(ByteFormat.string(state.bytesDone)) of \(state.bytesTotal > 0 ? ByteFormat.string(state.bytesTotal) : "unknown")")70 if state.bytesTotal > 0 { Text("\(Int(state.fraction * 100)) %") }71 if let s = state.bytesPerSecond, s > 0 { Text("\(ByteFormat.string(Int64(s)))/s") }72 if let eta = state.eta, state.phase == .downloading { Text("\(MetricFormat.duration(eta)) left") }73 Spacer(minLength: 0)74 Text("\(state.filesDone)/\(state.filesTotal) files")75 }76 .font(DS.F.mono(11)).foregroundStyle(DS.C.text2).lineLimit(1).minimumScaleFactor(0.8)77 if let file = state.currentFile, state.phase.isActive { Text(file).font(DS.F.micro).foregroundStyle(DS.C.muted).lineLimit(1).truncationMode(.middle) }78 if let failure { InlineNotice(.error, failure) }79 }80 .card(padding: DS.S.md)81 }8283 @ViewBuilder private var controls: some View {84 HStack(spacing: 2) {85 switch state.phase {86 case .downloading, .queued:87 IconButton("pause.fill", label: "Pause") { Task { await app.downloads.pause(id: state.id) } }88 IconButton("xmark", label: "Cancel download", tint: DS.C.danger) { Task { await app.downloads.cancel(id: state.id) } }89 case .verifying:90 ProgressView().controlSize(.small)91 case .paused:92 IconButton("play.fill", label: "Resume", tint: DS.C.accent) { Task { await app.downloads.resume(id: state.id) } }93 IconButton("xmark", label: "Cancel download", tint: DS.C.danger) { Task { await app.downloads.cancel(id: state.id) } }94 case .failed:95 IconButton("arrow.clockwise", label: "Retry", tint: DS.C.accent) { Task { await app.downloads.resume(id: state.id) } }96 IconButton("xmark", label: "Remove", tint: DS.C.danger) { Task { await app.downloads.cancel(id: state.id) } }97 case .completed, .cancelled:98 EmptyView()99 }100 }101 }102}103