Skip to content

Commit c6ae8cb

Browse files
committed
mq: Better handling for timer
1 parent d917eee commit c6ae8cb

2 files changed

Lines changed: 32 additions & 23 deletions

File tree

app/src/main/java/org/akanework/gramophone/logic/GramophoneExtensions.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ fun shuffledIndices(order: ShuffleOrder): MutableList<Int> {
400400
return result
401401
}
402402

403-
fun MediaController.getQueueForUi(index: Int = C.INDEX_UNSET): Pair<MutableList<Int>, MutableList<MediaItem>>? {
403+
fun MediaController.getQueueForUi(index: Int = -1): Pair<MutableList<Int>, MultiQueueObject>? {
404404
if (index == -1) {
405405
return null
406406
}
@@ -412,14 +412,13 @@ fun MediaController.getQueueForUi(index: Int = C.INDEX_UNSET): Pair<MutableList<
412412
val binder = getBinder("allQueues")!!
413413
BundleListRetriever.getList(binder).map {
414414
val mq = MultiQueueObject.fromBundle(it)
415-
val items = mq.queue
416415
val indexes: MutableList<Int> = if (mq.shuffleOrder == null) {
417416
(0 until mq.getSize()).toMutableList()
418417
} else {
419418
getIntArray("shuffleIndexes")!!.toMutableList()
420419
}
421420

422-
Pair(indexes, items)
421+
Pair(indexes, mq)
423422
}.firstOrNull()
424423
}
425424
}

app/src/main/java/org/akanework/gramophone/ui/components/PlaylistQueueSheet.kt

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import androidx.compose.foundation.shape.CircleShape
2222
import androidx.compose.material3.MaterialTheme
2323
import androidx.compose.runtime.Composable
2424
import androidx.compose.runtime.LaunchedEffect
25-
import androidx.compose.runtime.mutableStateOf
25+
import androidx.compose.runtime.mutableLongStateOf
2626
import androidx.compose.runtime.rememberCoroutineScope
2727
import androidx.compose.ui.Alignment
2828
import 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

Comments
 (0)