fix: refresh a mood after adding a song that is still downloading
CI / build (push) Successful in 4m1s
CI / play (push) Skipped

This commit is contained in:
TapTap committed 2026-11-15 21:45:12 +00:00
1 parent dc270f863c
commit 375bcc9c37
1 file changed
+26
@@ -396,6 +396,9 @@ class AppStore(
} }
applyMoodDetail(response.detail) applyMoodDetail(response.detail)
val title = response.detail.mood.name val title = response.detail.mood.name
if (response.downloading) {
pollUntilSongCount(response.detail.mood.id, response.detail.mood.songCount + 1)
}
_state.update { _state.update {
it.copy( it.copy(
message = when { message = when {
@@ -420,6 +423,9 @@ class AppStore(
try { try {
val response = api.addSong(target, url = url.trim()) val response = api.addSong(target, url = url.trim())
applyMoodDetail(response.detail) applyMoodDetail(response.detail)
if (response.downloading) {
pollUntilSongCount(response.detail.mood.id, response.detail.mood.songCount + 1)
}
_state.update { _state.update {
it.copy( it.copy(
searchQuery = "", searchQuery = "",
@@ -613,6 +619,9 @@ class AppStore(
try { try {
val decision = api.addRecommendation(moodId, videoId) val decision = api.addRecommendation(moodId, videoId)
applyMoodDetail(decision.detail) applyMoodDetail(decision.detail)
if (decision.recommendation.song == null) {
pollUntilSongCount(decision.detail.mood.id, decision.detail.mood.songCount + 1)
}
_state.update { _state.update {
it.copy( it.copy(
discovering = false, discovering = false,
@@ -1266,6 +1275,21 @@ class AppStore(
_state.update { it.copy(knownSongs = it.knownSongs + songs.associateBy { song -> song.id }) } _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() { private fun persistPlayback() {
val state = _state.value val state = _state.value
runCatching { runCatching {
@@ -1332,3 +1356,5 @@ private const val DOWNLOAD_ATTEMPTS = 3
private const val PROGRESS_POLL_TICKS = 12 private const val PROGRESS_POLL_TICKS = 12
private const val MAX_HISTORY = 50 private const val MAX_HISTORY = 50
private const val SEARCH_DEBOUNCE_MS = 300L private const val SEARCH_DEBOUNCE_MS = 300L
private const val DOWNLOAD_POLL_MS = 4_000L
private const val DOWNLOAD_POLLS = 60