Skip to content

Commit

Permalink
Fix Playlists
Browse files Browse the repository at this point in the history
  • Loading branch information
brahmkshatriya committed Feb 23, 2024
1 parent e63dd87 commit fbf5379
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ fun connectPlayerToUI(activity: MainActivity, player: MediaBrowser) {
observe(playerViewModel.audioIndexFlow) {
if (it >= 0) {
player.seekToDefaultPosition(it)
uiViewModel.playlist.emit(it)
}
}
observe(playerViewModel.seekTo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,27 +206,29 @@ fun createPlayerUI(


val linearLayoutManager = LinearLayoutManager(activity, VERTICAL, false)
var adapter: PlaylistAdapter? = null
val callback = object : ItemTouchHelper.SimpleCallback(UP or DOWN, START) {
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
playerViewModel.moveQueueItems(
viewHolder.bindingAdapterPosition, target.bindingAdapterPosition
)
val new = viewHolder.bindingAdapterPosition
val old = target.bindingAdapterPosition
playerViewModel.moveQueueItems(new, old)
adapter?.notifyItemMoved(new, old)
return true
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
playerViewModel.removeQueueItem(
viewHolder.bindingAdapterPosition
)
val pos = viewHolder.bindingAdapterPosition
playerViewModel.removeQueueItem(pos)
adapter?.notifyItemRemoved(pos)
}
}
val touchHelper = ItemTouchHelper(callback)
val adapter = PlaylistAdapter(object : PlaylistAdapter.Callback() {
override fun onDragHandleClicked(viewHolder: PlaylistAdapter.ViewHolder) {
adapter = PlaylistAdapter(object : PlaylistAdapter.Callback() {
override fun onDragHandleTouched(viewHolder: PlaylistAdapter.ViewHolder) {
touchHelper.startDrag(viewHolder)
}

Expand All @@ -236,9 +238,12 @@ fun createPlayerUI(

override fun onItemClosedClicked(position: Int) {
playerViewModel.removeQueueItem(position)
adapter?.notifyItemRemoved(position)
}
})



playlistBinding.playlistRecycler.apply {
layoutManager = linearLayoutManager
this.adapter = adapter
Expand All @@ -264,9 +269,9 @@ fun createPlayerUI(
container.post {
if (bottomPlayerBehavior.state == STATE_HIDDEN) {
bottomPlayerBehavior.isHideable = false
bottomPlayerBehavior.isDraggable = true
bottomPlayerBehavior.state = STATE_COLLAPSED
bottomPlaylistBehavior.state = STATE_COLLAPSED
bottomPlayerBehavior.isDraggable = true
}
}
}
Expand Down Expand Up @@ -345,9 +350,13 @@ fun createPlayerUI(
}
}
observe(uiViewModel.playlist) {
val viewHolder =
playlistBinding.playlistRecycler.findViewHolderForAdapterPosition(it) as PlaylistAdapter.ViewHolder?
adapter.setCurrent(viewHolder)
playlistBinding.playlistRecycler.apply {
post {
val viewHolder =
findViewHolderForAdapterPosition(it) as PlaylistAdapter.ViewHolder?
adapter.setCurrent(viewHolder)
}
}
}

observe(playerViewModel.clearQueueFlow) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class PlayerListener(
override fun onMediaItemTransition(mediaItem: MediaItem?, reason: Int) {
val id = mediaItem?.mediaId ?: return
viewModel.track.value = Global.getTrack(id)
viewModel.playlist.value = player.currentMediaItemIndex
}

override fun onPositionDiscontinuity(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dev.brahmkshatriya.echo.ui.adapters

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.MotionEvent.ACTION_DOWN
import android.view.ViewGroup
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
Expand All @@ -16,18 +18,22 @@ class PlaylistAdapter(

open class Callback {
open fun onItemClicked(position: Int) {}
open fun onDragHandleClicked(viewHolder: ViewHolder) {}
open fun onDragHandleTouched(viewHolder: ViewHolder) {}
open fun onItemClosedClicked(position: Int) {}
}

@SuppressLint("ClickableViewAccessibility")
inner class ViewHolder(val binding: ItemPlaylistItemBinding) :
RecyclerView.ViewHolder(binding.root) {
init {
binding.playlistItemClose.setOnClickListener {
callback.onItemClosedClicked(bindingAdapterPosition)
}
binding.playlistItemDragHandle.setOnClickListener {
callback.onDragHandleClicked(this)
binding.playlistItemDragHandle.setOnTouchListener { _, event ->
if (event.actionMasked != ACTION_DOWN)
return@setOnTouchListener false
callback.onDragHandleTouched(this)
true
}
binding.root.setOnClickListener {
callback.onItemClicked(bindingAdapterPosition)
Expand All @@ -47,15 +53,20 @@ class PlaylistAdapter(
binding.playlistItemTitle.text = track.title
track.cover?.loadInto(binding.playlistItemImageView)
binding.playlistItemAuthor.text = track.artists.joinToString(", ") { it.name }
binding.playlistCurrentItem.isVisible = holder == oldViewHolder
if (position == currentPosition) {
binding.playlistCurrentItem.isVisible = true
oldViewHolder = holder
}
}

private var currentPosition = 0
private var oldViewHolder: ViewHolder? = null
fun setCurrent(viewHolder: ViewHolder?) {
oldViewHolder?.binding?.playlistCurrentItem?.isVisible = false
oldViewHolder = viewHolder
viewHolder?.binding?.playlistCurrentItem?.apply {
isVisible = true
viewHolder?.apply {
binding.playlistCurrentItem.isVisible = true
currentPosition = bindingAdapterPosition
oldViewHolder = this
}
}
}

0 comments on commit fbf5379

Please sign in to comment.