package com.ttstd.dialer.view;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.ttstd.dialer.adapter.ContactInfoAdapter;


// 处理拖动和滑动事件
public class ItemTouchHelperCallback extends ItemTouchHelper.Callback {
    private final ContactInfoAdapter mAdapter;

    public ItemTouchHelperCallback(ContactInfoAdapter adapter) {
        mAdapter = adapter;
    }

    // 定义支持的拖动方向
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        // 允许上下拖动
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN ;
        // 允许左右滑动删除
        int swipeFlags = ItemTouchHelper.START;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    // 处理拖动交换位置
    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                          RecyclerView.ViewHolder target) {
        // 通知适配器项已移动
        mAdapter.onItemMove(viewHolder.getBindingAdapterPosition(), target.getBindingAdapterPosition());
        return true;
    }

    // 处理滑动删除
    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        // 通知适配器项已删除
        mAdapter.onItemDismiss(viewHolder.getBindingAdapterPosition());
    }

    // 当长按item时启用拖动
    @Override
    public boolean isLongPressDragEnabled() {
        return true;
    }

    // 启用滑动删除
    @Override
    public boolean isItemViewSwipeEnabled() {
        return false;
    }
}
