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
20 changes: 20 additions & 0 deletions AppleNugs/App/AppModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ final class AppModel {
private var deepLinkTask: Task<Void, Never>?

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
Expand Down Expand Up @@ -165,6 +168,7 @@ final class AppModel {
player.clear()
discardOfflineLibrary()
cachedArtists = nil
cachedArtistAvatars = nil
artistShowsCache.removeAll()
artistVideosCache.removeAll()
sessionState = .loggedOut
Expand Down Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions AppleNugs/Core/Catalog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions AppleNugs/Core/NugsClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
44 changes: 44 additions & 0 deletions AppleNugs/Theme/Components/ArtistPortrait.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
30 changes: 25 additions & 5 deletions AppleNugs/Views/ArtistListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
44 changes: 0 additions & 44 deletions AppleNugs/Views/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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

45 changes: 45 additions & 0 deletions AppleNugsTests/CatalogArtistPagingTests.swift
Original file line number Diff line number Diff line change
@@ -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))
}
}
Loading