Repository Public
1// SP Music iOS2// File: Components/DownloadButton.swift3// Description: Download state glyph, progress ring and the download button of albums, playlists and the library.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai78import SPMusicCore9import SwiftUI1011/// State of one track's download, as a small glyph (in rows).12struct DownloadGlyph: View {13 @Environment(AppEnvironment.self) private var env14 let trackId: String15 var body: some View {16 if let p = env.downloadsModel.items[trackId], let state = p.state {17 Group {18 switch state {19 case .done: Image(systemName: "arrow.down.circle.fill").foregroundStyle(Color.spEmber).accessibilityLabel("Téléchargé")20 case .running: ProgressRing(fraction: p.fraction).frame(width: 15, height: 15).accessibilityLabel("Téléchargement en cours")21 case .queued, .preparing: Image(systemName: "clock").foregroundStyle(Color.spMuted).accessibilityLabel("En attente de téléchargement")22 case .paused: Image(systemName: "pause.circle").foregroundStyle(Color.spMuted).accessibilityLabel("Téléchargement en pause")23 case .failed: Image(systemName: "exclamationmark.circle").foregroundStyle(.yellow).accessibilityLabel("Échec du téléchargement")24 }25 }26 .font(.caption)27 }28 }29}3031struct ProgressRing: View {32 var fraction: Double33 var lineWidth: CGFloat = 2.534 var body: some View {35 ZStack {36 Circle().stroke(Color.spLine, lineWidth: lineWidth)37 Circle().trim(from: 0, to: max(0.03, fraction)).stroke(Color.spEmber, style: StrokeStyle(lineWidth: lineWidth, lineCap: .round)).rotationEffect(.degrees(-90))38 }39 .animation(.linear(duration: 0.25), value: fraction)40 }41}4243/// "Télécharger" for an album, a playlist or the library: shows progress, lets the listener remove the download,44/// and asks before a large transfer on a cellular connection.45struct DownloadButton: View {46 @Environment(AppEnvironment.self) private var env47 let kind: PinKind48 let id: String49 var label: String = "Télécharger"50 var compact = false51 @State private var status = PinStatus()52 @State private var confirmBytes: Int64?53 @State private var confirmRemove = false5455 var body: some View {56 Button { Task { await tapped() } } label: {57 HStack(spacing: 8) {58 icon59 if !compact { Text(title).lineLimit(1) }60 }61 }62 .accessibilityIdentifier("download-\(kind.rawValue)")63 .accessibilityLabel(title)64 .task(id: "\(env.downloadsModel.version)|\(env.libraryVersion)|\(id)") { status = await env.downloads.pinStatus(kind, id) }65 .task(id: status.active > 0) {66 // While transfers run, refresh the aggregate a few times a second for a smooth ring.67 while status.active > 0, !Task.isCancelled { try? await Task.sleep(for: .milliseconds(400)); status = await env.downloads.pinStatus(kind, id) }68 }69 .confirmationDialog("Télécharger sur le réseau cellulaire ?", isPresented: Binding(get: { confirmBytes != nil }, set: { if !$0 { confirmBytes = nil } }), titleVisibility: .visible) {70 Button("Télécharger (\(Format.bytes(confirmBytes ?? 0)))") { Task { _ = await env.download(kind, id, confirmed: true) } }71 Button("Annuler", role: .cancel) {}72 } message: { Text("Ce téléchargement utilisera environ \(Format.bytes(confirmBytes ?? 0)) de données cellulaires.") }73 .confirmationDialog("Supprimer le téléchargement ?", isPresented: $confirmRemove, titleVisibility: .visible) {74 Button("Supprimer de cet appareil", role: .destructive) { Task { await env.removeDownload(kind, id) } }75 Button("Annuler", role: .cancel) {}76 } message: { Text("Les fichiers restent sur MacLustr. Ceux qu'un autre album ou une autre liste téléchargés utilisent sont conservés.") }77 }7879 private var title: String {80 if status.complete { return "Téléchargé" }81 if status.pinned && status.active > 0 { return "\(status.done)/\(status.total)" }82 if status.pinned && status.failed > 0 { return "Réessayer" }83 if status.pinned && status.done < status.total { return "Reprendre" }84 return label85 }86 @ViewBuilder private var icon: some View {87 if status.complete { Image(systemName: "checkmark.circle.fill") }88 else if status.pinned && status.active > 0 { ProgressRing(fraction: status.fraction).frame(width: 18, height: 18) }89 else if status.pinned && status.failed > 0 { Image(systemName: "exclamationmark.arrow.circlepath") }90 else { Image(systemName: "arrow.down.circle") }91 }92 private func tapped() async {93 if status.complete || (status.pinned && status.active > 0) { confirmRemove = true; return }94 if status.pinned && status.failed > 0 { await env.downloads.resumeAll(); await env.downloads.enqueueWanted(); return }95 confirmBytes = await env.download(kind, id)96 }97}98