queue: introduce corner effect on delete

This commit is contained in:
Alexander Capehart
2026-03-29 19:56:06 -06:00
parent 1e94786074
commit 44d14c5def
5 changed files with 68 additions and 51 deletions
@@ -19,6 +19,7 @@
package org.oxycblt.auxio.detail.list package org.oxycblt.auxio.detail.list
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable import android.graphics.drawable.LayerDrawable
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@@ -231,22 +232,20 @@ private constructor(private val binding: ItemEditableSongBinding) :
override val root = binding.root override val root = binding.root
override val body = binding.body override val body = binding.body
override val delete = binding.background override val delete = binding.background
override val background = override val liftableBackground =
MaterialShapeDrawable.createWithElevationOverlay(binding.root.context).apply { MaterialShapeDrawable.createWithElevationOverlay(binding.root.context).apply {
fillColor = binding.context.getAttrColorCompat(MR.attr.colorSurfaceContainerHigh) fillColor = binding.context.getAttrColorCompat(MR.attr.colorSurfaceContainerHigh)
alpha = 0 alpha = 0
} }
override val roundableBackground: Drawable
get() =
MaterialShapeDrawable.createWithElevationOverlay(binding.context).apply {
fillColor = binding.context.getAttrColorCompat(MR.attr.colorSurface)
}
init { init {
binding.body.background = binding.body.background = LayerDrawable(arrayOf(roundableBackground, liftableBackground))
LayerDrawable(
arrayOf(
MaterialShapeDrawable.createWithElevationOverlay(binding.context).apply {
fillColor = binding.context.getAttrColorCompat(MR.attr.colorSurface)
},
background,
)
)
} }
/** /**
@@ -21,23 +21,21 @@ package org.oxycblt.auxio.list.recycler
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.view.View import android.view.View
import android.view.animation.AccelerateDecelerateInterpolator
import androidx.core.view.isInvisible import androidx.core.view.isInvisible
import androidx.dynamicanimation.animation.SpringAnimation import androidx.dynamicanimation.animation.SpringAnimation
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.R as MR import com.google.android.material.R as MR
import com.google.android.material.shape.MaterialShapeDrawable
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
import kotlin.math.sign import kotlin.math.sign
import org.oxycblt.auxio.R import org.oxycblt.auxio.R
import org.oxycblt.auxio.list.recycler.MaterialDragCallback.ViewHolder
import org.oxycblt.auxio.ui.Effect import org.oxycblt.auxio.ui.Effect
import org.oxycblt.auxio.ui.Spatial import org.oxycblt.auxio.ui.Spatial
import org.oxycblt.auxio.util.getDimen import org.oxycblt.auxio.util.getDimen
import org.oxycblt.auxio.util.getInteger import org.oxycblt.auxio.util.getDimenPixels
import timber.log.Timber as L import timber.log.Timber as L
/** /**
@@ -53,7 +51,7 @@ abstract class MaterialDragCallback : ItemTouchHelper.Callback() {
val elevationAnim: SpringAnimation, val elevationAnim: SpringAnimation,
val translateZAnim: SpringAnimation, val translateZAnim: SpringAnimation,
val alphaAnim: SpringAnimation, val alphaAnim: SpringAnimation,
val causedBy: CausedBy val causedBy: CausedBy,
) { ) {
fun cancel() { fun cancel() {
cornerAnim.cancel() cornerAnim.cancel()
@@ -62,11 +60,12 @@ abstract class MaterialDragCallback : ItemTouchHelper.Callback() {
alphaAnim.cancel() alphaAnim.cancel()
} }
} }
enum class CausedBy { enum class CausedBy {
CLICK, CLICK,
RELEASE RELEASE,
} }
private var shouldLift = true
private val itemCornerSpring = Spatial.DEFAULT private val itemCornerSpring = Spatial.DEFAULT
private val itemAlphaSpring = Effect.DEFAULT private val itemAlphaSpring = Effect.DEFAULT
private val itemTranslateZString = Spatial.DEFAULT private val itemTranslateZString = Spatial.DEFAULT
@@ -127,19 +126,21 @@ abstract class MaterialDragCallback : ItemTouchHelper.Callback() {
// this is only done once when the item is initially picked up. // this is only done once when the item is initially picked up.
if (isCurrentlyActive && actionState == ItemTouchHelper.ACTION_STATE_DRAG) { if (isCurrentlyActive && actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
L.d("Lifting ViewHolder") L.d("Lifting ViewHolder")
val bg = holder.background as MaterialShapeDrawable val bg = holder.liftableBackground as MaterialShapeDrawable
val elevation = recyclerView.context.getDimen(MR.dimen.m3_sys_elevation_level4) val elevation = recyclerView.context.getDimen(MR.dimen.m3_sys_elevation_level4)
val cornerSize = recyclerView.context.getDimen(R.dimen.spacing_mid_large) val cornerSize = recyclerView.context.getDimen(R.dimen.spacing_mid_large)
val itemAnim = itemAnims[holder.root] val itemAnim = itemAnims[holder.root]
if (itemAnim?.causedBy != CausedBy.CLICK) { if (itemAnim?.causedBy != CausedBy.CLICK) {
itemAnim?.cancel() itemAnim?.cancel()
itemAnims[holder.root] = AnimBundle( itemAnims[holder.root] =
cornerAnim = itemCornerSpring.corners(holder.root.context, bg, cornerSize), AnimBundle(
elevationAnim = itemCornerSpring.elevation(holder.root.context, bg, elevation), cornerAnim = itemCornerSpring.corners(holder.root.context, bg, cornerSize),
translateZAnim = itemTranslateZString.translateZ(holder.root, elevation), elevationAnim =
alphaAnim = itemAlphaSpring.alpha(holder.root.context, bg, 255), itemCornerSpring.elevation(holder.root.context, bg, elevation),
causedBy = CausedBy.CLICK translateZAnim = itemTranslateZString.translateZ(holder.root, elevation),
) alphaAnim = itemAlphaSpring.alpha(holder.root.context, bg, 255),
causedBy = CausedBy.CLICK,
)
} }
} }
@@ -158,6 +159,11 @@ abstract class MaterialDragCallback : ItemTouchHelper.Callback() {
// this ourselves. // this ourselves.
holder.body.translationX = dX holder.body.translationX = dX
holder.root.translationY = dY holder.root.translationY = dY
val cornerSize = recyclerView.context.getDimen(R.dimen.spacing_mid_large)
val threshold = recyclerView.context.getDimenPixels(R.dimen.size_touchable_large)
val bg = holder.roundableBackground as MaterialShapeDrawable
val progressiveCornerSize = (cornerSize * abs(dX) / threshold).coerceAtMost(cornerSize)
bg.setCornerSize(progressiveCornerSize)
} }
final override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { final override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
@@ -168,22 +174,21 @@ abstract class MaterialDragCallback : ItemTouchHelper.Callback() {
// translationZ is already non-zero. // translationZ is already non-zero.
if (holder.root.translationZ != 0f) { if (holder.root.translationZ != 0f) {
L.d("Lifting ViewHolder") L.d("Lifting ViewHolder")
val bg = holder.background as MaterialShapeDrawable val bg = holder.liftableBackground as MaterialShapeDrawable
val itemAnim = itemAnims[holder.root] val itemAnim = itemAnims[holder.root]
if (itemAnim?.causedBy != CausedBy.RELEASE) { if (itemAnim?.causedBy != CausedBy.RELEASE) {
itemAnim?.cancel() itemAnim?.cancel()
itemAnims[holder.root] = AnimBundle( itemAnims[holder.root] =
cornerAnim = itemCornerSpring.corners(holder.root.context, bg, 0f), AnimBundle(
elevationAnim = itemCornerSpring.elevation(holder.root.context, bg, 0f), cornerAnim = itemCornerSpring.corners(holder.root.context, bg, 0f),
translateZAnim = itemTranslateZString.translateZ(holder.root, 0f), elevationAnim = itemCornerSpring.elevation(holder.root.context, bg, 0f),
alphaAnim = itemAlphaSpring.alpha(holder.root.context, bg, 0), translateZAnim = itemTranslateZString.translateZ(holder.root, 0f),
causedBy = CausedBy.RELEASE alphaAnim = itemAlphaSpring.alpha(holder.root.context, bg, 0),
) causedBy = CausedBy.RELEASE,
)
} }
} }
shouldLift = true
// Reset translations. We do not call the default implementation, so we must do // Reset translations. We do not call the default implementation, so we must do
// this ourselves. // this ourselves.
holder.body.translationX = 0f holder.body.translationX = 0f
@@ -204,7 +209,9 @@ abstract class MaterialDragCallback : ItemTouchHelper.Callback() {
/** The scrim view showing the delete icon. Should be behind [body]. */ /** The scrim view showing the delete icon. Should be behind [body]. */
val delete: View val delete: View
/** The drawable of the [body] background that can be elevated. */ /** The drawable of the [body] background that can be elevated. */
val background: Drawable val liftableBackground: Drawable
/** The drawable of the [body] background that can be rounded when dragging. */
val roundableBackground: Drawable
} }
companion object { companion object {
@@ -30,7 +30,6 @@ import org.oxycblt.auxio.music.resolveNames
import org.oxycblt.auxio.playback.state.RepeatMode import org.oxycblt.auxio.playback.state.RepeatMode
import org.oxycblt.auxio.ui.ViewBindingFragment import org.oxycblt.auxio.ui.ViewBindingFragment
import org.oxycblt.auxio.util.collectImmediately import org.oxycblt.auxio.util.collectImmediately
import org.oxycblt.auxio.util.getDimenPixels
import org.oxycblt.musikr.Song import org.oxycblt.musikr.Song
import timber.log.Timber as L import timber.log.Timber as L
@@ -19,6 +19,7 @@
package org.oxycblt.auxio.playback.queue package org.oxycblt.auxio.playback.queue
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.graphics.drawable.Drawable
import android.graphics.drawable.LayerDrawable import android.graphics.drawable.LayerDrawable
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
@@ -117,12 +118,17 @@ class QueueSongViewHolder private constructor(private val binding: ItemEditableS
override val root = binding.root override val root = binding.root
override val body = binding.body override val body = binding.body
override val delete = binding.background override val delete = binding.background
override val background = override val liftableBackground =
MaterialShapeDrawable.createWithElevationOverlay(binding.root.context).apply { MaterialShapeDrawable.createWithElevationOverlay(binding.root.context).apply {
fillColor = binding.context.getAttrColorCompat(MR.attr.colorSurfaceContainerHighest) fillColor = binding.context.getAttrColorCompat(MR.attr.colorSurfaceContainerHighest)
alpha = 0 alpha = 0
} }
override val roundableBackground: Drawable =
MaterialShapeDrawable.createWithElevationOverlay(binding.context).apply {
fillColor = binding.context.getAttrColorCompat(MR.attr.colorSurfaceContainerHigh)
}
/** /**
* Whether this ViewHolder should be full-opacity to represent a future item, or greyed out to * Whether this ViewHolder should be full-opacity to represent a future item, or greyed out to
* represent a past item. True if former, false if latter. * represent a past item. True if former, false if latter.
@@ -136,16 +142,7 @@ class QueueSongViewHolder private constructor(private val binding: ItemEditableS
} }
init { init {
binding.body.background = binding.body.background = LayerDrawable(arrayOf(roundableBackground, liftableBackground))
LayerDrawable(
arrayOf(
MaterialShapeDrawable.createWithElevationOverlay(binding.context).apply {
fillColor =
binding.context.getAttrColorCompat(MR.attr.colorSurfaceContainerHigh)
},
background,
)
)
} }
/** /**
@@ -80,7 +80,12 @@ class Spatial private constructor(@AttrRes val attr: Int, val defaultStyle: Int)
} }
} }
fun elevation(context: Context, drawable: MaterialShapeDrawable, to: Float, jumpOnCancellation: Boolean = false): SpringAnimation { fun elevation(
context: Context,
drawable: MaterialShapeDrawable,
to: Float,
jumpOnCancellation: Boolean = false,
): SpringAnimation {
val from = drawable.elevation val from = drawable.elevation
val springForce = resolve(context) val springForce = resolve(context)
return SpringAnimation(FloatValueHolder(from)).apply { return SpringAnimation(FloatValueHolder(from)).apply {
@@ -95,7 +100,12 @@ class Spatial private constructor(@AttrRes val attr: Int, val defaultStyle: Int)
} }
} }
fun corners(context: Context, drawable: MaterialShapeDrawable, to: Float, jumpOnCancellation: Boolean = false): SpringAnimation { fun corners(
context: Context,
drawable: MaterialShapeDrawable,
to: Float,
jumpOnCancellation: Boolean = false,
): SpringAnimation {
val from = drawable.topRightCornerResolvedSize val from = drawable.topRightCornerResolvedSize
val springForce = resolve(context) val springForce = resolve(context)
return SpringAnimation(FloatValueHolder(from)).apply { return SpringAnimation(FloatValueHolder(from)).apply {
@@ -151,7 +161,12 @@ class Effect private constructor(@AttrRes val attr: Int, @StyleRes val defaultSt
} }
} }
fun alpha(context: Context, drawable: Drawable, to: Int, jumpOnCancellation: Boolean = false): SpringAnimation { fun alpha(
context: Context,
drawable: Drawable,
to: Int,
jumpOnCancellation: Boolean = false,
): SpringAnimation {
val from = drawable.alpha val from = drawable.alpha
val springForce = resolve(context) val springForce = resolve(context)
return SpringAnimation(FloatValueHolder(from.toFloat())).apply { return SpringAnimation(FloatValueHolder(from.toFloat())).apply {