refactor: optimize playlist queue updates by combining flows for current playable media

This commit is contained in:
2026-04-25 22:35:58 +02:00
parent 81a3504606
commit aa58cf3aff

View File

@@ -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
)
}
}