SPB Git

Repository Public

SearchView.swift 2.9 KB 61 lines swift

1// SP Music iOS2// File: Features/Search/SearchView.swift3// Description: Search over the device's copy of the library (works offline).4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai78import SPMusicCore9import SwiftUI1011/// Search runs on the device's copy of the library: instant, and the same in aeroplane mode (over downloaded music).12struct SearchView: View {13    @Environment(AppEnvironment.self) private var env14    @State private var query = ""15    @State private var results = SearchResults()16    @State private var playlistFor: Track?1718    var body: some View {19        List {20            if !results.artists.isEmpty {21                Section("Artistes") { ForEach(results.artists) { a in NavigationLink(value: Route.artist(a.id)) { ArtistRow(artist: a) }.listRowBackground(Color.spBackground) } }22            }23            if !results.albums.isEmpty {24                Section("Albums") {25                    ForEach(results.albums) { a in26                        NavigationLink(value: Route.album(a.id)) {27                            HStack(spacing: 12) {28                                ArtworkView(id: a.artworkId, side: 48, corner: 6)29                                VStack(alignment: .leading, spacing: 2) { Text(a.title).lineLimit(1); Text(a.artistName).font(.footnote).foregroundStyle(Color.spMuted).lineLimit(1) }30                            }31                        }32                        .listRowBackground(Color.spBackground)33                    }34                }35            }36            if !results.tracks.isEmpty {37                Section("Titres") {38                    ForEach(Array(results.tracks.enumerated()), id: \.element.id) { i, t in39                        TrackRow(track: t, onAddToPlaylist: { playlistFor = $0 }) { env.play(results.tracks, startAt: i) }.listRowBackground(Color.spBackground)40                    }41                }42            }43        }44        .listStyle(.plain).scrollContentBackground(.hidden).background(Color.spBackground)45        .overlay {46            if query.trimmingCharacters(in: .whitespaces).isEmpty {47                ContentUnavailableView("Rechercher", systemImage: "magnifyingglass", description: Text(env.downloadedOnly ? "Dans la musique téléchargée sur cet appareil." : "Artistes, albums, titres ou genres de votre bibliothèque."))48            } else if results.isEmpty { ContentUnavailableView.search(text: query) }49        }50        .navigationTitle("Recherche")51        .searchable(text: $query, prompt: "Artistes, albums, titres")52        .autocorrectionDisabled()53        .sheet(item: $playlistFor) { AddToPlaylistSheet(tracks: [$0]) }54        .task(id: env.reloadKey(query)) {55            try? await Task.sleep(for: .milliseconds(120))          // typing: wait for a short pause56            guard !Task.isCancelled else { return }57            results = await env.db.search(query, downloadedOnly: env.downloadedOnly)58        }59    }60}61
Files main
Menu

Theme