list: rubber-band the swipe reveal, mute swipes while playlist-editing

This commit is contained in:
jay
2026-11-12 16:32:39 -05:00
parent 94170a8079
commit 18332d8478
2 changed files with 32 additions and 7 deletions
@@ -125,8 +125,14 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
binding.detailRecycler.apply { adapter = getDetailListAdapter() } binding.detailRecycler.apply { adapter = getDetailListAdapter() }
ItemTouchHelper( ItemTouchHelper(
SwipeActionsCallback(isEnabled = { listModel.selected.value.isEmpty() }) { SwipeActionsCallback(
song, // Queue swipes are inert during multi-select and while a playlist is being
// edited (the editing gestures — reorder, remove — own the rows then).
isEnabled = {
listModel.selected.value.isEmpty() &&
detailModel.editedPlaylist.value == null
}
) { song,
action -> action ->
when (action) { when (action) {
SwipeActionsCallback.SwipeAction.QUEUE -> playbackModel.addToQueue(song) SwipeActionsCallback.SwipeAction.QUEUE -> playbackModel.addToQueue(song)
@@ -28,6 +28,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.R as MR import com.google.android.material.R as MR
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.min import kotlin.math.min
import kotlin.math.sign
import org.oxycblt.auxio.R import org.oxycblt.auxio.R
import org.oxycblt.auxio.list.adapter.FlexibleListAdapter import org.oxycblt.auxio.list.adapter.FlexibleListAdapter
import org.oxycblt.auxio.util.getAttrColorCompat import org.oxycblt.auxio.util.getAttrColorCompat
@@ -98,8 +99,9 @@ class SwipeActionsCallback(
song, song,
if (direction == ItemTouchHelper.END) SwipeAction.QUEUE else SwipeAction.PLAY_NEXT, if (direction == ItemTouchHelper.END) SwipeAction.QUEUE else SwipeAction.PLAY_NEXT,
) )
// The row stays: rebind it in place, cancelling the swipe-out animation. // The row stays: rebind it in place, cancelling the swipe-out animation. The payload
recyclerView.adapter?.notifyItemChanged(position) // keeps the item animator from cross-fading the row.
recyclerView.adapter?.notifyItemChanged(position, PAYLOAD_SWIPE_COMMIT)
} }
/** /**
@@ -123,11 +125,10 @@ class SwipeActionsCallback(
actionState: Int, actionState: Int,
isCurrentlyActive: Boolean, isCurrentlyActive: Boolean,
) { ) {
// Clamp the slide to the square underlay: the row resists past the reveal. // Free slide up to the square cell, then rubber-band resistance past it.
val clampedDX = val clampedDX =
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) { if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
val reveal = revealWidth(viewHolder).toFloat() rubberBand(dX, revealWidth(viewHolder).toFloat())
dX.coerceIn(-reveal, reveal)
} else { } else {
dX dX
} }
@@ -137,6 +138,19 @@ class SwipeActionsCallback(
super.onChildDraw(c, recyclerView, viewHolder, clampedDX, dY, actionState, isCurrentlyActive) super.onChildDraw(c, recyclerView, viewHolder, clampedDX, dY, actionState, isCurrentlyActive)
} }
/**
* Map a raw drag to its visual translation: one-to-one up to [reveal], then increasing
* resistance asymptotic to twice the reveal, so the row can be pulled past the cell but
* fights back more the further it goes.
*/
private fun rubberBand(dX: Float, reveal: Float): Float {
val past = abs(dX) - reveal
if (past <= 0f) {
return dX
}
return sign(dX) * (reveal + past / (1f + past / reveal))
}
/** The square underlay cell's width: a standard large touchable size. */ /** The square underlay cell's width: a standard large touchable size. */
private fun revealWidth(viewHolder: RecyclerView.ViewHolder) = private fun revealWidth(viewHolder: RecyclerView.ViewHolder) =
viewHolder.itemView.context.getDimenPixels(R.dimen.size_touchable_large) viewHolder.itemView.context.getDimenPixels(R.dimen.size_touchable_large)
@@ -199,4 +213,9 @@ class SwipeActionsCallback(
) )
icon.draw(c) icon.draw(c)
} }
private companion object {
/** Payload for the in-place rebind after a committed swipe; avoids the cross-fade. */
val PAYLOAD_SWIPE_COMMIT = Any()
}
} }