From 31783b1bb147b9f596f08ec0362409c4b59bc851 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20T=C3=A4kker?= Date: Thu, 8 Oct 2026 00:00:45 +0200 Subject: [PATCH] Show all search result types --- Music/View/Search/SearchView.swift | 139 ++++++++++------------------ Music/View/Track/TrackRowView.swift | 30 ++++-- 2 files changed, 71 insertions(+), 98 deletions(-) diff --git a/Music/View/Search/SearchView.swift b/Music/View/Search/SearchView.swift index 1771c5a..988ead5 100644 --- a/Music/View/Search/SearchView.swift +++ b/Music/View/Search/SearchView.swift @@ -1,27 +1,8 @@ import Combine import SwiftUI -private enum SearchCategory: String, CaseIterable, Identifiable { - case topResults = "Top Results" - case artists = "Artists" - case albums = "Albums" - case songs = "Songs" - - var id: String { rawValue } - - var apiType: SearchType { - switch self { - case .topResults: .default - case .artists: .artist - case .albums: .album - case .songs: .track - } - } -} - @MainActor private final class SearchViewModel: ObservableObject { - @Published var category: SearchCategory = .topResults @Published private(set) var results = SearchResults(tracks: [], albums: [], artists: []) @Published private(set) var recentTracks: [Track] = [] @Published private(set) var isLoading = false @@ -54,9 +35,9 @@ private final class SearchViewModel: ObservableObject { isLoading = true errorMessage = nil do { - // The API splits its limit between result kinds, so request 27 to get - // up to nine items for the selected kind. - results = try await api.search(trimmed, type: category.apiType, limit: 27) + // The API splits its limit between result kinds; 27 allows up to nine + // tracks, albums, and artists in the combined results. + results = try await api.search(trimmed, type: .default, limit: 27) } catch is CancellationError { return } catch { @@ -106,9 +87,6 @@ struct SearchView: View { .padding(.top, 4) .padding(.bottom, 16) - categoryPicker - Divider().overlay(.white.opacity(0.12)) - if model.isLoading && !model.hasResults { ProgressView("Searching…") .tint(.red) @@ -144,78 +122,61 @@ struct SearchView: View { .toolbar(.hidden, for: .navigationBar) .task { model.loadRecentTracks() } .onChange(of: query) { _, value in model.search(value) } - .onChange(of: model.category) { _, _ in model.search(query, immediately: true) } - } - - private var categoryPicker: some View { - ScrollView(.horizontal) { - HStack(spacing: 10) { - ForEach(SearchCategory.allCases) { category in - Button { - model.category = category - } label: { - Text(category.rawValue) - .font(.system(size: 17, weight: .semibold)) - .padding(.horizontal, 18) - .frame(height: 44) - .background { - Capsule().fill(model.category == category ? Color.red : Color.clear) - } - .overlay { - if model.category != category { - Capsule().stroke(.white.opacity(0.18), lineWidth: 1) - } - } - } - .buttonStyle(.plain) - .accessibilityAddTraits(model.category == category ? .isSelected : []) - } - } - .padding(.vertical, 14) - } - .scrollIndicators(.hidden) } @ViewBuilder private var resultList: some View { + let rowCount = max( + model.results.tracks.count, + max(model.results.albums.count, model.results.artists.count) + ) + LazyVStack(spacing: 0) { - ForEach(Array(model.results.tracks.enumerated()), id: \.element.id) { index, track in - TrackRowView( - track: track, - index: index, - showsArtwork: true, - onPlayOnlyThis: { playFromSearch(track) }, - onPlay: { playFromSearch(track) } - ) - rowDivider - } - ForEach(model.results.albums) { album in - NavigationLink { - AlbumDetailView(albumID: album.id) - } label: { - SearchCatalogRow( - title: album.name, - subtitle: "Album · \(artistNames(album.artists))", - artworkURL: artworkURL(album.file), - symbol: "opticaldisc" + ForEach(0.. Void)? = nil var onPlay: (() -> Void)? = nil @@ -46,16 +47,27 @@ struct TrackRowView: View { .font(.body) .foregroundStyle(.primary) .lineLimit(1) - if !artists.isEmpty { + if !artists.isEmpty || subtitlePrefix != nil { HStack(spacing: 4) { - ForEach(artists) { artist in - NavigationLink(artist.name) { - ArtistDetailView(artistID: artist.id) - } - .font(.caption) - .foregroundStyle(.secondary) - if artist.id != artists.last?.id { - Text(",").font(.caption).foregroundStyle(.secondary) + if let subtitlePrefix { + Text(subtitlePrefix) + .font(.caption) + .foregroundStyle(.secondary) + } + if artists.isEmpty { + Text("Unknown Artist") + .font(.caption) + .foregroundStyle(.secondary) + } else { + ForEach(artists) { artist in + NavigationLink(artist.name) { + ArtistDetailView(artistID: artist.id) + } + .font(.caption) + .foregroundStyle(.secondary) + if artist.id != artists.last?.id { + Text(",").font(.caption).foregroundStyle(.secondary) + } } } }