From aa58cf3aff57ee48588d8921d998cf5c793a9dec Mon Sep 17 00:00:00 2001 From: Barnabas Balogh Date: Sat, 25 Apr 2026 22:35:58 +0200 Subject: [PATCH] refactor: optimize playlist queue updates by combining flows for current playable media --- .../purefin/player/viewmodel/PlayerViewModel.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/hu/bbara/purefin/player/viewmodel/PlayerViewModel.kt b/core/src/main/java/hu/bbara/purefin/player/viewmodel/PlayerViewModel.kt index e24045a3..4807369c 100644 --- a/core/src/main/java/hu/bbara/purefin/player/viewmodel/PlayerViewModel.kt +++ b/core/src/main/java/hu/bbara/purefin/player/viewmodel/PlayerViewModel.kt @@ -19,6 +19,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.flow.asStateFlow +import kotlinx.coroutines.flow.combine import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch @@ -121,13 +122,14 @@ class PlayerViewModel @Inject constructor( } viewModelScope.launch { - val currentPlayableMedia = playerManager.currentPlayableMedia - playerManager.playlist.collect { playlist -> + combine(playerManager.playlist, playerManager.currentPlayableMedia) { playlist, currentPlayableMedia -> + playlist.mapNotNull { playableMedia -> + playableMedia.toPlaylistElementUiModel(currentPlayableMedia?.id) + } + }.collect { queue -> _uiState.update { state -> state.copy( - queue = playlist.mapNotNull { playableMedia -> - playableMedia.toPlaylistElementUiModel(currentPlayableMedia.first()?.id) - } + queue = queue ) } }