Show all search result types
This commit is contained in:
1 parent
74872549b0
commit
31783b1bb1
2 files changed
+33
-60
No files matched your search
@@ -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,58 +122,38 @@ 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
|
||||
ForEach(0..<rowCount, id: \.self) { index in
|
||||
if model.results.tracks.indices.contains(index) {
|
||||
let track = model.results.tracks[index]
|
||||
TrackRowView(
|
||||
track: track,
|
||||
index: index,
|
||||
showsArtwork: true,
|
||||
subtitlePrefix: "Song -",
|
||||
onPlayOnlyThis: { playFromSearch(track) },
|
||||
onPlay: { playFromSearch(track) }
|
||||
)
|
||||
rowDivider
|
||||
}
|
||||
ForEach(model.results.albums) { album in
|
||||
|
||||
if model.results.albums.indices.contains(index) {
|
||||
let album = model.results.albums[index]
|
||||
NavigationLink {
|
||||
AlbumDetailView(albumID: album.id)
|
||||
} label: {
|
||||
SearchCatalogRow(
|
||||
title: album.name,
|
||||
subtitle: "Album · \(artistNames(album.artists))",
|
||||
subtitle: "Album - \(artistNames(album.artists))",
|
||||
artworkURL: artworkURL(album.file),
|
||||
symbol: "opticaldisc"
|
||||
)
|
||||
@@ -203,7 +161,9 @@ struct SearchView: View {
|
||||
.buttonStyle(.plain)
|
||||
rowDivider
|
||||
}
|
||||
ForEach(model.results.artists) { artist in
|
||||
|
||||
if model.results.artists.indices.contains(index) {
|
||||
let artist = model.results.artists[index]
|
||||
NavigationLink {
|
||||
ArtistDetailView(artistID: artist.id)
|
||||
} label: {
|
||||
@@ -218,6 +178,7 @@ struct SearchView: View {
|
||||
rowDivider
|
||||
}
|
||||
}
|
||||
}
|
||||
.padding(.top, 8)
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ struct TrackRowView: View {
|
||||
let index: Int
|
||||
var hidesAlbumAction = false
|
||||
var showsArtwork = false
|
||||
var subtitlePrefix: String? = nil
|
||||
var onPlayOnlyThis: (() -> Void)? = nil
|
||||
var onPlay: (() -> Void)? = nil
|
||||
|
||||
@@ -46,8 +47,18 @@ struct TrackRowView: View {
|
||||
.font(.body)
|
||||
.foregroundStyle(.primary)
|
||||
.lineLimit(1)
|
||||
if !artists.isEmpty {
|
||||
if !artists.isEmpty || subtitlePrefix != nil {
|
||||
HStack(spacing: 4) {
|
||||
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)
|
||||
@@ -59,6 +70,7 @@ struct TrackRowView: View {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.lineLimit(1)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user