fix: refresh a mood after adding a song that is still downloading
This commit is contained in:
1 parent
dc270f863c
commit
375bcc9c37
1 file changed
+26
@@ -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
|
||||
Reference in new issue
Block a user