From 375bcc9c37a68a3565c4ed739a3c8ab195283e84 Mon Sep 17 00:00:00 2001 From: TapTap Date: Sun, 15 Nov 2026 21:45:12 +0000 Subject: [PATCH] fix: refresh a mood after adding a song that is still downloading --- .../musicmanager/client/store/AppStore.kt | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt index 6d96f1a..bd92fc3 100644 --- a/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt +++ b/composeApp/src/commonMain/kotlin/win/taptap/musicmanager/client/store/AppStore.kt @@ -396,6 +396,9 @@ class AppStore( } applyMoodDetail(response.detail) val title = response.detail.mood.name + if (response.downloading) { + pollUntilSongCount(response.detail.mood.id, response.detail.mood.songCount + 1) + } _state.update { it.copy( message = when { @@ -420,6 +423,9 @@ class AppStore( try { val response = api.addSong(target, url = url.trim()) applyMoodDetail(response.detail) + if (response.downloading) { + pollUntilSongCount(response.detail.mood.id, response.detail.mood.songCount + 1) + } _state.update { it.copy( searchQuery = "", @@ -613,6 +619,9 @@ class AppStore( try { val decision = api.addRecommendation(moodId, videoId) applyMoodDetail(decision.detail) + if (decision.recommendation.song == null) { + pollUntilSongCount(decision.detail.mood.id, decision.detail.mood.songCount + 1) + } _state.update { it.copy( discovering = false, @@ -1266,6 +1275,21 @@ class AppStore( _state.update { it.copy(knownSongs = it.knownSongs + songs.associateBy { song -> song.id }) } } + /** + * A song added while its download is still running is not in the detail + * response yet. Poll briefly so the new row appears without a manual refresh. + */ + private fun pollUntilSongCount(moodId: String, expectedAtLeast: Int) { + scope.launch { + repeat(DOWNLOAD_POLLS) { + delay(DOWNLOAD_POLL_MS) + val detail = runCatching { api.mood(moodId) }.getOrNull() ?: return@launch + applyMoodDetail(detail) + if (detail.songs.size >= expectedAtLeast) return@launch + } + } + } + private fun persistPlayback() { val state = _state.value runCatching { @@ -1332,3 +1356,5 @@ private const val DOWNLOAD_ATTEMPTS = 3 private const val PROGRESS_POLL_TICKS = 12 private const val MAX_HISTORY = 50 private const val SEARCH_DEBOUNCE_MS = 300L +private const val DOWNLOAD_POLL_MS = 4_000L +private const val DOWNLOAD_POLLS = 60