@@ -22,7 +22,7 @@ import androidx.compose.foundation.shape.CircleShape
2222import androidx.compose.material3.MaterialTheme
2323import androidx.compose.runtime.Composable
2424import androidx.compose.runtime.LaunchedEffect
25- import androidx.compose.runtime.mutableStateOf
25+ import androidx.compose.runtime.mutableLongStateOf
2626import androidx.compose.runtime.rememberCoroutineScope
2727import androidx.compose.ui.Alignment
2828import androidx.compose.ui.Modifier
@@ -70,7 +70,7 @@ class PlaylistQueueSheet(
7070 private val touchHelper: ItemTouchHelper
7171 private val queueHead: ComposeView
7272
73- private val durationState = mutableStateOf( false )
73+ private val durationState = mutableLongStateOf( - 1 )
7474 private val mqEnabled: Boolean
7575 private var detachedHead: Boolean = false
7676
@@ -143,7 +143,7 @@ class PlaylistQueueSheet(
143143 factory = { context ->
144144 val layout = LayoutInflater .from(context)
145145 .inflate(R .layout.playlist_bottom_sheet_actions, null )
146- val durationView: Chronometer = findViewById(R .id.duration)!!
146+ val durationView: Chronometer = layout. findViewById(R .id.duration)!!
147147 durationView.isCountDown = true
148148
149149 layout.findViewById<Button >(R .id.clearQueue)!!
@@ -168,13 +168,12 @@ class PlaylistQueueSheet(
168168 layout
169169 },
170170 update = { view ->
171- val trigger = durationState.value
171+ val durationBase = durationState.longValue
172172 if (detachedHead) return @AndroidView // chromometer is never shown for inactive queues
173173 val durationView: Chronometer = view.findViewById(R .id.duration)
174174 val pl = playlistAdapter.playlist
175175
176- val current = (instance?.currentMediaItemIndex ? : 0 )
177- val elapsedCurrentMs = (instance?.currentPosition ? : 0 )
176+
178177 durationView.format = context.getString(
179178 R .string.duration_queue,
180179 " %s" ,
@@ -185,10 +184,7 @@ class PlaylistQueueSheet(
185184 } else {
186185 durationView.stop()
187186 }
188- durationView.base = SystemClock .elapsedRealtime() +
189- pl.second.subList(current, pl.second.size)
190- .sumOf { it.mediaMetadata.durationMs ? : 0L
191- } - elapsedCurrentMs + 1000
187+ durationView.base = durationBase
192188 }
193189 )
194190 }
@@ -319,8 +315,13 @@ class PlaylistQueueSheet(
319315 playlistAdapter.updateList()
320316 }
321317
322- fun forceUpdate (mq : Int ) {
323- playlistAdapter.updateList(true , mq)
318+ /* *
319+ * Force a full update of playlist and timer
320+ *
321+ * @param mq Inactive queue index. Set to -1 to load the active queue
322+ */
323+ fun forceUpdate (mq : Int = -1) {
324+ playlistAdapter.updateList(mq)
324325 }
325326
326327 private inner class PlaylistCardAdapter : EditSongAdapter (activity) {
@@ -437,16 +438,25 @@ class PlaylistQueueSheet(
437438 return Pair (indexes, items)
438439 }
439440
440- fun updateList (nuke : Boolean = false, index : Int = -1) {
441- updateTimer()
442- if (nuke) {
443- playlist = instance?.getQueueForUi(index) ? : dumpPlaylist()
444- notifyDataSetChanged()
445- }
441+ /* *
442+ * Update playlist and timer
443+ */
444+ fun updateList (mqIndex : Int? = null) {
445+ val mq = mqIndex?.let { instance?.getQueueForUi(mqIndex) }
446+ val pl = if (mq != null ) Pair (mq.first, mq.second.queue) else dumpPlaylist()
447+ playlist = pl
448+ notifyDataSetChanged()
449+ updateTimer(mq?.second?.startIndex, mq?.second?.startPositionMs)
446450 }
447451
448- fun updateTimer () {
449- durationState.value = ! durationState.value
452+ fun updateTimer (currentMediaItemIndex : Int? = null, currentPosition : Long? = null) {
453+ val current = (currentMediaItemIndex ? : instance?.currentMediaItemIndex ? : 0 )
454+ val elapsedCurrentMs = (currentPosition ? : instance?.currentPosition ? : 0 )
455+ durationState.longValue = SystemClock .elapsedRealtime() +
456+ playlist.second.subList(current, playlist.second.size)
457+ .sumOf {
458+ it.mediaMetadata.durationMs ? : 0L
459+ } - elapsedCurrentMs + 1000
450460 }
451461 }
452462}
0 commit comments