SPB Git

Repository Public

PlaylistView.swift 9.1 KB 181 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Music pour macOS9// File             : Features/Playlists/PlaylistView.swift10// Description      : Playlist detail: reorder, remove, keep offline.11// Module           : Features12// Repository       : https://git.spboucher.ai/sp-music-macos13// Created          : 2026-11-1814// Last Modified    : 2026-11-1815//16// =============================================================================17// AUTHOR & PROFESSIONAL AFFILIATION18// =============================================================================19//20// Author           : Simon-Pierre Boucher21// Position         : Professor of Finance22// Institution      : Université du Québec en Outaouais (UQO)23// Department       : Département des sciences administratives24// Organization     : SP Boucher25//26// Website          : https://spboucher.ai27// Git              : https://git.spboucher.ai28//29// =============================================================================30// OFFICIAL CONTACT DIRECTORY31// =============================================================================32//33// General          : info@spboucher.ai34// Professional     : contact@spboucher.ai35// Direct           : simon@spboucher.ai36// Research         : research@spboucher.ai37// Development      : dev@spboucher.ai38// Support          : support@spboucher.ai39// Security/Admin   : admin@spboucher.ai40// Notifications    : notifications@spboucher.ai41//42// Preferred Contact: support@spboucher.ai43//44// =============================================================================45// COPYRIGHT & INTELLECTUAL PROPERTY46// =============================================================================47//48// Copyright (c) 202649// Simon-Pierre Boucher50//51// All rights reserved where applicable.52//53// This file is governed by the applicable project license,54// copyright notices, institutional agreements and relevant law.55//56// Unless expressly authorized by the applicable license or57// rights holder, copying, redistribution, sublicensing,58// publication and commercial exploitation are not permitted.59//60// Existing third-party rights and notices remain unaffected.61//62// =============================================================================63// CONFIDENTIALITY & SECURITY64// =============================================================================65//66// Classification   : Public67// Distribution     : Public repository; use is governed by the project license68// Security Contact : admin@spboucher.ai69//70// Access to this source does not independently grant permission71// to redistribute or disclose confidential materials.72//73// Do not embed credentials, private keys, access tokens,74// certificates or confidential personal information.75//76// =============================================================================77// LICENSE & MAINTENANCE78// =============================================================================79//80// License          : None specified (no license file in this repository)81// Maintainer       : Simon-Pierre Boucher82// Technical Contact: dev@spboucher.ai83//84// =============================================================================85// END OF SP BOUCHER SOURCE HEADER86// =============================================================================8788import SwiftUI89import SPMusicCore9091struct PlaylistView: View {92    @Environment(AppModel.self) private var model93    let playlistID: String94    @State private var playlist: Playlist?95    @State private var entries: [PlaylistEntry] = []96    @State private var loaded = false97    @State private var renaming = false98    @State private var name = ""99    @State private var confirmDelete = false100101    private var tracks: [Track] { entries.map(\.track) }102103    var body: some View {104        ZStack {105            Color.clear106            if let playlist {107                List {108                    header(playlist).listRowSeparator(.hidden).listRowInsets(EdgeInsets(top: 20, leading: 20, bottom: 14, trailing: 20))109                    if entries.isEmpty {110                        Text("Cette liste est vide. Glissez-y des titres, ou utilisez « Ajouter à une liste » depuis un titre ou un album.").foregroundStyle(.secondary).padding(.vertical, 30).frame(maxWidth: .infinity).listRowSeparator(.hidden)111                    }112                    ForEach(Array(entries.enumerated()), id: \.element.id) { i, e in113                        TrackRow(track: e.track, number: i + 1) { model.play(tracks, at: i) }114                            .contextMenu { TrackMenu(tracks: [e.track]) { remove(e) } }115                            .listRowInsets(EdgeInsets(top: 0, leading: 12, bottom: 0, trailing: 12)).listRowSeparator(.hidden)116                    }117                    .onMove { from, to in118                        entries.move(fromOffsets: from, toOffset: to)119                        model.setPlaylistOrder(playlistID, entryIDs: entries.map(\.id))120                    }121                    .onDelete { idx in122                        entries.remove(atOffsets: idx)123                        model.setPlaylistOrder(playlistID, entryIDs: entries.map(\.id))124                    }125                }126                .listStyle(.plain)127                .dropDestination(for: String.self) { ids, _ in128                    Task { if let s = model.store { model.addToPlaylist(playlistID, tracks: await s.tracks(ids: ids)) } }129                    return true130                }131            } else if loaded { EmptyState(symbol: "music.note.list", title: "Liste introuvable", message: "Elle a peut-être été supprimée sur un autre appareil.") }132        }133        .navigationTitle(playlist?.name ?? "")134        .libraryTask(model, extra: playlistID) {135            guard let s = model.store else { return }136            playlist = await s.playlist(playlistID); entries = await s.playlistEntries(playlistID); loaded = true137        }138        .alert("Renommer la liste", isPresented: $renaming) {139            TextField("Nom", text: $name)140            Button("Renommer") { model.renamePlaylist(playlistID, name: name) }141            Button("Annuler", role: .cancel) {}142        }143        .confirmationDialog("Supprimer cette liste ?", isPresented: $confirmDelete) {144            Button("Supprimer la liste", role: .destructive) { model.deletePlaylist(playlistID) }145        } message: { Text("Les titres restent dans votre bibliothèque et sur ce Mac.") }146    }147148    private func remove(_ e: PlaylistEntry) {149        entries.removeAll { $0.id == e.id }150        model.setPlaylistOrder(playlistID, entryIDs: entries.map(\.id))151    }152153    private func header(_ p: Playlist) -> some View {154        HStack(alignment: .bottom, spacing: 20) {155            MosaicArtwork(artworkIDs: p.artworkIDs, size: 180, cornerRadius: 10).shadow(color: .black.opacity(0.22), radius: 14, y: 7)156            VStack(alignment: .leading, spacing: 8) {157                Text("LISTE DE LECTURE").font(.caption.weight(.semibold)).foregroundStyle(.secondary)158                Text(p.name).font(.system(size: 30, weight: .bold)).lineLimit(2)159                Text("\(entries.count) titres · \(Format.longDuration(ms: tracks.reduce(0) { $0 + $1.durationMs }))" + (p.pendingCreation ? " · pas encore envoyée au cluster" : "")).font(.callout).foregroundStyle(.secondary)160                HStack(spacing: 10) {161                    Button { model.play(tracks) } label: { Label("Lire", systemImage: "play.fill").frame(minWidth: 70) }.buttonStyle(.borderedProminent).controlSize(.large).disabled(entries.isEmpty)162                    Button { model.play(tracks, shuffle: true) } label: { Label("Aléatoire", systemImage: "shuffle") }.controlSize(.large).disabled(entries.isEmpty)163                    if p.pinned {164                        Button { model.removePlaylistDownload(playlistID) } label: {165                            Label(p.downloadedCount >= entries.count ? "Téléchargée" : "\(p.downloadedCount)/\(entries.count)", systemImage: p.downloadedCount >= entries.count ? "checkmark.circle.fill" : "arrow.down.circle.dotted")166                        }167                        .controlSize(.large).help("Cette liste est gardée hors ligne : les titres qu’on y ajoute sont téléchargés aussi. Cliquez pour arrêter.")168                    } else {169                        Button { model.downloadPlaylist(playlistID) } label: { Label("Télécharger la liste", systemImage: "arrow.down.circle") }.controlSize(.large).disabled(entries.isEmpty || !model.isOnline && p.downloadedCount < entries.count)170                    }171                    Menu {172                        Button("Renommer…") { name = p.name; renaming = true }173                        Button("Supprimer…", role: .destructive) { confirmDelete = true }174                    } label: { Image(systemName: "ellipsis") }.menuStyle(.borderlessButton).frame(width: 30)175                }176                .padding(.top, 6)177            }178        }179    }180}181
Files main
Menu

Theme