diff --git a/app/src/main/java/org/oxycblt/auxio/list/recycler/MaterialDragCallback.kt b/app/src/main/java/org/oxycblt/auxio/list/recycler/MaterialDragCallback.kt index 28112ca61..97eed4987 100644 --- a/app/src/main/java/org/oxycblt/auxio/list/recycler/MaterialDragCallback.kt +++ b/app/src/main/java/org/oxycblt/auxio/list/recycler/MaterialDragCallback.kt @@ -25,6 +25,10 @@ import android.view.animation.AccelerateDecelerateInterpolator import androidx.core.view.isInvisible import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.RecyclerView +import kotlin.math.abs +import kotlin.math.max +import kotlin.math.min +import kotlin.math.sign import org.oxycblt.auxio.R import org.oxycblt.auxio.list.recycler.MaterialDragCallback.ViewHolder import org.oxycblt.auxio.util.getDimen @@ -53,6 +57,27 @@ abstract class MaterialDragCallback : ItemTouchHelper.Callback() { 0 } + override fun interpolateOutOfBoundsScroll( + recyclerView: RecyclerView, + viewSize: Int, + viewSizeOutOfBounds: Int, + totalSize: Int, + msSinceStartScroll: Long + ): Int { + // Clamp the scroll speed to prevent thefrom freaking out + // Adapted from NewPipe: https://github.com/TeamNewPipe/NewPipe + val standardSpeed = + super.interpolateOutOfBoundsScroll( + recyclerView, viewSize, viewSizeOutOfBounds, totalSize, msSinceStartScroll) + + val clampedAbsVelocity = + max( + MINIMUM_INITIAL_DRAG_VELOCITY, + min(abs(standardSpeed), MAXIMUM_INITIAL_DRAG_VELOCITY)) + + return clampedAbsVelocity * sign(viewSizeOutOfBounds.toDouble()).toInt() + } + final override fun onChildDraw( c: Canvas, recyclerView: RecyclerView, @@ -150,4 +175,9 @@ abstract class MaterialDragCallback : ItemTouchHelper.Callback() { /** The drawable of the [body] background that can be elevated. */ val background: Drawable } + + companion object { + const val MINIMUM_INITIAL_DRAG_VELOCITY = 10 + const val MAXIMUM_INITIAL_DRAG_VELOCITY = 25 + } }