SPB Git

Repository Public

LibrarySidebar.swift 8.5 KB 164 lines swift

1// ORIX DJ2// File: App/Sources/Features/Library/LibrarySidebar.swift3// Description: Library sidebar: fixed sources, history sessions, crates, playlists and smart crates, with creation,4//              renaming, deletion and drops of tracks onto crates and playlists.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import ORIXDJCore9import SwiftUI1011struct LibrarySidebar: View {12    @Environment(AppModel.self) private var model13    var controller: LibraryController14    @State private var deletion: LibrarySource?1516    var body: some View {17        ScrollView {18            VStack(alignment: .leading, spacing: 1) {19                header("Library", id: "library")20                row(.all, title: String(localized: "All Tracks"), count: controller.libraryCount)21                row(.favorites, title: String(localized: "Favourites"))22                row(.history(nil), title: String(localized: "History"))23                ForEach(controller.sessions.prefix(8)) { s in24                    row(.history(s.id), title: LibrarySourceNames.sessionTitle(s), count: s.entryCount, indent: true)25                }26                row(.unanalysed, title: String(localized: "Not Analysed"))27                row(.missing, title: String(localized: "Missing Files"))2829                header("Crates", id: "crates") { controller.nameRequest = LibraryNameRequest(purpose: .newCrate, text: String(localized: "New Crate")) }30                if controller.crates.isEmpty { hint("Unordered sets of tracks. Create one, then drag tracks onto it.") }31                ForEach(controller.crates) { c in row(.crate(c.id), title: c.name, count: c.trackCount) }3233                header("Playlists", id: "playlists") { controller.nameRequest = LibraryNameRequest(purpose: .newPlaylist, text: String(localized: "New Playlist")) }34                if controller.playlists.isEmpty { hint("Ordered lists. Drag rows inside a playlist to reorder it.") }35                ForEach(controller.playlists) { p in row(.playlist(p.id), title: p.name, count: p.trackCount) }3637                header("Smart Crates", id: "smart") { controller.newSmartCrate() }38                if controller.smartCrates.isEmpty { hint("Filled automatically by rules such as tempo, key, rating or tags.") }39                ForEach(controller.smartCrates) { c in row(.smartCrate(c.id), title: c.name) }40            }41            .padding(.horizontal, ORIX.Space.xs).padding(.vertical, ORIX.Space.xs)42        }43        .background(ORIX.Palette.surface)44        .accessibilityIdentifier("library-sidebar")45        .confirmationDialog(deletionTitle, isPresented: Binding(get: { deletion != nil }, set: { if !$0 { deletion = nil } })) {46            Button("Delete", role: .destructive) { if let d = deletion { controller.delete(d, model: model) }; deletion = nil }47            Button("Cancel", role: .cancel) { deletion = nil }48        } message: { Text("Tracks stay in the library, and audio files are never touched.") }49    }5051    private var deletionTitle: LocalizedStringKey {52        guard let d = deletion else { return "Delete?" }53        return "Delete \"\(LibrarySourceNames.title(d, controller: controller))\"?"54    }5556    private func header(_ title: LocalizedStringKey, id: String, add: (() -> Void)? = nil) -> some View {57        HStack {58            SectionLabel(title)59            Spacer()60            if let add {61                Button(action: add) { Image(systemName: "plus").font(.system(size: 10, weight: .bold)).frame(width: 18, height: 18).contentShape(Rectangle()) }62                    .buttonStyle(.plain).foregroundStyle(ORIX.Palette.textSecondary)63                    .accessibilityLabel(Text("New")).accessibilityHint(Text(title))64                    .accessibilityIdentifier("library-add-\(id)")65            }66        }67        .padding(.horizontal, 6).padding(.top, 12).padding(.bottom, 3)68    }6970    private func hint(_ text: LocalizedStringKey) -> some View {71        Text(text).font(.system(size: 10.5)).foregroundStyle(ORIX.Palette.textTertiary).padding(.horizontal, 6).padding(.bottom, 2)72            .fixedSize(horizontal: false, vertical: true)73    }7475    private func row(_ source: LibrarySource, title: String, count: Int? = nil, indent: Bool = false) -> some View {76        LibrarySidebarRow(source: source, title: title, count: count, indent: indent, selected: LibrarySource(scope: model.libraryScope) == source && controller.smartDraft == nil,77                          controller: controller, onDelete: { deletion = source })78    }79}8081private struct LibrarySidebarRow: View {82    @Environment(AppModel.self) private var model83    var source: LibrarySource84    var title: String85    var count: Int?86    var indent: Bool87    var selected: Bool88    var controller: LibraryController89    var onDelete: () -> Void90    @State private var targeted = false9192    private var acceptsTracks: Bool {93        switch source { case .crate, .playlist, .favorites: true; default: false }94    }9596    private var editable: Bool {97        switch source { case .crate, .playlist, .smartCrate, .history(.some): true; default: false }98    }99100    var body: some View {101        Button {102            controller.smartDraft = nil103            model.libraryScope = source.scope104        } label: {105            HStack(spacing: 7) {106                Image(systemName: selected && source == .favorites ? "heart.fill" : source.symbol).font(.system(size: 11, weight: .medium))107                    .foregroundStyle(selected ? ORIX.Palette.accent : ORIX.Palette.textSecondary).frame(width: 16)108                Text(verbatim: title).font(.system(size: 12, weight: selected ? .semibold : .regular)).foregroundStyle(ORIX.Palette.textPrimary).lineLimit(1)109                Spacer(minLength: 4)110                if let count { Text(verbatim: "\(count)").font(ORIX.Typeface.caption).foregroundStyle(ORIX.Palette.textTertiary) }111            }112            .padding(.leading, indent ? 22 : 6).padding(.trailing, 6).frame(height: 24)113            .background(selected ? ORIX.Palette.surfaceRaised : .clear, in: RoundedRectangle(cornerRadius: ORIX.Radius.chip, style: .continuous))114            .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.chip, style: .continuous).strokeBorder(targeted ? ORIX.Palette.accent : .clear, lineWidth: 1.5))115            .overlay(alignment: .leading) { if selected { Capsule().fill(ORIX.Palette.accent).frame(width: 2.5, height: 14) } }116            .contentShape(Rectangle())117        }118        .buttonStyle(.plain)119        .accessibilityLabel(Text(verbatim: title))120        .accessibilityValue(count.map { Text("\($0) tracks") } ?? Text(verbatim: ""))121        .accessibilityAddTraits(selected ? [.isSelected] : [])122        .accessibilityIdentifier("library-source-\(identifier)")123        .dropDestination(for: String.self) { items, _ in124            let ids = LibraryDrag.trackIDs(from: items)125            guard acceptsTracks, !ids.isEmpty else { return false }126            switch source {127            case let .crate(id): controller.addToCrate(ids, crate: id, model: model)128            case let .playlist(id): controller.addToPlaylist(ids, playlist: id, model: model)129            case .favorites: controller.setFavorite(ids, true, model: model)130            default: return false131            }132            return true133        } isTargeted: { targeted = $0 && acceptsTracks }134        .contextMenu {135            if editable {136                switch source {137                case let .crate(id): Button("Rename…") { controller.nameRequest = LibraryNameRequest(purpose: .renameCrate(id), text: title) }138                case let .playlist(id): Button("Rename…") { controller.nameRequest = LibraryNameRequest(purpose: .renamePlaylist(id), text: title) }139                case let .smartCrate(id):140                    Button("Edit Rules…") { if let c = controller.smartCrates.first(where: { $0.id == id }) { controller.editSmartCrate(c) } }141                    Button("Rename…") { controller.nameRequest = LibraryNameRequest(purpose: .renameSmartCrate(id), text: title) }142                default: EmptyView()143                }144                Divider()145                Button("Delete…", role: .destructive, action: onDelete)146            }147        }148    }149150    private var identifier: String {151        switch source {152        case .all: "all"153        case .favorites: "favourites"154        case .unanalysed: "not-analysed"155        case .missing: "missing"156        case .history(nil): "history"157        case let .history(id?): "session-\(id)"158        case let .crate(id): "crate-\(id)"159        case let .playlist(id): "playlist-\(id)"160        case let .smartCrate(id): "smart-\(id)"161        }162    }163}164
Files main
Menu

Theme