SPB Git

Repository Public

SidebarView.swift 5.8 KB 102 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Components/SidebarView.swift3// Description: Sidebar: library sections, playlists, and the synchronisation footer.4import SwiftUI5import UniformTypeIdentifiers6import SPMusicCore78struct SidebarView: View {9    @Environment(AppModel.self) private var model10    @State private var renaming: Playlist?11    @State private var newName = ""12    @State private var creating = false13    @State private var deleting: Playlist?1415    var body: some View {16        @Bindable var model = model17        List(selection: Binding(get: { model.selection }, set: { if let v = $0 { model.select(v) } })) {18            Section {19                Label("Accueil", systemImage: "house").tag(SidebarItem.home)20            }21            Section("Bibliothèque") {22                Label("Albums", systemImage: "square.stack").tag(SidebarItem.albums)23                Label("Artistes", systemImage: "music.mic").tag(SidebarItem.artists)24                Label("Titres", systemImage: "music.note").tag(SidebarItem.tracks)25                Label("Favoris", systemImage: "heart").tag(SidebarItem.favorites)26                Label("Écoutés récemment", systemImage: "clock").tag(SidebarItem.history)27            }28            Section("Sur ce Mac") {29                Label("Téléchargements", systemImage: "arrow.down.circle").tag(SidebarItem.downloads)30                    .badge(model.activeDownloads > 0 ? Text("\(model.activeDownloads)") : nil)31            }32            Section("Listes de lecture") {33                ForEach(model.playlists) { p in34                    Label { Text(p.name).lineLimit(1) } icon: { Image(systemName: p.pinned ? "arrow.down.circle" : "music.note.list") }35                        .tag(SidebarItem.playlist(p.id))36                        .contextMenu {37                            Button("Lire") { Task { await model.playPlaylist(p.id) } }38                            Divider()39                            if p.pinned { Button("Ne plus garder hors ligne") { model.removePlaylistDownload(p.id) } } else { Button("Télécharger la liste") { model.downloadPlaylist(p.id) } }40                            Button("Renommer…") { newName = p.name; renaming = p }41                            Button("Supprimer…", role: .destructive) { deleting = p }42                        }43                        .dropDestination(for: String.self) { ids, _ in44                            Task { if let s = model.store { model.addToPlaylist(p.id, tracks: await s.tracks(ids: ids)) } }45                            return true46                        }47                }48                Button { newName = ""; creating = true } label: { Label("Nouvelle liste…", systemImage: "plus") }.buttonStyle(.plain).foregroundStyle(.secondary)49            }50            if model.isAdmin {51                Section("Cluster") { Label("Administration", systemImage: "server.rack").tag(SidebarItem.admin) }52            }53        }54        .listStyle(.sidebar)55        .safeAreaInset(edge: .bottom) { SyncFooter() }56        .alert("Nouvelle liste de lecture", isPresented: $creating) {57            TextField("Nom", text: $newName)58            Button("Créer") { Task { if let id = await model.createPlaylist(name: newName) { model.select(.playlist(id)) } } }59            Button("Annuler", role: .cancel) {}60        }61        .alert("Renommer la liste", isPresented: Binding(get: { renaming != nil }, set: { if !$0 { renaming = nil } })) {62            TextField("Nom", text: $newName)63            Button("Renommer") { if let p = renaming { model.renamePlaylist(p.id, name: newName) } }64            Button("Annuler", role: .cancel) {}65        }66        .confirmationDialog("Supprimer « \(deleting?.name ?? "") » ?", isPresented: Binding(get: { deleting != nil }, set: { if !$0 { deleting = nil } })) {67            Button("Supprimer la liste", role: .destructive) { if let p = deleting { model.deletePlaylist(p.id) } }68        } message: { Text("Les titres restent dans votre bibliothèque et sur ce Mac.") }69    }70}7172/// Bottom of the sidebar: who is signed in, and whether the library is in step with the cluster.73struct SyncFooter: View {74    @Environment(AppModel.self) private var model75    var body: some View {76        HStack(spacing: 8) {77            Circle().fill(model.settings.forcedOffline ? Color.gray : model.sessionExpired ? .orange : model.isOnline ? .green : .gray).frame(width: 7, height: 7)78            VStack(alignment: .leading, spacing: 0) {79                Text(model.account?.displayName ?? "").font(.caption.weight(.medium)).lineLimit(1)80                Text(status).font(.caption2).foregroundStyle(.secondary).lineLimit(1)81            }82            Spacer(minLength: 4)83            if model.syncing { ProgressView().controlSize(.mini) }84            else {85                Button { Task { await model.syncNow() } } label: { Image(systemName: "arrow.triangle.2.circlepath").font(.system(size: 11)) }86                    .buttonStyle(.plain).foregroundStyle(.secondary).disabled(!model.isOnline || model.sessionExpired).help("Synchroniser maintenant").accessibilityLabel("Synchroniser maintenant")87            }88        }89        .padding(.horizontal, 14).padding(.vertical, 8).background(.bar)90        .overlay(alignment: .top) { Divider() }91    }92    private var status: String {93        if model.settings.forcedOffline { return "Mode hors ligne" }94        if model.sessionExpired { return "Session expirée" }95        if !model.isOnline { return model.pendingChanges > 0 ? "Hors ligne · \(model.pendingChanges) modification(s) en attente" : "Hors ligne" }96        if model.syncing { return "Synchronisation…" }97        if model.pendingChanges > 0 { return "\(model.pendingChanges) modification(s) à envoyer" }98        if let d = model.lastSync { return "Synchronisé à " + d.formatted(date: .omitted, time: .shortened) }99        return "Connecté au cluster"100    }101}102
Files main
Menu

Theme