From c4bc708bbac5666504b4c7691f2cd4fde4f35ddf Mon Sep 17 00:00:00 2001 From: Tim Date: Mon, 16 Nov 2026 10:32:07 -0500 Subject: [PATCH] feat(ios): artist photos on the Artists tab Each row of the Artists list now leads with the artist's photo in a 40pt circle, as on Home's crate. The list needs every artist's photo, so the v1 /artists list is walked a page at a time (100 is its cap; 718 artists = 8 requests) once per session, after the names are on screen, and cached on AppModel beside the artist list (cleared on logout; an empty walk isn't cached, so it retries). Home's crate reads from that cache once it exists. The paging stop rule is pure and tested (short page, total reached, a 20-page cap against a bad total). ArtistPortrait moves out of HomeView into Theme/Components, shared. iOS only; the Mac sidebar list is unchanged. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01VriHZgxEZZHunPFyTvTmjt --- AppleNugs/App/AppModel.swift | 20 +++++++++ AppleNugs/Core/Catalog.swift | 16 +++++++ AppleNugs/Core/NugsClient.swift | 29 ++++++++++++ .../Theme/Components/ArtistPortrait.swift | 44 ++++++++++++++++++ AppleNugs/Views/ArtistListView.swift | 30 ++++++++++--- AppleNugs/Views/HomeView.swift | 44 ------------------ AppleNugsTests/CatalogArtistPagingTests.swift | 45 +++++++++++++++++++ 7 files changed, 179 insertions(+), 49 deletions(-) create mode 100644 AppleNugs/Theme/Components/ArtistPortrait.swift create mode 100644 AppleNugsTests/CatalogArtistPagingTests.swift diff --git a/AppleNugs/App/AppModel.swift b/AppleNugs/App/AppModel.swift index 6484a94..cd985e8 100644 --- a/AppleNugs/App/AppModel.swift +++ b/AppleNugs/App/AppModel.swift @@ -39,6 +39,9 @@ final class AppModel { private var deepLinkTask: Task? private var cachedArtists: [ArtistEntry]? + /// Every artist's photo, by id — the Artists list's, fetched once per + /// session (8 pages). Cleared with `cachedArtists`. + private var cachedArtistAvatars: [String: URL]? /// Fully loaded artist catalogs (shows/releases, and videos), reused for /// `ArtistCatalogPaging.cacheTTL` so revisiting an artist page makes no @@ -165,6 +168,7 @@ final class AppModel { player.clear() discardOfflineLibrary() cachedArtists = nil + cachedArtistAvatars = nil artistShowsCache.removeAll() artistVideosCache.removeAll() sessionState = .loggedOut @@ -212,9 +216,25 @@ final class AppModel { #if DEBUG if AppModel.isUITestRun { return [:] } #endif + if let cachedArtistAvatars { + return cachedArtistAvatars.filter { ids.contains($0.key) } + } return try await client.artistAvatars(ids: ids) } + /// Every artist's photo, by id, cached for the session. Empty on a + /// `-UITEST` run (fake ids), and not cached when the walk came back empty, + /// so a failed first try is retried next time the list opens. + func allArtistAvatars() async throws -> [String: URL] { + #if DEBUG + if AppModel.isUITestRun { return [:] } + #endif + if let cachedArtistAvatars { return cachedArtistAvatars } + let avatars = try await client.allArtistAvatars() + if !avatars.isEmpty { cachedArtistAvatars = avatars } + return avatars + } + /// Resolve a display name to the catalog's own entry. /// /// The player only ever knows an artist's NAME — `QueueTrack` carries no diff --git a/AppleNugs/Core/Catalog.swift b/AppleNugs/Core/Catalog.swift index 991cb80..c3db3b5 100644 --- a/AppleNugs/Core/Catalog.swift +++ b/AppleNugs/Core/Catalog.swift @@ -219,6 +219,22 @@ enum Catalog { return avatars } + /// The v1 `/artists` list's largest page (`limit` above 100 → HTTP 400). + static let artistPageSize = 100 + /// A ceiling on pages walked, so a misreported `total` can't loop us: + /// the catalog was 718 artists (8 pages) on 2026-11-16. + static let maxArtistPages = 20 + + /// The offset of the next v1 `/artists` page, or nil when the walk is done: + /// a short or empty page, the reported `total` reached, or the page cap. + static func nextArtistPageOffset(after offset: Int, received: Int, + total: Int?, pageIndex: Int) -> Int? { + guard received >= artistPageSize, pageIndex + 1 < maxArtistPages else { return nil } + let next = offset + received + if let total, next >= total { return nil } + return next + } + /// catalog.containersAll → containers (newest-first per the API). static func containers(from json: JSON) -> [ContainerSummary] { json.unwrapped.arr("containers", "Containers").compactMap { c in diff --git a/AppleNugs/Core/NugsClient.swift b/AppleNugs/Core/NugsClient.swift index ac7809e..e65a2c0 100644 --- a/AppleNugs/Core/NugsClient.swift +++ b/AppleNugs/Core/NugsClient.swift @@ -267,6 +267,35 @@ actor NugsClient { return Catalog.artistAvatars(from: json) } + /// Every artist's photo, for the Artists list: the v1 `/artists` list + /// walked a page (100) at a time — 8 requests for the 718-artist catalog, + /// through the same token bucket as every catalog call. Keyed by + /// `ArtistEntry.id`. Pages that arrive before a failure are kept. + func allArtistAvatars() async throws -> [String: URL] { + var avatars: [String: URL] = [:] + var offset: Int? = 0 + var page = 0 + while let current = offset { + let json: JSON + do { + json = try await catalogV1Get("/artists", query: [ + "offset": String(current), + "limit": String(Catalog.artistPageSize), + ]) + } catch { + if avatars.isEmpty { throw error } + break + } + let received = json.unwrapped.arr("items", "Items").count + avatars.merge(Catalog.artistAvatars(from: json)) { _, new in new } + offset = Catalog.nextArtistPageOffset(after: current, received: received, + total: json.unwrapped.int("total"), + pageIndex: page) + page += 1 + } + return avatars + } + // --- video catalog ------------------------------------------------------ /// Recently-added VOD (the only artist-independent global video listing). diff --git a/AppleNugs/Theme/Components/ArtistPortrait.swift b/AppleNugs/Theme/Components/ArtistPortrait.swift new file mode 100644 index 0000000..7a934b0 --- /dev/null +++ b/AppleNugs/Theme/Components/ArtistPortrait.swift @@ -0,0 +1,44 @@ +import SwiftUI + +/// An artist's photo clipped to a circle — Apple Music's shape for an artist, +/// apart from the square show covers — filling the width it's given. The +/// monogram stands in while the photo loads, when there is none, and when it +/// 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. +struct ArtistPortrait: View { + @Environment(\.theme) private var theme + + let url: URL? + let name: String + + var body: some View { + GeometryReader { geo in + let side = min(geo.size.width, geo.size.height) + Group { + if let url { + AsyncImage(url: url) { phase in + if case .success(let image) = phase { + image.resizable().aspectRatio(contentMode: .fill) + } else { + monogram(side) + } + } + } else { + monogram(side) + } + } + .frame(width: side, height: side) + .clipShape(Circle()) + .overlay { Circle().strokeBorder(theme.palette.hairline, lineWidth: 1) } + .frame(maxWidth: .infinity, maxHeight: .infinity) + } + .aspectRatio(1, contentMode: .fit) + .accessibilityHidden(true) + } + + private func monogram(_ side: CGFloat) -> some View { + MonogramTile(text: name, size: side) + } +} diff --git a/AppleNugs/Views/ArtistListView.swift b/AppleNugs/Views/ArtistListView.swift index 6a079ff..a0cdbfb 100644 --- a/AppleNugs/Views/ArtistListView.swift +++ b/AppleNugs/Views/ArtistListView.swift @@ -10,6 +10,9 @@ struct ArtistListView: View { @State private var filter = "" @State private var loading = false @State private var error: String? + /// Every artist's photo, by id (iOS). Empty until the walk lands, and for + /// good if it fails — rows show the monogram meanwhile. + @State private var avatars: [String: URL] = [:] @FocusState private var filterFocused: Bool /// A–Z sections, derived state: filtering + grouping + locale-aware @@ -63,11 +66,18 @@ struct ArtistListView: View { // over an artist list": names only (Tim's // ruling 2: no show counts), heavy 17 on frost // rules, under heavy slush letter headers. - Text(artist.name) - .font(theme.type.title(17)) - .foregroundStyle(theme.palette.textPrimary) - .lineLimit(1) - .padding(.vertical, 4) + HStack(spacing: 12) { + #if os(iOS) + // The artist's photo, as on Home's crate. + ArtistPortrait(url: avatars[artist.id], name: artist.name) + .frame(width: Self.portraitSide, height: Self.portraitSide) + #endif + Text(artist.name) + .font(theme.type.title(17)) + .foregroundStyle(theme.palette.textPrimary) + .lineLimit(1) + } + .padding(.vertical, 4) } // Each row is ruled in frost (`.alist li`). .listRowSeparator(.visible) @@ -177,5 +187,15 @@ struct ArtistListView: View { } catch { self.error = error.localizedDescription } + #if os(iOS) + // After the names are on screen: photos are a nicety, never a reason + // to hold the list back, and a failure reports nothing. + if !artists.isEmpty, avatars.isEmpty { + avatars = (try? await app.allArtistAvatars()) ?? [:] + } + #endif } + + /// The row's photo: 40pt, a size that reads as a face in a dense list. + private static let portraitSide: CGFloat = 40 } diff --git a/AppleNugs/Views/HomeView.swift b/AppleNugs/Views/HomeView.swift index cf9ca90..6097f9e 100644 --- a/AppleNugs/Views/HomeView.swift +++ b/AppleNugs/Views/HomeView.swift @@ -466,47 +466,3 @@ private extension View { modifier(Reveal(appeared: appeared, index: index)) } } - -#if os(iOS) -// MARK: - Artist portrait - -/// An artist's photo clipped to a circle, filling the width it's given. The -/// monogram stands in while the photo loads, when there is none, and when it -/// fails — so a crate with no network still reads as artists, not holes. -private struct ArtistPortrait: View { - @Environment(\.theme) private var theme - - let url: URL? - let name: String - - var body: some View { - GeometryReader { geo in - let side = min(geo.size.width, geo.size.height) - Group { - if let url { - AsyncImage(url: url) { phase in - if case .success(let image) = phase { - image.resizable().aspectRatio(contentMode: .fill) - } else { - monogram(side) - } - } - } else { - monogram(side) - } - } - .frame(width: side, height: side) - .clipShape(Circle()) - .overlay { Circle().strokeBorder(theme.palette.hairline, lineWidth: 1) } - .frame(maxWidth: .infinity, maxHeight: .infinity) - } - .aspectRatio(1, contentMode: .fit) - .accessibilityHidden(true) - } - - private func monogram(_ side: CGFloat) -> some View { - MonogramTile(text: name, size: side) - } -} -#endif - diff --git a/AppleNugsTests/CatalogArtistPagingTests.swift b/AppleNugsTests/CatalogArtistPagingTests.swift new file mode 100644 index 0000000..4eb75aa --- /dev/null +++ b/AppleNugsTests/CatalogArtistPagingTests.swift @@ -0,0 +1,45 @@ +import XCTest + +final class CatalogArtistPagingTests: XCTestCase { + + private func next(_ offset: Int, _ received: Int, total: Int?, page: Int) -> Int? { + Catalog.nextArtistPageOffset(after: offset, received: received, total: total, pageIndex: page) + } + + /// The real catalog: 718 artists → offsets 0, 100, …, 700, then stop. + func testWalksTheRealCatalogInEightPages() { + var offsets: [Int] = [] + var offset: Int? = 0 + var page = 0 + while let current = offset { + offsets.append(current) + let received = min(Catalog.artistPageSize, 718 - current) + offset = next(current, received, total: 718, page: page) + page += 1 + } + XCTAssertEqual(offsets, [0, 100, 200, 300, 400, 500, 600, 700]) + } + + /// A short page is the last one, whatever `total` says. + func testShortPageStops() { + XCTAssertNil(next(700, 18, total: 9999, page: 7)) + XCTAssertNil(next(0, 0, total: nil, page: 0)) + } + + /// Reaching `total` stops even on a full page. + func testTotalReachedStops() { + XCTAssertNil(next(100, 100, total: 200, page: 1)) + XCTAssertEqual(next(100, 100, total: 201, page: 1), 200) + } + + /// No `total` in the response: keep going while pages are full. + func testMissingTotalFollowsFullPages() { + XCTAssertEqual(next(0, 100, total: nil, page: 0), 100) + } + + /// A misreported total can't loop forever: the page cap ends the walk. + func testPageCapEndsTheWalk() { + XCTAssertEqual(next(1800, 100, total: 100_000, page: Catalog.maxArtistPages - 2), 1900) + XCTAssertNil(next(1900, 100, total: 100_000, page: Catalog.maxArtistPages - 1)) + } +}