Add version to state

This commit is contained in:
Damontecres 2026-03-03 22:39:15 -05:00
parent 1af906fe90
commit 84611c24ed
No known key found for this signature in database
2 changed files with 14 additions and 4 deletions

View file

@ -230,7 +230,10 @@ class MusicService
private suspend fun updateQueueSize() {
withContext(Dispatchers.Main) {
_state.update {
it.copy(queueSize = player.mediaItemCount)
it.copy(
queueVersion = it.queueVersion + 1,
queueSize = player.mediaItemCount,
)
}
}
start()
@ -278,6 +281,7 @@ class MusicService
@Stable
data class MusicServiceState(
val queueVersion: Long,
val queueSize: Int,
val currentIndex: Int,
val currentItemId: UUID?,
@ -286,7 +290,7 @@ data class MusicServiceState(
val loadingState: LoadingState = LoadingState.Pending,
) {
companion object {
val EMPTY = MusicServiceState(0, 0, null, false, null)
val EMPTY = MusicServiceState(0L, 0, 0, null, false, null)
}
}
@ -357,9 +361,10 @@ private class MusicPlayerListener(
@Composable
fun rememberQueue(
player: Player,
queueVersion: Long,
queueSize: Int,
): List<AudioItem> =
remember(queueSize) {
remember(queueVersion, queueSize) {
object : AbstractList<AudioItem>() {
override val size: Int
get() = player.mediaItemCount

View file

@ -66,7 +66,12 @@ fun NowPlayingPage(
val context = LocalContext.current
val state by viewModel.state.collectAsState()
val player = viewModel.player
val queue = rememberQueue(player, state.musicServiceState.queueSize)
val queue =
rememberQueue(
player,
state.musicServiceState.queueVersion,
state.musicServiceState.queueSize,
)
val current = queue.getOrNull(state.musicServiceState.currentIndex)
val controllerViewState = viewModel.controllerViewState