playback: move drag helper to list
Move most of QueueDragCallback to the list module. This is planned to be re-used with the playlist view, so it should be shared.
This commit is contained in:
@@ -158,6 +158,8 @@ constructor(
|
||||
val playlistInstructions: Event<UpdateInstructions>
|
||||
get() = _playlistInstructions
|
||||
|
||||
private var isEditingPlaylist = false
|
||||
|
||||
/** The current [Sort] used for [Song]s in [playlistList]. */
|
||||
var playlistSongSort: Sort
|
||||
get() = musicSettings.playlistSongSort
|
||||
@@ -412,7 +414,7 @@ constructor(
|
||||
val list = mutableListOf<Item>()
|
||||
|
||||
if (playlist.songs.isNotEmpty()) {
|
||||
val header = SortHeader(R.string.lbl_songs)
|
||||
val header = BasicHeader(R.string.lbl_songs)
|
||||
list.add(Divider(header))
|
||||
list.add(header)
|
||||
if (replace) {
|
||||
|
||||
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
* Copyright (c) 2021 Auxio Project
|
||||
* ExtendedDragCallback.kt is part of Auxio.
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package org.oxycblt.auxio.list.recycler
|
||||
|
||||
import android.graphics.Canvas
|
||||
import android.graphics.drawable.Drawable
|
||||
import android.view.View
|
||||
import android.view.animation.AccelerateDecelerateInterpolator
|
||||
import androidx.core.view.isInvisible
|
||||
import androidx.recyclerview.widget.ItemTouchHelper
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.util.getDimen
|
||||
import org.oxycblt.auxio.util.getInteger
|
||||
import org.oxycblt.auxio.util.logD
|
||||
|
||||
/**
|
||||
* A highly customized [ItemTouchHelper.Callback] that enables some extra eye candy in editable UIs,
|
||||
* such as an animation when lifting items. Note that this requires a [ViewHolder] implementation in
|
||||
* order to function.
|
||||
*
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
abstract class MaterialDragCallback : ItemTouchHelper.Callback() {
|
||||
private var shouldLift = true
|
||||
|
||||
final override fun getMovementFlags(
|
||||
recyclerView: RecyclerView,
|
||||
viewHolder: RecyclerView.ViewHolder
|
||||
) =
|
||||
if (viewHolder is ViewHolder) {
|
||||
makeFlag(
|
||||
ItemTouchHelper.ACTION_STATE_DRAG, ItemTouchHelper.UP or ItemTouchHelper.DOWN) or
|
||||
makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE, ItemTouchHelper.START)
|
||||
} else {
|
||||
0
|
||||
}
|
||||
|
||||
final override fun onChildDraw(
|
||||
c: Canvas,
|
||||
recyclerView: RecyclerView,
|
||||
viewHolder: RecyclerView.ViewHolder,
|
||||
dX: Float,
|
||||
dY: Float,
|
||||
actionState: Int,
|
||||
isCurrentlyActive: Boolean
|
||||
) {
|
||||
val holder = viewHolder as ViewHolder
|
||||
|
||||
// Hook drag events to "lifting" the item (i.e raising it's elevation). Make sure
|
||||
// this is only done once when the item is initially picked up.
|
||||
// TODO: I think this is possible to improve with a raw ValueAnimator.
|
||||
if (shouldLift && isCurrentlyActive && actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
|
||||
logD("Lifting item")
|
||||
|
||||
val bg = holder.background
|
||||
val elevation = recyclerView.context.getDimen(R.dimen.elevation_normal)
|
||||
holder.root
|
||||
.animate()
|
||||
.translationZ(elevation)
|
||||
.setDuration(
|
||||
recyclerView.context.getInteger(R.integer.anim_fade_exit_duration).toLong())
|
||||
.setUpdateListener {
|
||||
bg.alpha = ((holder.root.translationZ / elevation) * 255).toInt()
|
||||
}
|
||||
.setInterpolator(AccelerateDecelerateInterpolator())
|
||||
.start()
|
||||
|
||||
shouldLift = false
|
||||
}
|
||||
|
||||
// We show a background with a delete icon behind the item each time one is swiped
|
||||
// away. To avoid working with canvas, this is simply placed behind the body.
|
||||
// That comes with a couple of problems, however. For one, the background view will always
|
||||
// lag behind the body view, resulting in a noticeable pixel offset when dragging. To fix
|
||||
// this, we make this a separate view and make this view invisible whenever the item is
|
||||
// not being swiped. This issue is also the reason why the background is not merged with
|
||||
// the FrameLayout within the item.
|
||||
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
|
||||
holder.delete.isInvisible = dX == 0f
|
||||
}
|
||||
|
||||
// Update other translations. We do not call the default implementation, so we must do
|
||||
// this ourselves.
|
||||
holder.body.translationX = dX
|
||||
holder.root.translationY = dY
|
||||
}
|
||||
|
||||
final override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
|
||||
// When an elevated item is cleared, we reset the elevation using another animation.
|
||||
val holder = viewHolder as ViewHolder
|
||||
|
||||
// This function can be called multiple times, so only start the animation when the view's
|
||||
// translationZ is already non-zero.
|
||||
if (holder.root.translationZ != 0f) {
|
||||
logD("Dropping item")
|
||||
|
||||
val bg = holder.background
|
||||
val elevation = recyclerView.context.getDimen(R.dimen.elevation_normal)
|
||||
holder.root
|
||||
.animate()
|
||||
.translationZ(0f)
|
||||
.setDuration(
|
||||
recyclerView.context.getInteger(R.integer.anim_fade_exit_duration).toLong())
|
||||
.setUpdateListener {
|
||||
bg.alpha = ((holder.root.translationZ / elevation) * 255).toInt()
|
||||
}
|
||||
.setInterpolator(AccelerateDecelerateInterpolator())
|
||||
.start()
|
||||
}
|
||||
|
||||
shouldLift = true
|
||||
|
||||
// Reset translations. We do not call the default implementation, so we must do
|
||||
// this ourselves.
|
||||
holder.body.translationX = 0f
|
||||
holder.root.translationY = 0f
|
||||
}
|
||||
|
||||
// Long-press events are too buggy, only allow dragging with the handle.
|
||||
final override fun isLongPressDragEnabled() = false
|
||||
|
||||
/** Required [RecyclerView.ViewHolder] implementation that exposes the following. */
|
||||
interface ViewHolder {
|
||||
/** The root view containing the delete scrim and information. */
|
||||
val root: View
|
||||
/** The body view containing music information. */
|
||||
val body: View
|
||||
/** The scrim view showing the delete icon. Should be behind [body]. */
|
||||
val delete: View
|
||||
/** The drawable of the [body] background that can be elevated. */
|
||||
val background: Drawable
|
||||
}
|
||||
}
|
||||
@@ -26,9 +26,10 @@ import androidx.core.view.isInvisible
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.android.material.shape.MaterialShapeDrawable
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.databinding.ItemQueueSongBinding
|
||||
import org.oxycblt.auxio.databinding.ItemEditableSongBinding
|
||||
import org.oxycblt.auxio.list.EditableListListener
|
||||
import org.oxycblt.auxio.list.adapter.*
|
||||
import org.oxycblt.auxio.list.recycler.MaterialDragCallback
|
||||
import org.oxycblt.auxio.list.recycler.SongViewHolder
|
||||
import org.oxycblt.auxio.music.Song
|
||||
import org.oxycblt.auxio.music.resolveNames
|
||||
@@ -96,34 +97,32 @@ class QueueAdapter(private val listener: EditableListListener<Song>) :
|
||||
}
|
||||
|
||||
/**
|
||||
* A [PlayingIndicatorAdapter.ViewHolder] that displays a queue [Song]. Use [from] to create an
|
||||
* instance.
|
||||
* A [PlayingIndicatorAdapter.ViewHolder] that displays an editable [Song] which can be re-ordered
|
||||
* and removed. Use [from] to create an instance.
|
||||
*
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
class QueueSongViewHolder private constructor(private val binding: ItemQueueSongBinding) :
|
||||
PlayingIndicatorAdapter.ViewHolder(binding.root) {
|
||||
/** The "body" view of this [QueueSongViewHolder] that shows the [Song] information. */
|
||||
val bodyView: View
|
||||
class QueueSongViewHolder private constructor(private val binding: ItemEditableSongBinding) :
|
||||
PlayingIndicatorAdapter.ViewHolder(binding.root), MaterialDragCallback.ViewHolder {
|
||||
override val root: View
|
||||
get() = binding.root
|
||||
|
||||
override val body: View
|
||||
get() = binding.body
|
||||
|
||||
/** The background view of this [QueueSongViewHolder] that shows the delete icon. */
|
||||
val backgroundView: View
|
||||
override val delete: View
|
||||
get() = binding.background
|
||||
|
||||
/** The actual background drawable of this [QueueSongViewHolder] that can be manipulated. */
|
||||
val backgroundDrawable =
|
||||
override val background =
|
||||
MaterialShapeDrawable.createWithElevationOverlay(binding.root.context).apply {
|
||||
fillColor = binding.context.getAttrColorCompat(R.attr.colorSurface)
|
||||
elevation = binding.context.getDimen(R.dimen.elevation_normal) * 5
|
||||
alpha = 0
|
||||
}
|
||||
|
||||
/** If this queue item is considered "in the future" (i.e has not played yet). */
|
||||
var isFuture: Boolean
|
||||
get() = binding.songAlbumCover.isEnabled
|
||||
set(value) {
|
||||
// Don't want to disable clicking, just indicate the body and handle is disabled
|
||||
binding.songAlbumCover.isEnabled = value
|
||||
binding.songName.isEnabled = value
|
||||
binding.songInfo.isEnabled = value
|
||||
@@ -137,7 +136,7 @@ class QueueSongViewHolder private constructor(private val binding: ItemQueueSong
|
||||
fillColor = binding.context.getAttrColorCompat(R.attr.colorSurface)
|
||||
elevation = binding.context.getDimen(R.dimen.elevation_normal)
|
||||
},
|
||||
backgroundDrawable))
|
||||
background))
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -148,7 +147,7 @@ class QueueSongViewHolder private constructor(private val binding: ItemQueueSong
|
||||
*/
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
fun bind(song: Song, listener: EditableListListener<Song>) {
|
||||
listener.bind(song, this, bodyView, binding.songDragHandle)
|
||||
listener.bind(song, this, body, binding.songDragHandle)
|
||||
binding.songAlbumCover.bind(song)
|
||||
binding.songName.text = song.name.resolve(binding.context)
|
||||
binding.songInfo.text = song.artists.resolveNames(binding.context)
|
||||
@@ -170,7 +169,7 @@ class QueueSongViewHolder private constructor(private val binding: ItemQueueSong
|
||||
* @return A new instance.
|
||||
*/
|
||||
fun from(parent: View) =
|
||||
QueueSongViewHolder(ItemQueueSongBinding.inflate(parent.context.inflater))
|
||||
QueueSongViewHolder(ItemEditableSongBinding.inflate(parent.context.inflater))
|
||||
|
||||
/** A comparator that can be used with DiffUtil. */
|
||||
val DIFF_CALLBACK = SongViewHolder.DIFF_CALLBACK
|
||||
|
||||
@@ -18,15 +18,9 @@
|
||||
|
||||
package org.oxycblt.auxio.playback.queue
|
||||
|
||||
import android.graphics.Canvas
|
||||
import android.view.animation.AccelerateDecelerateInterpolator
|
||||
import androidx.core.view.isInvisible
|
||||
import androidx.recyclerview.widget.ItemTouchHelper
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.util.getDimen
|
||||
import org.oxycblt.auxio.util.getInteger
|
||||
import org.oxycblt.auxio.util.logD
|
||||
import org.oxycblt.auxio.list.recycler.MaterialDragCallback
|
||||
|
||||
/**
|
||||
* A highly customized [ItemTouchHelper.Callback] that enables some extra eye candy in the queue UI,
|
||||
@@ -34,108 +28,16 @@ import org.oxycblt.auxio.util.logD
|
||||
*
|
||||
* @author Alexander Capehart (OxygenCobalt)
|
||||
*/
|
||||
class QueueDragCallback(private val playbackModel: QueueViewModel) : ItemTouchHelper.Callback() {
|
||||
private var shouldLift = true
|
||||
|
||||
override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) =
|
||||
makeFlag(ItemTouchHelper.ACTION_STATE_DRAG, ItemTouchHelper.UP or ItemTouchHelper.DOWN) or
|
||||
makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE, ItemTouchHelper.START)
|
||||
|
||||
override fun onChildDraw(
|
||||
c: Canvas,
|
||||
recyclerView: RecyclerView,
|
||||
viewHolder: RecyclerView.ViewHolder,
|
||||
dX: Float,
|
||||
dY: Float,
|
||||
actionState: Int,
|
||||
isCurrentlyActive: Boolean
|
||||
) {
|
||||
val holder = viewHolder as QueueSongViewHolder
|
||||
|
||||
// Hook drag events to "lifting" the queue item (i.e raising it's elevation). Make sure
|
||||
// this is only done once when the item is initially picked up.
|
||||
// TODO: I think this is possible to improve with a raw ValueAnimator.
|
||||
if (shouldLift && isCurrentlyActive && actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
|
||||
logD("Lifting queue item")
|
||||
|
||||
val bg = holder.backgroundDrawable
|
||||
val elevation = recyclerView.context.getDimen(R.dimen.elevation_normal)
|
||||
holder.itemView
|
||||
.animate()
|
||||
.translationZ(elevation)
|
||||
.setDuration(
|
||||
recyclerView.context.getInteger(R.integer.anim_fade_exit_duration).toLong())
|
||||
.setUpdateListener {
|
||||
bg.alpha = ((holder.itemView.translationZ / elevation) * 255).toInt()
|
||||
}
|
||||
.setInterpolator(AccelerateDecelerateInterpolator())
|
||||
.start()
|
||||
|
||||
shouldLift = false
|
||||
}
|
||||
|
||||
// We show a background with a delete icon behind the queue song each time one is swiped
|
||||
// away. To avoid working with canvas, this is simply placed behind the queue body.
|
||||
// That comes with a couple of problems, however. For one, the background view will always
|
||||
// lag behind the body view, resulting in a noticeable pixel offset when dragging. To fix
|
||||
// this, we make this a separate view and make this view invisible whenever the item is
|
||||
// not being swiped. This issue is also the reason why the background is not merged with
|
||||
// the FrameLayout within the queue item.
|
||||
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
|
||||
holder.backgroundView.isInvisible = dX == 0f
|
||||
}
|
||||
|
||||
// Update other translations. We do not call the default implementation, so we must do
|
||||
// this ourselves.
|
||||
holder.bodyView.translationX = dX
|
||||
holder.itemView.translationY = dY
|
||||
}
|
||||
|
||||
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
|
||||
// When an elevated item is cleared, we reset the elevation using another animation.
|
||||
val holder = viewHolder as QueueSongViewHolder
|
||||
|
||||
// This function can be called multiple times, so only start the animation when the view's
|
||||
// translationZ is already non-zero.
|
||||
if (holder.itemView.translationZ != 0f) {
|
||||
logD("Dropping queue item")
|
||||
|
||||
val bg = holder.backgroundDrawable
|
||||
val elevation = recyclerView.context.getDimen(R.dimen.elevation_normal)
|
||||
holder.itemView
|
||||
.animate()
|
||||
.translationZ(0f)
|
||||
.setDuration(
|
||||
recyclerView.context.getInteger(R.integer.anim_fade_exit_duration).toLong())
|
||||
.setUpdateListener {
|
||||
bg.alpha = ((holder.itemView.translationZ / elevation) * 255).toInt()
|
||||
}
|
||||
.setInterpolator(AccelerateDecelerateInterpolator())
|
||||
.start()
|
||||
}
|
||||
|
||||
shouldLift = true
|
||||
|
||||
// Reset translations. We do not call the default implementation, so we must do
|
||||
// this ourselves.
|
||||
holder.bodyView.translationX = 0f
|
||||
holder.itemView.translationY = 0f
|
||||
}
|
||||
|
||||
class QueueDragCallback(private val queueModel: QueueViewModel) : MaterialDragCallback() {
|
||||
override fun onMove(
|
||||
recyclerView: RecyclerView,
|
||||
viewHolder: RecyclerView.ViewHolder,
|
||||
target: RecyclerView.ViewHolder
|
||||
): Boolean {
|
||||
logD("${viewHolder.bindingAdapterPosition} ${target.bindingAdapterPosition}")
|
||||
return playbackModel.moveQueueDataItems(
|
||||
) =
|
||||
queueModel.moveQueueDataItems(
|
||||
viewHolder.bindingAdapterPosition, target.bindingAdapterPosition)
|
||||
}
|
||||
|
||||
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
|
||||
playbackModel.removeQueueDataItem(viewHolder.bindingAdapterPosition)
|
||||
queueModel.removeQueueDataItem(viewHolder.bindingAdapterPosition)
|
||||
}
|
||||
|
||||
// Long-press events are too buggy, only allow dragging with the handle.
|
||||
override fun isLongPressDragEnabled() = false
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user