Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion AppleNugs/Theme/Components/ArtistPortrait.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import SwiftUI
/// fails, so a list with no network still reads as artists, not holes.
///
/// Photos come from the v1 catalog's `avatarImage` (`Catalog.artistAvatars`).
/// Used by Home's crate tiles and the Artists list.
/// Used by `ArtistTile` (Home, Favorites), the Artists list, the artist page
/// header and search results.
struct ArtistPortrait: View {
@Environment(\.theme) private var theme

Expand Down
24 changes: 21 additions & 3 deletions AppleNugs/Views/SearchView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ struct SearchView: View {
@State private var results: SearchModel?
@State private var searching = false
@State private var error: String?
/// The artist hits' photos, by id. Empty until the lookup lands (and for
/// good if it fails) — rows show the monogram meanwhile.
@State private var avatars: [String: URL] = [:]
@FocusState private var fieldFocused: Bool

var body: some View {
Expand All @@ -37,6 +40,13 @@ struct SearchView: View {
#endif
}
.onChange(of: ui.searchFocusTick) { fieldFocused = true }
// One `?ids=` lookup per result set (known photos are remembered on
// AppModel). Merged, so an earlier search's photos stay put.
.task(id: results?.artists.map(\.id) ?? []) {
guard let ids = results?.artists.map(\.id), !ids.isEmpty,
let found = try? await app.artistAvatars(ids: ids) else { return }
avatars.merge(found) { _, new in new }
}
.scrollDismissesKeyboard(.immediately)
.toolbar {
#if os(iOS)
Expand Down Expand Up @@ -90,7 +100,10 @@ struct SearchView: View {
ContentUnavailableView(theme.copy.searchNoResults(searchedQuery),
systemImage: "magnifyingglass")
} else {
resultsList(results)
// `avatars` is read here, in body, and handed down: read only
// inside the List's rows it isn't a dependency of this body,
// and photos that land late never reach the rows.
resultsList(results, avatars: avatars)
}
} else {
ContentUnavailableView(
Expand All @@ -100,13 +113,18 @@ struct SearchView: View {
}
}

private func resultsList(_ results: SearchModel) -> some View {
private func resultsList(_ results: SearchModel, avatars: [String: URL]) -> some View {
List {
if !results.artists.isEmpty {
Section {
ForEach(results.artists) { artist in
NavigationLink(value: Route.artist(artist)) {
Label(artist.name, systemImage: "music.mic")
HStack(spacing: 10) {
// The artist's photo, as on the Artists list.
ArtistPortrait(url: avatars[artist.id], name: artist.name)
.frame(width: 32, height: 32)
Text(artist.name).lineLimit(1)
}
}
.themedListRow()
.contextMenu {
Expand Down
Loading