Repository Public
1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Features/Search/SearchView.swift3// Description: Search results from the local database (works offline).4import SwiftUI5import SPMusicCore67/// Search runs on the local database: it answers as you type and works exactly the same without a connection8/// (then limited to what is downloaded).9struct SearchView: View {10 @Environment(AppModel.self) private var model11 @State private var results = LibraryStore.SearchResults()12 @State private var searched = ""1314 var body: some View {15 Group {16 if results.isEmpty && searched == model.searchText {17 EmptyState(symbol: "magnifyingglass", title: "Aucun résultat pour « \(model.searchText) »",18 message: model.downloadedOnly ? "La recherche porte sur la musique téléchargée sur ce Mac." : "Vérifiez l’orthographe, ou essayez un autre mot.")19 } else {20 ScrollView {21 VStack(alignment: .leading, spacing: 24) {22 if !results.artists.isEmpty {23 VStack(alignment: .leading, spacing: 8) {24 SectionHeader(title: "Artistes")25 ScrollView(.horizontal, showsIndicators: false) {26 HStack(spacing: 18) {27 ForEach(results.artists) { a in28 VStack(spacing: 6) {29 ArtworkView(artworkID: a.artworkID, size: 96, cornerRadius: 48, symbol: "music.mic")30 Text(a.name).font(.callout).lineLimit(1)31 }32 .frame(width: 110).contentShape(Rectangle()).onTapGesture { model.show(.artist(a.id)) }33 }34 }35 }36 }37 }38 if !results.albums.isEmpty { VStack(alignment: .leading, spacing: 8) { SectionHeader(title: "Albums"); AlbumShelf(albums: results.albums) } }39 if !results.tracks.isEmpty {40 VStack(alignment: .leading, spacing: 4) {41 SectionHeader(title: "Titres")42 ForEach(Array(results.tracks.enumerated()), id: \.element.id) { i, t in TrackRow(track: t) { model.play(results.tracks, at: i) } }43 }44 }45 }46 .padding(24).frame(maxWidth: .infinity, alignment: .leading)47 }48 }49 }50 .navigationTitle("Recherche")51 .task(id: "\(model.searchText)-\(model.libraryRevision)-\(model.downloadedOnly)") {52 let q = model.searchText53 try? await Task.sleep(nanoseconds: 90_000_000) // let the listener finish the word54 guard !Task.isCancelled, let s = model.store else { return }55 results = await s.search(q, downloadedOnly: model.downloadedOnly); searched = q56 }57 }58}59