From 037c5b13823f6b60428a42642278679e6f758f12 Mon Sep 17 00:00:00 2001 From: Jay Date: Thu, 12 Nov 2026 16:48:02 -0500 Subject: [PATCH] list: commit swipes on spring-back instead of flying off --- .../oxycblt/auxio/detail/DetailFragment.kt | 6 -- .../list/recycler/SwipeActionsCallback.kt | 79 ++++++++++++------- .../oxycblt/auxio/search/SearchFragment.kt | 5 -- 3 files changed, 50 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/org/oxycblt/auxio/detail/DetailFragment.kt b/app/src/main/java/org/oxycblt/auxio/detail/DetailFragment.kt index 89b873f17..b0e98cccf 100644 --- a/app/src/main/java/org/oxycblt/auxio/detail/DetailFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/detail/DetailFragment.kt @@ -25,7 +25,6 @@ import android.view.View import androidx.core.view.updatePadding import androidx.fragment.app.activityViewModels import androidx.navigation.findNavController -import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.ItemTouchHelper import com.google.android.material.appbar.AppBarLayout import com.google.android.material.transition.MaterialSharedAxis @@ -143,11 +142,6 @@ abstract class DetailFragment

: ) .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) } diff --git a/app/src/main/java/org/oxycblt/auxio/list/recycler/SwipeActionsCallback.kt b/app/src/main/java/org/oxycblt/auxio/list/recycler/SwipeActionsCallback.kt index e1759abee..2006d2d8b 100644 --- a/app/src/main/java/org/oxycblt/auxio/list/recycler/SwipeActionsCallback.kt +++ b/app/src/main/java/org/oxycblt/auxio/list/recycler/SwipeActionsCallback.kt @@ -37,9 +37,11 @@ import org.oxycblt.musikr.Song /** * 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 - * as a square underlay cell that reveals the action's icon, resists past that, and always - * springs back; releasing past the cell commits the action. + * the back of the deck, swipe-left plays it next at the deck's front. The row slides freely up + * to a square underlay cell revealing the action's icon, rubber-bands past it, and on release + * 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 onAction Called with the swiped [Song] and the committed [SwipeAction]. @@ -63,6 +65,9 @@ class SwipeActionsCallback( private var queueIcon: Drawable? = null private var playNextIcon: Drawable? = null + private var armedViewHolder: RecyclerView.ViewHolder? = null + private var armedAction: SwipeAction? = null + override fun getMovementFlags( recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, @@ -90,31 +95,17 @@ class SwipeActionsCallback( ) = false override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) { - val position = viewHolder.bindingAdapterPosition - val recyclerView = viewHolder.itemView.parent as? RecyclerView ?: return - 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 fires: releases always take the cancel path (see getSwipeThreshold), so the row + // springs back instead of flying off, and commits are handled in clearView. } - /** - * Commit when released past the revealed square, not halfway across the row. - */ - override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder): Float { - val width = viewHolder.itemView.width - return if (width > 0) { - min(1f, revealWidth(viewHolder).toFloat() / width) - } else { - 0.2f - } - } + /** Never auto-commit: the release always springs back, and we track commits ourselves. */ + override fun getSwipeThreshold(viewHolder: RecyclerView.ViewHolder) = 2f + + /** Fling-to-commit would fly the row off-screen; disable it for a consistent spring-back. */ + override fun getSwipeEscapeVelocity(defaultValue: Float) = Float.MAX_VALUE + + override fun getSwipeVelocityThreshold(defaultValue: Float) = Float.MAX_VALUE override fun onChildDraw( c: Canvas, @@ -132,12 +123,43 @@ class SwipeActionsCallback( } else { dX } - if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && clampedDX != 0f) { - drawUnderlay(c, viewHolder, clampedDX) + if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && isCurrentlyActive) { + // 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) } + 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 * 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) } - } diff --git a/app/src/main/java/org/oxycblt/auxio/search/SearchFragment.kt b/app/src/main/java/org/oxycblt/auxio/search/SearchFragment.kt index 54b2c649a..e1fdb3f62 100644 --- a/app/src/main/java/org/oxycblt/auxio/search/SearchFragment.kt +++ b/app/src/main/java/org/oxycblt/auxio/search/SearchFragment.kt @@ -30,7 +30,6 @@ import androidx.core.widget.addTextChangedListener import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels import androidx.navigation.fragment.findNavController -import androidx.recyclerview.widget.DefaultItemAnimator import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.ItemTouchHelper import com.google.android.material.transition.MaterialSharedAxis @@ -196,10 +195,6 @@ class SearchFragment : ListFragment() { ) .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 --- collectImmediately(searchModel.searchResults, ::updateSearchResults)