SPB Git

Repository Public

Common.swift 6.6 KB 133 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Components/Common.swift3// Description: Shared small views: empty state, download indicator, quality badge, toasts, status banner.4import SwiftUI5import SPMusicCore67struct EmptyState: View {8    let symbol: String9    let title: String10    var message: String = ""11    var actionTitle: String? = nil12    var action: (() -> Void)? = nil13    var body: some View {14        VStack(spacing: 10) {15            Image(systemName: symbol).font(.system(size: 44, weight: .light)).foregroundStyle(.tertiary)16            Text(title).font(.title3.weight(.semibold))17            if !message.isEmpty { Text(message).font(.callout).foregroundStyle(.secondary).multilineTextAlignment(.center).frame(maxWidth: 420) }18            if let actionTitle, let action { Button(actionTitle, action: action).buttonStyle(.borderedProminent).padding(.top, 6) }19        }20        .padding(40).frame(maxWidth: .infinity, maxHeight: .infinity)21    }22}2324/// Where a track stands with respect to this Mac: downloaded, on its way, waiting, failed — or nothing.25struct DownloadIndicator: View {26    @Environment(AppModel.self) private var model27    let track: Track28    var body: some View {29        Group {30            if let p = model.liveProgress[track.id] {31                ProgressRing(fraction: p.fraction).frame(width: 14, height: 14).help("Téléchargement : \(Int(p.fraction * 100)) %")32            } else {33                switch track.downloadState {34                case .done: Image(systemName: "arrow.down.circle.fill").foregroundStyle(.secondary).help(track.removedRemotely ? "Sur ce Mac (retiré du serveur)" : "Téléchargé sur ce Mac")35                case .queued, .downloading: Image(systemName: "arrow.down.circle.dotted").foregroundStyle(.tertiary).help("En attente de téléchargement")36                case .paused: Image(systemName: "pause.circle").foregroundStyle(.tertiary).help("Téléchargement en pause")37                case .failed: Image(systemName: "exclamationmark.circle").foregroundStyle(.orange).help("Le téléchargement a échoué")38                case nil: Color.clear39                }40            }41        }42        .font(.system(size: 13)).frame(width: 16, height: 16)43    }44}4546struct ProgressRing: View {47    let fraction: Double48    var body: some View {49        ZStack {50            Circle().stroke(.quaternary, lineWidth: 2)51            Circle().trim(from: 0, to: max(0.02, min(1, fraction))).stroke(Color.accentColor, style: StrokeStyle(lineWidth: 2, lineCap: .round)).rotationEffect(.degrees(-90))52        }53        .animation(.linear(duration: 0.2), value: fraction)54    }55}5657struct QualityBadge: View {58    let track: Track59    var body: some View {60        if Format.isHiRes(track) { badge("Hi-Res", .orange) } else if Format.isLossless(track) { badge("Lossless", .secondary) }61    }62    private func badge(_ text: String, _ color: Color) -> some View {63        Text(text).font(.system(size: 9, weight: .bold)).padding(.horizontal, 4).padding(.vertical, 1.5)64            .overlay(RoundedRectangle(cornerRadius: 3).strokeBorder(color.opacity(0.7), lineWidth: 1)).foregroundStyle(color)65    }66}6768struct SectionHeader: View {69    let title: String70    var trailing: String? = nil71    var action: (() -> Void)? = nil72    var body: some View {73        HStack(alignment: .firstTextBaseline) {74            Text(title).font(.title2.weight(.bold))75            Spacer()76            if let trailing, let action { Button(trailing, action: action).buttonStyle(.plain).foregroundStyle(Color.accentColor).font(.callout) }77        }78    }79}8081struct ToastOverlay: View {82    @Environment(AppModel.self) private var model83    var body: some View {84        VStack(spacing: 8) {85            ForEach(model.toasts) { t in86                HStack(spacing: 8) {87                    Image(systemName: t.isError ? "exclamationmark.triangle.fill" : "checkmark.circle.fill").foregroundStyle(t.isError ? .orange : .green)88                    Text(t.text).font(.callout).fixedSize(horizontal: false, vertical: true)89                }90                .padding(.horizontal, 14).padding(.vertical, 9).frame(maxWidth: 460)91                .background(.regularMaterial, in: RoundedRectangle(cornerRadius: 10, style: .continuous))92                .shadow(color: .black.opacity(0.18), radius: 10, y: 4)93                .transition(.move(edge: .top).combined(with: .opacity))94            }95        }96        .padding(.top, 10).animation(.spring(duration: 0.3), value: model.toasts)97        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top).allowsHitTesting(false)98    }99}100101/// The discreet line that says the cluster is unreachable, the session expired, or the library folder is missing.102struct StatusBanner: View {103    @Environment(AppModel.self) private var model104    var body: some View {105        if !model.libraryRootAvailable {106            banner("externaldrive.badge.xmark", "Le dossier de la bibliothèque est introuvable (disque externe débranché ?). Vos téléchargements n’ont pas été supprimés.", .orange)107        } else if model.sessionExpired {108            banner("person.crop.circle.badge.exclamationmark", "Session expirée : votre musique téléchargée reste lisible. Reconnectez-vous pour synchroniser.", .orange, action: ("Se reconnecter", { Task { await model.signInWithPasskey() } }))109        } else if model.settings.forcedOffline {110            banner("airplane", "Mode hors ligne : seule la musique téléchargée est lue, aucune requête n’est envoyée.", .secondary, action: ("Revenir en ligne", { model.setForcedOffline(false) }))111        } else if !model.isOnline {112            banner("icloud.slash", "Serveur injoignable : lecture de la musique téléchargée. La synchronisation reprendra toute seule.", .secondary)113        }114    }115    private func banner(_ symbol: String, _ text: String, _ color: Color, action: (String, () -> Void)? = nil) -> some View {116        HStack(spacing: 8) {117            Image(systemName: symbol).foregroundStyle(color)118            Text(text).font(.callout).foregroundStyle(.secondary).lineLimit(2)119            Spacer(minLength: 8)120            if let action { Button(action.0, action: action.1).controlSize(.small) }121        }122        .padding(.horizontal, 16).padding(.vertical, 7).background(.bar)123        .overlay(alignment: .bottom) { Divider() }124    }125}126127extension View {128    /// Reloads `load` when the view appears and every time the local library changes.129    func libraryTask(_ model: AppModel, extra: String = "", _ load: @escaping () async -> Void) -> some View {130        task(id: "\(model.libraryRevision)-\(model.downloadedOnly)-\(extra)") { await load() }131    }132}133
Files main
Menu

Theme