2019-07-22 15:26:37 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Hemanth Savarala.
|
|
|
|
*
|
|
|
|
* Licensed under the GNU General Public License v3
|
|
|
|
*
|
|
|
|
* This is free software: you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package code.name.monkey.retromusic.util;
|
|
|
|
|
|
|
|
import android.graphics.Canvas;
|
2021-09-08 18:30:20 +00:00
|
|
|
|
2019-10-29 14:49:28 +00:00
|
|
|
import androidx.annotation.NonNull;
|
2019-07-22 15:26:37 +00:00
|
|
|
import androidx.recyclerview.widget.ItemTouchHelper;
|
2020-02-26 16:49:24 +00:00
|
|
|
import androidx.recyclerview.widget.RecyclerView;
|
2019-07-22 15:26:37 +00:00
|
|
|
|
|
|
|
public class SwipeAndDragHelper extends ItemTouchHelper.Callback {
|
|
|
|
|
2021-10-11 06:17:55 +00:00
|
|
|
private final ActionCompletionContract contract;
|
2019-07-22 15:26:37 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public SwipeAndDragHelper(@NonNull ActionCompletionContract contract) {
|
|
|
|
this.contract = contract;
|
|
|
|
}
|
2019-07-22 15:26:37 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
@Override
|
|
|
|
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
|
|
|
|
int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
|
|
|
|
return makeMovementFlags(dragFlags, 0);
|
|
|
|
}
|
2019-07-22 15:26:37 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
@Override
|
|
|
|
public boolean onMove(
|
|
|
|
RecyclerView recyclerView,
|
|
|
|
RecyclerView.ViewHolder viewHolder,
|
|
|
|
RecyclerView.ViewHolder target) {
|
|
|
|
contract.onViewMoved(viewHolder.getLayoutPosition(), target.getLayoutPosition());
|
|
|
|
return true;
|
|
|
|
}
|
2019-07-22 15:26:37 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
@Override
|
|
|
|
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {}
|
2019-07-22 15:26:37 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
@Override
|
|
|
|
public boolean isLongPressDragEnabled() {
|
|
|
|
return false;
|
|
|
|
}
|
2019-07-22 15:26:37 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
@Override
|
|
|
|
public void onChildDraw(
|
|
|
|
Canvas c,
|
|
|
|
RecyclerView recyclerView,
|
|
|
|
RecyclerView.ViewHolder viewHolder,
|
|
|
|
float dX,
|
|
|
|
float dY,
|
|
|
|
int actionState,
|
|
|
|
boolean isCurrentlyActive) {
|
|
|
|
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
|
|
|
|
float alpha = 1 - (Math.abs(dX) / recyclerView.getWidth());
|
|
|
|
viewHolder.itemView.setAlpha(alpha);
|
2019-07-22 15:26:37 +00:00
|
|
|
}
|
2020-10-06 08:46:04 +00:00
|
|
|
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
|
|
|
|
}
|
2019-07-22 15:26:37 +00:00
|
|
|
|
2020-10-06 08:46:04 +00:00
|
|
|
public interface ActionCompletionContract {
|
|
|
|
void onViewMoved(int oldPosition, int newPosition);
|
|
|
|
}
|
2019-07-22 15:26:37 +00:00
|
|
|
}
|