Skip to content

Commit

Permalink
multiqueue: Enqueue next now starts playback after player is dismissed
Browse files Browse the repository at this point in the history
*  Move init/deinit of queue to their own functions
  • Loading branch information
mikooomich committed Jan 12, 2025
1 parent 29b10d7 commit faf4dc3
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class QueueBoard(queues: MutableList<MultiQueueObject> = ArrayList()) {
private val masterQueues: MutableList<MultiQueueObject> = queues
private var masterIndex = masterQueues.size - 1 // current queue index
var detachedHead = false
var initialized = false


/**
Expand Down
78 changes: 45 additions & 33 deletions app/src/main/java/com/dd3boh/outertune/playback/MusicService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -387,27 +387,45 @@ class MusicService : MediaLibraryService(),
}
}

initQueue()
CoroutineScope(Dispatchers.Main).launch {
val queuePos = queueBoard.setCurrQueue(this@MusicService, false)
if (queuePos != null) {
player.seekTo(queuePos, dataStore.get(LastPosKey, C.TIME_UNSET))
dataStore.edit { settings ->
settings[LastPosKey] = C.TIME_UNSET
}
}
}
}

fun initQueue() {
if (dataStore.get(PersistentQueueKey, true)) {
queueBoard = QueueBoard(database.readQueue().toMutableList())
isShuffleEnabled.value = queueBoard.getCurrentQueue()?.shuffled ?: false
if (queueBoard.getAllQueues().isNotEmpty()) {
val queue = queueBoard.getCurrentQueue()
if (queue != null) {
isShuffleEnabled.value = queue.shuffled
CoroutineScope(Dispatchers.Main).launch {
val queuePos = queueBoard.setCurrQueue(this@MusicService, false)
if (queuePos != null) {
player.seekTo(queuePos, dataStore.get(LastPosKey, C.TIME_UNSET))
dataStore.edit { settings ->
settings[LastPosKey] = C.TIME_UNSET
}
}
}
queueBoard.initialized = true
}
}
}
}

fun deInitQueue() {
if (dataStore.get(PersistentQueueKey, true)) {
saveQueueToDisk()
scope.launch {
dataStore.edit { settings ->
settings[LastPosKey] = player.currentPosition
}
}
}
// do not replace the object. Can lead to entire queue being deleted even though it is supposed to be saved already
queueBoard.initialized = false
}

fun updateNotification() {
mediaSession.setCustomLayout(
listOf(
Expand Down Expand Up @@ -488,6 +506,9 @@ class MusicService : MediaLibraryService(),
* If both are unspecified, the title will default to "Queue".
*/
fun playQueue(queue: Queue, playWhenReady: Boolean = true, replace: Boolean = false, title: String? = null) {
if (!queueBoard.initialized) {
initQueue()
}
queueTitle = title
queuePlaylistId = queue.playlistId

Expand Down Expand Up @@ -533,18 +554,23 @@ class MusicService : MediaLibraryService(),
* Add items to queue, right after current playing item
*/
fun enqueueNext(items: List<MediaItem>) {
val currentQueue = queueBoard.getCurrentQueue()
if (currentQueue == null) {
if (!queueBoard.initialized) {
// when enqueuing next when player isn't active, play as a new song
if (items.isNotEmpty()) {
playQueue(
ListQueue(
title = items.first().mediaMetadata.title.toString(),
items = items.mapNotNull { it.metadata }
CoroutineScope(Dispatchers.Main).launch {
playQueue(
ListQueue(
title = items.first().mediaMetadata.title.toString(),
items = items.mapNotNull { it.metadata }
)
)
)
}
}
} else {
queueBoard.addSongsToQueue(currentQueue, player.currentMediaItemIndex + 1, items.mapNotNull { it.metadata }, this)
// enqueue next
queueBoard.getCurrentQueue()?.let {
queueBoard.addSongsToQueue(it, player.currentMediaItemIndex + 1, items.mapNotNull { it.metadata }, this)
}
}
}

Expand Down Expand Up @@ -829,14 +855,7 @@ class MusicService : MediaLibraryService(),
}

override fun onDestroy() {
if (dataStore.get(PersistentQueueKey, true)) {
saveQueueToDisk()
scope.launch {
dataStore.edit { settings ->
settings[LastPosKey] = player.currentPosition
}
}
}
deInitQueue()
mediaSession.release()
player.removeListener(this)
player.removeListener(sleepTimer)
Expand All @@ -847,14 +866,7 @@ class MusicService : MediaLibraryService(),
override fun onBind(intent: Intent?) = super.onBind(intent) ?: binder

override fun onTaskRemoved(rootIntent: Intent?) {
if (dataStore.get(PersistentQueueKey, true)) {
saveQueueToDisk()
scope.launch {
dataStore.edit { settings ->
settings[LastPosKey] = player.currentPosition
}
}
}
deInitQueue()

super.onTaskRemoved(rootIntent)
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/dd3boh/outertune/ui/player/Player.kt
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ import com.dd3boh.outertune.extensions.toggleRepeatMode
import com.dd3boh.outertune.models.MediaMetadata
import com.dd3boh.outertune.models.isShuffleEnabled
import com.dd3boh.outertune.playback.PlayerConnection
import com.dd3boh.outertune.playback.PlayerConnection.Companion.queueBoard
import com.dd3boh.outertune.ui.component.AsyncLocalImage
import com.dd3boh.outertune.ui.component.BottomSheet
import com.dd3boh.outertune.ui.component.BottomSheetState
Expand Down Expand Up @@ -230,6 +231,7 @@ fun BottomSheetPlayer(
onDismiss = {
playerConnection.player.stop()
playerConnection.player.clearMediaItems()
playerConnection.service.deInitQueue()
},
collapsedContent = {
MiniPlayer(
Expand Down

0 comments on commit faf4dc3

Please sign in to comment.