diff --git a/app/src/main/java/dev/brahmkshatriya/echo/player/ui/ConnectPlayerToUI.kt b/app/src/main/java/dev/brahmkshatriya/echo/player/ui/ConnectPlayerToUI.kt index d6a14f1f..82c5daca 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/player/ui/ConnectPlayerToUI.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/player/ui/ConnectPlayerToUI.kt @@ -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) { diff --git a/app/src/main/java/dev/brahmkshatriya/echo/player/ui/CreatePlayerUI.kt b/app/src/main/java/dev/brahmkshatriya/echo/player/ui/CreatePlayerUI.kt index eb5b58a0..492458aa 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/player/ui/CreatePlayerUI.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/player/ui/CreatePlayerUI.kt @@ -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) } @@ -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 @@ -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 } } } @@ -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) { diff --git a/app/src/main/java/dev/brahmkshatriya/echo/player/ui/PlayerListener.kt b/app/src/main/java/dev/brahmkshatriya/echo/player/ui/PlayerListener.kt index 44481912..3a502bb9 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/player/ui/PlayerListener.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/player/ui/PlayerListener.kt @@ -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( diff --git a/app/src/main/java/dev/brahmkshatriya/echo/ui/adapters/PlaylistAdapter.kt b/app/src/main/java/dev/brahmkshatriya/echo/ui/adapters/PlaylistAdapter.kt index 085dbb12..08d87ed3 100644 --- a/app/src/main/java/dev/brahmkshatriya/echo/ui/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/dev/brahmkshatriya/echo/ui/adapters/PlaylistAdapter.kt @@ -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 @@ -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) @@ -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 } } }