From 29cc9a03d4f943cac1eb8d982edb396b7a815569 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 23 Nov 2026 10:35:06 -0800 Subject: [PATCH] Random Album Radio: every new song adds another album, continuous queue (build 61) Co-Authored-By: Claude Opus 5.5 --- ios/Stemify/Playback/PlaybackManager.swift | 30 +++++++++++++++++----- ios/project.yml | 2 +- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/ios/Stemify/Playback/PlaybackManager.swift b/ios/Stemify/Playback/PlaybackManager.swift index a9265d0..cc21f8a 100644 --- a/ios/Stemify/Playback/PlaybackManager.swift +++ b/ios/Stemify/Playback/PlaybackManager.swift @@ -219,11 +219,19 @@ final class PlaybackManager: StemEngineListener, @unchecked Sendable { @ObservationIgnored private var stationGen = 0 @ObservationIgnored private var stationBusy = false private static let radioAlbumsAhead = 8 - private static let radioAlbumsLow = 3 + /// Each new song adds an album, up to this many ahead (keeps the queue from growing without end). + private static let radioAlbumsMax = 40 + /// Album radio: the song the last top-up saw, and songs started since that haven't added their album yet. + @ObservationIgnored private var radioSeenUid: Int64? + @ObservationIgnored private var radioOwed = 0 private static let radioSongsBatch = 100 private static let radioSongsLow = 25 - private func setStation(_ s: Station?) { station = s; stationGen += 1; stationBusy = false } + private func setStation(_ s: Station?) { + station = s; stationGen += 1; stationBusy = false + radioSeenUid = queue.indices.contains(index) ? queue[index].uid : nil + radioOwed = 0 + } private func albumDeck() async throws -> [String] { var deck = (try? await plex.albums().map(\.ratingKey)) ?? [] @@ -268,18 +276,28 @@ final class PlaybackManager: StemEngineListener, @unchecked Sendable { let gen = stationGen switch st { case .albums(let saved): + // Every song that starts (the next track, a skip, a jump to another album) owes one more album, + // so the queue always runs on past what's playing. + let uid = queue[index].uid + if uid != radioSeenUid { radioSeenUid = uid; radioOwed += 1 } let ahead = Set(queue[index...].map { $0.track.albumKey ?? $0.track.album }).count - guard ahead <= Self.radioAlbumsLow else { return } + let want = min(max(Self.radioAlbumsAhead + 1 - ahead, radioOwed), Self.radioAlbumsMax - ahead) + guard want > 0 else { radioOwed = 0; return } stationBusy = true + let owed = radioOwed Task { @MainActor in - defer { if gen == stationGen { stationBusy = false } } + var got = false + // Checks again once the albums are in, unless nothing came back (server down: don't spin). + defer { if gen == stationGen { stationBusy = false; if got { topUpStation() } } } var deck = saved - if deck.isEmpty { deck = (try? await albumDeck()) ?? [] } - let take = Array(deck.prefix(Self.radioAlbumsAhead + 1 - ahead)) + if deck.count < want { deck += (try? await albumDeck()) ?? [] } + let take = Array(deck.prefix(want)) deck.removeFirst(take.count) let tracks = await albumTracks(take) guard gen == stationGen else { return } station = .albums(deck) + radioOwed = max(radioOwed - owed, 0) + got = !tracks.isEmpty addToQueue(tracks) } case .songs: diff --git a/ios/project.yml b/ios/project.yml index 67dfee4..e0edb5b 100644 --- a/ios/project.yml +++ b/ios/project.yml @@ -9,7 +9,7 @@ settings: # iOS needs a plain number for the bundle version; the label is what the app and releases show. MARKETING_VERSION: "0.10.1" VERSION_LABEL: "0.10.1" - CURRENT_PROJECT_VERSION: "60" + CURRENT_PROJECT_VERSION: "61" # Dev builds override these (build-ipa.sh dev) so they install next to the release app. APP_ID: org.cdnutter.stemify APP_NAME: Stemify