package com.ody.photopicker.adapter; import android.support.v7.widget.RecyclerView; import java.util.ArrayList; import java.util.List; import com.ody.photopicker.entity.Photo; import com.ody.photopicker.entity.PhotoDirectory; import com.ody.photopicker.event.Selectable; public abstract class SelectableAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> implements Selectable { private static final String TAG = SelectableAdapter.class.getSimpleName(); protected List<PhotoDirectory> photoDirectories; protected List<String> selectedPhotos; public int currentDirectoryIndex = 0; public SelectableAdapter() { photoDirectories = new ArrayList<>(); selectedPhotos = new ArrayList<>(); } /** * Indicates if the item at position where is selected * * @param photo Photo of the item to check * @return true if the item is selected, false otherwise */ @Override public boolean isSelected(Photo photo) { return getSelectedPhotos().contains(photo.getPath()); } /** * Toggle the selection status of the item at a given position * * @param photo Photo of the item to toggle the selection status for */ @Override public void toggleSelection(Photo photo) { if (selectedPhotos.contains(photo.getPath())) { selectedPhotos.remove(photo.getPath()); } else { selectedPhotos.add(photo.getPath()); } } /** * Clear the selection status for all items */ @Override public void clearSelection() { selectedPhotos.clear(); } /** * Count the selected items * * @return Selected items count */ @Override public int getSelectedItemCount() { return selectedPhotos.size(); } public void setCurrentDirectoryIndex(int currentDirectoryIndex) { this.currentDirectoryIndex = currentDirectoryIndex; } public List<Photo> getCurrentPhotos() { return photoDirectories.get(currentDirectoryIndex).getPhotos(); } public List<String> getCurrentPhotoPaths() { List<String> currentPhotoPaths = new ArrayList<>(getCurrentPhotos().size()); for (Photo photo : getCurrentPhotos()) { currentPhotoPaths.add(photo.getPath()); } return currentPhotoPaths; } public List<String> getSelectedPhotos() { return selectedPhotos; } }