Fixes a crash during playback (#1514)

## Description
Fixes a crash that could occur during the beginning of playback

Also adds better handling for clicking next extremely fast

### Technical details

The playlist objects would be created on the IO thread and it had a
`mutableIntStateOf` to track the current index. This meant that there is
a race conditions for the IO thread to commit the snapshot for the main
thread to see them. If the main thread is faster, it will crash.

To fix that, this PR moves the mutable state in `Playlist` into the
`PlaybackState` flow in the view model. This enures both the background
threads and re-compositions have the right state.

### Related issues
Fixes #1509 

### Testing
Emulator

## Screenshots
N/A

## AI or LLM usage
None
This commit is contained in:
Damontecres 2026-06-03 12:41:13 -04:00 • committed by GitHub
parent 5ae452155e
commit 471dbd62db
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 99 additions and 89 deletions

View file

@ -1,8 +1,5 @@
package com.github.damontecres.wholphin.data.model
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.setValue
import org.jellyfin.sdk.model.api.MediaType
import java.util.UUID
@ -11,36 +8,9 @@ import java.util.UUID
*
* This is not the same thing as a Jellyfin server playlist
*/
class Playlist(
items: List<PlaylistItem>,
startIndex: Int = 0,
data class Playlist(
val items: List<PlaylistItem>,
) {
val items = items.subList(startIndex, items.size)
var index by mutableIntStateOf(0)
fun hasPrevious(): Boolean = index > 0
fun hasNext(): Boolean = (index + 1) < items.size
fun getPreviousAndReverse(): PlaylistItem = items[--index]
fun getAndAdvance(): PlaylistItem = items[++index]
fun peek(): PlaylistItem? = items.getOrNull(index + 1)
fun upcomingItems(): List<PlaylistItem> = items.subList(index + 1, items.size)
fun advanceTo(id: UUID): PlaylistItem? {
while (hasNext()) {
val potential = getAndAdvance()
if (potential.id == id) {
return potential
}
}
return null
}
companion object {
const val MAX_SIZE = 100
}