| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- import SwiftUI
- /// Download button for album headers — shows aggregate download state across all tracks.
- /// States: "Download All" / "3 of 12" / "Downloaded" / "5 remaining"
- struct AlbumDownloadButton: View {
- let tracks: [Track]
- let apiClient: ChadMusicAPIClient
- @State private var downloadManager = DownloadManager.shared
- private var cloudTracks: [Track] {
- tracks.filter { $0.isCloud }
- }
- private var downloadedCount: Int {
- cloudTracks.filter { $0.downloadState == .downloaded }.count
- }
- private var downloadingCount: Int {
- cloudTracks.filter { $0.downloadState == .downloading }.count
- }
- var body: some View {
- if !cloudTracks.isEmpty {
- Button {
- handleTap()
- } label: {
- Label(buttonLabel, systemImage: buttonIcon)
- .font(.caption)
- }
- .buttonStyle(.plain)
- .foregroundStyle(.secondary)
- .fixedSize()
- }
- }
- private var buttonLabel: String {
- let total = cloudTracks.count
- if downloadedCount == total {
- return "Downloaded"
- }
- if downloadingCount > 0 {
- return "\(downloadedCount + downloadingCount) of \(total)"
- }
- if downloadedCount > 0 {
- return "\(total - downloadedCount) remaining"
- }
- return "Download All"
- }
- private var buttonIcon: String {
- if downloadedCount == cloudTracks.count {
- return "checkmark.circle.fill"
- }
- if downloadingCount > 0 {
- return "arrow.down.circle"
- }
- return "arrow.down.circle"
- }
- private func handleTap() {
- let total = cloudTracks.count
- if downloadedCount == total {
- return // All downloaded
- }
- if downloadingCount > 0 {
- // Cancel all active
- downloadManager.cancelBatch(tracks: cloudTracks)
- } else {
- // Download remaining
- downloadManager.downloadBatch(tracks: cloudTracks, apiClient: apiClient)
- }
- }
- }
|