list: commit swipes on spring-back instead of flying off

This commit is contained in:
jay
2026-11-12 16:48:02 -05:00
parent 93c8f8f05f
commit 037c5b1382
3 changed files with 50 additions and 40 deletions
@@ -25,7 +25,6 @@ import android.view.View
import androidx.core.view.updatePadding import androidx.core.view.updatePadding
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.navigation.findNavController import androidx.navigation.findNavController
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import com.google.android.material.appbar.AppBarLayout import com.google.android.material.appbar.AppBarLayout
import com.google.android.material.transition.MaterialSharedAxis import com.google.android.material.transition.MaterialSharedAxis
@@ -143,11 +142,6 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
) )
.attachToRecyclerView(binding.detailRecycler) .attachToRecyclerView(binding.detailRecycler)
// A committed swipe springs the row back via a rebind; a change cross-fade on that
// rebind reads as a flash. Zero the change duration instead of disabling change
// animations — the helper's recovery runs through the animator's change path.
binding.detailRecycler.itemAnimator = DefaultItemAnimator().apply { changeDuration = 0 }
spacingSmall = requireContext().getDimenPixels(R.dimen.spacing_small) spacingSmall = requireContext().getDimenPixels(R.dimen.spacing_small)
} }
@@ -37,9 +37,11 @@ import org.oxycblt.musikr.Song
/** /**
* An [ItemTouchHelper.Callback] giving song rows swipe actions: swipe-right queues the song at * An [ItemTouchHelper.Callback] giving song rows swipe actions: swipe-right queues the song at
* the back of the deck, swipe-left plays it next at the deck's front. The row slides only as far * the back of the deck, swipe-left plays it next at the deck's front. The row slides freely up
* as a square underlay cell that reveals the action's icon, resists past that, and always * to a square underlay cell revealing the action's icon, rubber-bands past it, and on release
* springs back; releasing past the cell commits the action. * always springs back home — every release takes the touch helper's cancel animation, which is
* that spring-back. Committing is tracked here: armed when the finger passes the cell, disarmed
* when pulled back, fired as the row settles ([clearView]).
* *
* @param isEnabled Whether swipes are currently allowed (false during multi-select). * @param isEnabled Whether swipes are currently allowed (false during multi-select).
* @param onAction Called with the swiped [Song] and the committed [SwipeAction]. * @param onAction Called with the swiped [Song] and the committed [SwipeAction].
@@ -63,6 +65,9 @@ class SwipeActionsCallback(
private var queueIcon: Drawable? = null private var queueIcon: Drawable? = null
private var playNextIcon: Drawable? = null private var playNextIcon: Drawable? = null
private var armedViewHolder: RecyclerView.ViewHolder? = null
private var armedAction: SwipeAction? = null
override fun getMovementFlags( override fun getMovementFlags(
recyclerView: RecyclerView, recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder, viewHolder: RecyclerView.ViewHolder,
@@ -90,31 +95,17 @@ class SwipeActionsCallback(
) = false ) = false
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val position = viewHolder.bindingAdapterPosition // Never fires: releases always take the cancel path (see getSwipeThreshold), so the row
val recyclerView = viewHolder.itemView.parent as? RecyclerView ?: return // springs back instead of flying off, and commits are handled in clearView.
val song =
(recyclerView.adapter as? FlexibleListAdapter<*, *>)?.getItem(position) as? Song
?: return
onAction(
song,
if (direction == ItemTouchHelper.END) SwipeAction.QUEUE else SwipeAction.PLAY_NEXT,
)
// The row stays: rebind it in place; the full rebind also tells the touch helper to
// cancel the swipe-away animation and recover the row.
recyclerView.adapter?.notifyItemChanged(position)
} }
/** /** Never auto-commit: the release always springs back, and we track commits ourselves. */
* Commit when released past the revealed square, not halfway across the row. override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder) = 2f
*/
override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float { /** Fling-to-commit would fly the row off-screen; disable it for a consistent spring-back. */
val width = viewHolder.itemView.width override fun getSwipeEscapeVelocity(defaultValue: Float) = Float.MAX_VALUE
return if (width > 0) {
min(1f, revealWidth(viewHolder).toFloat() / width) override fun getSwipeVelocityThreshold(defaultValue: Float) = Float.MAX_VALUE
} else {
0.2f
}
}
override fun onChildDraw( override fun onChildDraw(
c: Canvas, c: Canvas,
@@ -132,12 +123,43 @@ class SwipeActionsCallback(
} else { } else {
dX dX
} }
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && clampedDX != 0f) { if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && isCurrentlyActive) {
drawUnderlay(c, viewHolder, clampedDX) // Arm when the finger passes the cell; disarm when pulled back inside it.
val raw = abs(dX)
val reveal = revealWidth(viewHolder).toFloat()
if (raw > reveal) {
armedViewHolder = viewHolder
armedAction = if (dX > 0) SwipeAction.QUEUE else SwipeAction.PLAY_NEXT
} else if (raw < reveal * 0.5f) {
armedViewHolder = null
armedAction = null
}
if (clampedDX != 0f) {
drawUnderlay(c, viewHolder, clampedDX)
}
} }
super.onChildDraw(c, recyclerView, viewHolder, clampedDX, dY, actionState, isCurrentlyActive) super.onChildDraw(c, recyclerView, viewHolder, clampedDX, dY, actionState, isCurrentlyActive)
} }
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
super.clearView(recyclerView, viewHolder)
// The row has settled home; commit if this gesture armed an action.
if (armedViewHolder === viewHolder) {
val action = armedAction
armedViewHolder = null
armedAction = null
if (action != null) {
val position = viewHolder.bindingAdapterPosition
val song =
(recyclerView.adapter as? FlexibleListAdapter<*, *>)?.getItem(position)
as? Song
if (song != null) {
onAction(song, action)
}
}
}
}
/** /**
* Map a raw drag to its visual translation: one-to-one up to [reveal], then increasing * 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 * resistance asymptotic to twice the reveal, so the row can be pulled past the cell but
@@ -213,5 +235,4 @@ class SwipeActionsCallback(
) )
icon.draw(c) icon.draw(c)
} }
} }
@@ -30,7 +30,6 @@ import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import com.google.android.material.transition.MaterialSharedAxis import com.google.android.material.transition.MaterialSharedAxis
@@ -196,10 +195,6 @@ class SearchFragment : ListFragment<Music, FragmentSearchBinding>() {
) )
.attachToRecyclerView(binding.searchRecycler) .attachToRecyclerView(binding.searchRecycler)
// See DetailFragment: zero-duration change animations, so a committed swipe doesn't
// flash. (Disabling them outright breaks the helper's recovery.)
binding.searchRecycler.itemAnimator = DefaultItemAnimator().apply { changeDuration = 0 }
// --- VIEWMODEL SETUP --- // --- VIEWMODEL SETUP ---
collectImmediately(searchModel.searchResults, ::updateSearchResults) collectImmediately(searchModel.searchResults, ::updateSearchResults)