Open queue scrolled to current track.

This commit is contained in:
Hugh Daschbach 2023-01-04 13:28:44 +00:00 committed by Ryan Harg
parent c0b7e37cb4
commit 1566d1fbcf
4 changed files with 18 additions and 5 deletions

View File

@ -50,7 +50,9 @@ class QueueFragment : BottomSheetDialogFragment() {
return super.onCreateDialog(savedInstanceState).apply {
setOnShowListener {
findViewById<View>(com.google.android.material.R.id.design_bottom_sheet)?.let {
BottomSheetBehavior.from(it).skipCollapsed = true
val behavior = BottomSheetBehavior.from(it)
behavior.skipCollapsed = true
behavior.state = BottomSheetBehavior.STATE_EXPANDED
}
}
}
@ -100,10 +102,10 @@ class QueueFragment : BottomSheetDialogFragment() {
CommandBus.send(Command.ClearQueue)
}
refresh()
refresh(true)
}
private fun refresh() {
private fun refresh(scroll: Boolean) {
lifecycleScope.launch(Main) {
RequestBus.send(Request.GetQueue).wait<Response.Queue>()?.let { response ->
binding.included.let { included ->
@ -120,6 +122,11 @@ class QueueFragment : BottomSheetDialogFragment() {
}
}
}
if (scroll) {
RequestBus.send(Request.GetCurrentTrackIndex).wait<Response.CurrentTrackIndex>()?.let { sresp ->
binding.included.queue.scrollToPosition(sresp.index)
}
}
}
}
}
@ -128,7 +135,7 @@ class QueueFragment : BottomSheetDialogFragment() {
lifecycleScope.launch(Main) {
EventBus.get().collect { message ->
if (message is Event.QueueChanged) {
refresh()
refresh(false)
}
}
}
@ -136,7 +143,7 @@ class QueueFragment : BottomSheetDialogFragment() {
lifecycleScope.launch(Main) {
CommandBus.get().collect { command ->
if (command is Command.RefreshTrack) {
refresh()
refresh(false)
}
}
}

View File

@ -240,6 +240,8 @@ class PlayerService : Service() {
RequestBus.get().collect { request ->
if (request is Request.GetCurrentTrack) {
request.channel?.trySend(Response.CurrentTrack(queue.current()))?.isSuccess
} else if (request is Request.GetCurrentTrackIndex) {
request.channel?.trySend(Response.CurrentTrackIndex(queue.currentIndex()))?.isSuccess
} else if (request is Request.GetState) {
request.channel?.trySend(Response.State(player.playWhenReady))?.isSuccess
} else if (request is Request.GetQueue) {

View File

@ -167,6 +167,8 @@ class QueueManager(val context: Context) {
return metadata.getOrNull(current)
}
fun currentIndex(): Int = (if (current == -1) 0 else current)
fun clear() {
metadata = mutableListOf()
dataSources.clear()

View File

@ -61,6 +61,7 @@ sealed class Request(var channel: Channel<Response>? = null) {
object GetState : Request()
object GetQueue : Request()
object GetCurrentTrack : Request()
object GetCurrentTrackIndex : Request()
object GetDownloads : Request()
}
@ -68,6 +69,7 @@ sealed class Response {
class State(val playing: Boolean) : Response()
class Queue(val queue: List<Track>) : Response()
class CurrentTrack(val track: Track?) : Response()
class CurrentTrackIndex(val index: Int) : Response()
class Downloads(val cursor: DownloadCursor) : Response()
}