list: rubber-band the swipe reveal, mute swipes while playlist-editing
This commit is contained in:
@@ -125,8 +125,14 @@ abstract class DetailFragment<P : MusicParent, C : Music> :
|
||||
binding.detailRecycler.apply { adapter = getDetailListAdapter() }
|
||||
|
||||
ItemTouchHelper(
|
||||
SwipeActionsCallback(isEnabled = { listModel.selected.value.isEmpty() }) {
|
||||
song,
|
||||
SwipeActionsCallback(
|
||||
// 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 ->
|
||||
when (action) {
|
||||
SwipeActionsCallback.SwipeAction.QUEUE -> playbackModel.addToQueue(song)
|
||||
|
||||
@@ -28,6 +28,7 @@ import androidx.recyclerview.widget.RecyclerView
|
||||
import com.google.android.material.R as MR
|
||||
import kotlin.math.abs
|
||||
import kotlin.math.min
|
||||
import kotlin.math.sign
|
||||
import org.oxycblt.auxio.R
|
||||
import org.oxycblt.auxio.list.adapter.FlexibleListAdapter
|
||||
import org.oxycblt.auxio.util.getAttrColorCompat
|
||||
@@ -98,8 +99,9 @@ class SwipeActionsCallback(
|
||||
song,
|
||||
if (direction == ItemTouchHelper.END) SwipeAction.QUEUE else SwipeAction.PLAY_NEXT,
|
||||
)
|
||||
// The row stays: rebind it in place, cancelling the swipe-out animation.
|
||||
recyclerView.adapter?.notifyItemChanged(position)
|
||||
// The row stays: rebind it in place, cancelling the swipe-out animation. The payload
|
||||
// keeps the item animator from cross-fading the row.
|
||||
recyclerView.adapter?.notifyItemChanged(position, PAYLOAD_SWIPE_COMMIT)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,11 +125,10 @@ class SwipeActionsCallback(
|
||||
actionState: Int,
|
||||
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 =
|
||||
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
|
||||
val reveal = revealWidth(viewHolder).toFloat()
|
||||
dX.coerceIn(-reveal, reveal)
|
||||
rubberBand(dX, revealWidth(viewHolder).toFloat())
|
||||
} else {
|
||||
dX
|
||||
}
|
||||
@@ -137,6 +138,19 @@ class SwipeActionsCallback(
|
||||
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. */
|
||||
private fun revealWidth(viewHolder: RecyclerView.ViewHolder) =
|
||||
viewHolder.itemView.context.getDimenPixels(R.dimen.size_touchable_large)
|
||||
@@ -199,4 +213,9 @@ class SwipeActionsCallback(
|
||||
)
|
||||
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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user