chore: move shared touch helper

This commit is contained in:
Andries Smit 2022-03-28 11:44:24 +02:00
parent ea995f705a
commit 0c4cf71ef8
3 changed files with 19 additions and 15 deletions

View file

@ -1,4 +1,4 @@
package info.nightscout.androidaps.plugins.general.automation.dragHelpers package info.nightscout.androidaps.utils.dragHelpers
interface ItemTouchHelperAdapter { interface ItemTouchHelperAdapter {

View file

@ -1,4 +1,4 @@
package info.nightscout.androidaps.plugins.general.automation.dragHelpers package info.nightscout.androidaps.utils.dragHelpers
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView

View file

@ -1,21 +1,25 @@
package info.nightscout.androidaps.plugins.general.automation.dragHelpers package info.nightscout.androidaps.utils.dragHelpers
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.ItemTouchHelper.ACTION_STATE_DRAG
import androidx.recyclerview.widget.ItemTouchHelper.DOWN
import androidx.recyclerview.widget.ItemTouchHelper.END
import androidx.recyclerview.widget.ItemTouchHelper.START
import androidx.recyclerview.widget.ItemTouchHelper.UP
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import info.nightscout.androidaps.plugins.general.automation.AutomationFragment
class SimpleItemTouchHelperCallback : ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN or ItemTouchHelper.START or ItemTouchHelper.END, 0) { const val ALPHA_FULL = 1f
const val ALPHA_DRAGGING = 0.5f
override fun isLongPressDragEnabled(): Boolean { class SimpleItemTouchHelperCallback : ItemTouchHelper.SimpleCallback(UP or DOWN or START or END, 0) {
return false
} override fun isLongPressDragEnabled() = false
override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean { override fun onMove(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder, target: RecyclerView.ViewHolder): Boolean {
val adapter = recyclerView.adapter as AutomationFragment.EventListAdapter val adapter = recyclerView.adapter as ItemTouchHelperAdapter
val from = viewHolder.layoutPosition val from = viewHolder.layoutPosition
val to = target.layoutPosition val to = target.layoutPosition
adapter.onItemMove(from, to) adapter.onItemMove(from, to)
adapter.notifyItemMoved(from, to)
return true return true
} }
@ -24,14 +28,14 @@ class SimpleItemTouchHelperCallback : ItemTouchHelper.SimpleCallback(ItemTouchHe
override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) { override fun onSelectedChanged(viewHolder: RecyclerView.ViewHolder?, actionState: Int) {
super.onSelectedChanged(viewHolder, actionState) super.onSelectedChanged(viewHolder, actionState)
if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) { if (actionState == ACTION_STATE_DRAG) {
viewHolder?.itemView?.alpha = 0.5f viewHolder?.itemView?.alpha = ALPHA_DRAGGING
} }
} }
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
super.clearView(recyclerView, viewHolder) super.clearView(recyclerView, viewHolder)
viewHolder.itemView.alpha = 1.0f viewHolder.itemView.alpha = ALPHA_FULL
(recyclerView.adapter as AutomationFragment.EventListAdapter).onDrop() (recyclerView.adapter as ItemTouchHelperAdapter).onDrop()
} }
} }