|
|
@ -1,7 +1,6 @@ |
|
|
|
package awais.instagrabber.adapters; |
|
|
|
|
|
|
|
import androidx.annotation.NonNull; |
|
|
|
import androidx.recyclerview.widget.AsyncDifferConfig; |
|
|
|
import androidx.recyclerview.widget.DiffUtil; |
|
|
|
import androidx.recyclerview.widget.ListAdapter; |
|
|
|
import androidx.recyclerview.widget.RecyclerView; |
|
|
@ -9,47 +8,46 @@ import androidx.recyclerview.widget.RecyclerView; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
public abstract class MultiSelectListAdapter<T extends MultiSelectListAdapter.Selectable, VH extends RecyclerView.ViewHolder> extends ListAdapter<T, VH> { |
|
|
|
public abstract class MultiSelectListAdapter<T extends MultiSelectListAdapter.Selectable, VH extends RecyclerView.ViewHolder> extends |
|
|
|
ListAdapter<T, VH> { |
|
|
|
|
|
|
|
private boolean isSelecting = false; |
|
|
|
private OnItemClickListener<T> clickListener; |
|
|
|
private OnItemLongClickListener<T> longClickListener; |
|
|
|
private boolean isSelecting; |
|
|
|
private final OnItemClickListener<T> internalOnItemClickListener; |
|
|
|
private final OnItemLongClickListener<T> internalOnLongItemClickListener; |
|
|
|
|
|
|
|
private final List<T> selectedItems = new ArrayList<>(); |
|
|
|
protected final OnItemClickListener<T> internalOnItemClickListener = (item, position) -> { |
|
|
|
if (isSelecting) { |
|
|
|
toggleSelection(item, position); |
|
|
|
} |
|
|
|
if (clickListener == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
clickListener.onItemClick(item, position); |
|
|
|
}; |
|
|
|
protected final OnItemLongClickListener<T> internalOnLongItemClickListener = (item, position) -> { |
|
|
|
if (!isSelecting) { |
|
|
|
isSelecting = true; |
|
|
|
} |
|
|
|
toggleSelection(item, position); |
|
|
|
if (longClickListener == null) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
return longClickListener.onItemLongClick(item, position); |
|
|
|
}; |
|
|
|
|
|
|
|
protected MultiSelectListAdapter(@NonNull final DiffUtil.ItemCallback<T> diffCallback, |
|
|
|
final OnItemClickListener<T> clickListener, |
|
|
|
final OnItemLongClickListener<T> longClickListener) { |
|
|
|
super(diffCallback); |
|
|
|
this.clickListener = clickListener; |
|
|
|
this.longClickListener = longClickListener; |
|
|
|
internalOnItemClickListener = (item, position) -> { |
|
|
|
if (isSelecting) { |
|
|
|
toggleSelection(item, position); |
|
|
|
} |
|
|
|
if (clickListener == null) { |
|
|
|
return; |
|
|
|
} |
|
|
|
clickListener.onItemClick(item, position); |
|
|
|
}; |
|
|
|
internalOnLongItemClickListener = (item, position) -> { |
|
|
|
if (!isSelecting) { |
|
|
|
isSelecting = true; |
|
|
|
} |
|
|
|
toggleSelection(item, position); |
|
|
|
if (longClickListener == null) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
return longClickListener.onItemLongClick(item, position); |
|
|
|
}; |
|
|
|
} |
|
|
|
|
|
|
|
protected MultiSelectListAdapter(@NonNull final AsyncDifferConfig<T> config, |
|
|
|
final OnItemClickListener<T> clickListener, |
|
|
|
final OnItemLongClickListener<T> longClickListener) { |
|
|
|
super(config); |
|
|
|
this.clickListener = clickListener; |
|
|
|
this.longClickListener = longClickListener; |
|
|
|
public OnItemClickListener<T> getInternalOnItemClickListener() { |
|
|
|
return internalOnItemClickListener; |
|
|
|
} |
|
|
|
|
|
|
|
public OnItemLongClickListener<T> getInternalOnLongItemClickListener() { |
|
|
|
return internalOnLongItemClickListener; |
|
|
|
} |
|
|
|
|
|
|
|
private void toggleSelection(final T item, final int position) { |
|
|
|