|
|
|
@ -1,12 +1,22 @@
|
|
|
|
|
package com.todoroo.astrid.core
|
|
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
import android.graphics.Canvas
|
|
|
|
|
import android.graphics.drawable.ColorDrawable
|
|
|
|
|
import androidx.core.content.ContextCompat
|
|
|
|
|
import androidx.recyclerview.widget.ItemTouchHelper
|
|
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
|
import org.tasks.Callback
|
|
|
|
|
import org.tasks.Callback2
|
|
|
|
|
import org.tasks.R
|
|
|
|
|
|
|
|
|
|
class CustomFilterItemTouchHelper(private val onMove: Callback2<Int, Int>, private val onClear: Runnable) : ItemTouchHelper.Callback() {
|
|
|
|
|
class CustomFilterItemTouchHelper(
|
|
|
|
|
private val context: Context,
|
|
|
|
|
private val onMove: Callback2<Int, Int>,
|
|
|
|
|
private val onDelete: Callback<Int>,
|
|
|
|
|
private val onClear: Runnable) : ItemTouchHelper.Callback() {
|
|
|
|
|
override fun getMovementFlags(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder): Int {
|
|
|
|
|
return if (viewHolder.adapterPosition > 0) makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, 0) else 0
|
|
|
|
|
return if (viewHolder.adapterPosition > 0) makeMovementFlags(ItemTouchHelper.UP or ItemTouchHelper.DOWN, ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT) else 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
|
|
|
|
@ -27,6 +37,27 @@ class CustomFilterItemTouchHelper(private val onMove: Callback2<Int, Int>, priva
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun getSwipeEscapeVelocity(defaultValue: Float): Float {
|
|
|
|
|
return defaultValue * 3
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun getSwipeVelocityThreshold(defaultValue: Float): Float {
|
|
|
|
|
return .6f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onChildDraw(c: Canvas, recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, dX: Float, dY: Float, actionState: Int, isCurrentlyActive: Boolean) {
|
|
|
|
|
val itemView = viewHolder.itemView
|
|
|
|
|
val background = ColorDrawable(ContextCompat.getColor(context, R.color.overdue))
|
|
|
|
|
if (dX > 0) {
|
|
|
|
|
background.setBounds(0, itemView.top, itemView.left + dX.toInt(), itemView.bottom)
|
|
|
|
|
} else if (dX < 0) {
|
|
|
|
|
background.setBounds(itemView.right + dX.toInt(), itemView.top, itemView.right, itemView.bottom)
|
|
|
|
|
}
|
|
|
|
|
background.draw(c)
|
|
|
|
|
|
|
|
|
|
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
|
|
|
|
|
super.clearView(recyclerView, viewHolder)
|
|
|
|
|
|
|
|
|
@ -35,5 +66,7 @@ class CustomFilterItemTouchHelper(private val onMove: Callback2<Int, Int>, priva
|
|
|
|
|
onClear.run()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {}
|
|
|
|
|
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
|
|
|
|
|
onDelete.call(viewHolder.adapterPosition)
|
|
|
|
|
}
|
|
|
|
|
}
|