Skip to content

Commit

Permalink
Multi Selection options added...
Browse files Browse the repository at this point in the history
  • Loading branch information
Rajneesh Singh committed Jun 22, 2017
1 parent f1b26b8 commit 210f902
Show file tree
Hide file tree
Showing 4 changed files with 264 additions and 32 deletions.
15 changes: 15 additions & 0 deletions app/src/main/java/com/rks/musicx/base/BaseRecyclerViewAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public abstract class BaseRecyclerViewAdapter<TData, TViewHolder extends Recycle
private final LayoutInflater inflater;
public List<TData> data = new ArrayList<TData>();
private OnItemClickListener mOnItemClickListener;
private OnLongClickListener onLongClickListener;

public BaseRecyclerViewAdapter(@NonNull final Context context) {
this.context = context.getApplicationContext();
Expand Down Expand Up @@ -97,13 +98,27 @@ public void setOnItemClickListener(OnItemClickListener listener) {
mOnItemClickListener = listener;
}

public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
this.onLongClickListener = onLongClickListener;
}

public void triggerOnItemClickListener(int position, View view) {
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(position, view);
}
}

public void triggerOnLongClickListener(int pos) {
if (onLongClickListener != null) {
onLongClickListener.onLongItemClick(pos);
}
}

public interface OnItemClickListener {
void onItemClick(int position, View view);
}

public interface OnLongClickListener {
void onLongItemClick(int pos);
}
}
68 changes: 63 additions & 5 deletions app/src/main/java/com/rks/musicx/misc/utils/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -1089,9 +1089,7 @@ public static void setActionModeBackgroundColor(ActionMode actionMode, int color
* @param path
* @param context
*/
@SuppressLint("StringFormatInvalid")
public void DeleteTrack(int id, LoaderManager.LoaderCallbacks<List<Song>> songLoaders,
Fragment fragment, String name, String path, Context context) {
public void DeleteTrack(int id, LoaderManager.LoaderCallbacks<List<Song>> songLoaders, Fragment fragment, String name, String path, Context context) {
MaterialDialog.Builder dialog = new MaterialDialog.Builder(context);
dialog.title(name);
dialog.content(context.getString(R.string.delete_music, name));
Expand All @@ -1116,8 +1114,7 @@ public void onScanCompleted(String s, Uri uri) {
fragment.getLoaderManager().restartLoader(id, null, songLoaders);
}
});
fragment.getLoaderManager().restartLoader(id, null, songLoaders);
Toast.makeText(context, "Track deleted", Toast.LENGTH_SHORT).show();
Toast.makeText(context, "Song deleted", Toast.LENGTH_SHORT).show();
} else {
Log.e("-->", "file not Deleted :" + name);
fragment.getLoaderManager().restartLoader(id, null, songLoaders);
Expand All @@ -1137,6 +1134,67 @@ public void onScanCompleted(String s, Uri uri) {
dialog.show();
}

/**
* Multi file delete
*
* @param id
* @param songLoaders
* @param fragment
* @param songList
* @param context
*/
public void multiDeleteTrack(int id, LoaderManager.LoaderCallbacks<List<Song>> songLoaders, Fragment fragment, List<Song> songList, Context context) {
if (songList.size() == 0) {
return;
}
MaterialDialog.Builder dialog = new MaterialDialog.Builder(context);
dialog.title("Delete Tracks");
dialog.content(context.getString(R.string.delete_music));
dialog.positiveText(android.R.string.ok);
dialog.typeface(getFont(context), getFont(context));
dialog.onPositive(new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
boolean confirm = false;
for (Song song : songList) {
String path = song.getmSongPath();
if (path != null) {
File file = new File(path);
if (file.exists()) {
if (file.delete()) {
confirm = true;
MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, new String[]{"audio/*"}, new MediaScannerConnection.MediaScannerConnectionClient() {
@Override
public void onMediaScannerConnected() {

}

@Override
public void onScanCompleted(String s, Uri uri) {
fragment.getLoaderManager().restartLoader(id, null, songLoaders);
}
});
} else {
confirm = false;
}

}
}
}
if (confirm) {
Log.e("Helper", "files are Deleted");
Toast.makeText(context, "All Songs deleted", Toast.LENGTH_SHORT).show();
} else {
Log.e("Helper", "files are not Deleted");
fragment.getLoaderManager().restartLoader(id, null, songLoaders);
Toast.makeText(context, "Failed to delete song", Toast.LENGTH_SHORT).show();
}
}
});
dialog.negativeText(R.string.cancel);
dialog.show();
}

/**
* Song Menu Options
*
Expand Down
58 changes: 54 additions & 4 deletions app/src/main/java/com/rks/musicx/ui/adapters/SongListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.support.v4.content.ContextCompat;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down Expand Up @@ -57,11 +58,15 @@ public class SongListAdapter extends BaseRecyclerViewAdapter<Song, SongListAdapt
private int duration = 300;
private Interpolator interpolator = new LinearInterpolator();
private int lastpos = -1;
private SparseBooleanArray storeChecked = new SparseBooleanArray();
private boolean isMultiselect;
private int itemSelected;

public SongListAdapter(@NonNull Context context) {
super(context);
}


public void setLayoutId(int layoutId) {
layout = layoutId;
}
Expand Down Expand Up @@ -90,10 +95,12 @@ public void palettework(Palette palette) {
drawable.setTint(Color.WHITE);
holder.SongTitle.setTextColor(Color.WHITE);
holder.SongArtist.setTextColor(ContextCompat.getColor(getContext(), R.color.darkthemeTextColor));
holder.itemView.setBackgroundColor(storeChecked.get(position) ? ContextCompat.getColor(getContext(), R.color.translucent_white_8p) : Color.TRANSPARENT);
} else {
drawable.setTint(ContextCompat.getColor(getContext(), R.color.MaterialGrey));
holder.SongTitle.setTextColor(Color.BLACK);
holder.SongArtist.setTextColor(Color.DKGRAY);
holder.itemView.setBackgroundColor(storeChecked.get(position) ? ContextCompat.getColor(getContext(), R.color.cardview_dark_background) : Color.TRANSPARENT);
}
}
if (layout == R.layout.detail_list) {
Expand Down Expand Up @@ -145,7 +152,6 @@ public void palettework(Palette palette) {
}



public int getLayout() {
return layout;
}
Expand All @@ -167,7 +173,26 @@ public void setFilter(List<Song> songList) {
notifyDataSetChanged();
}

public class SongViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public void exitMultiselectMode() {
isMultiselect = false;
itemSelected = 0;
storeChecked.clear();
notifyDataSetChanged();
}

public List<Integer> getSelectedItems() {
List<Integer> items = new ArrayList<>(storeChecked.size());
for (int i = 0; i < storeChecked.size(); ++i) {
items.add(storeChecked.keyAt(i));
}
return items;
}

public boolean isMultiselect() {
return isMultiselect;
}

public class SongViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {

private TextView SongTitle, SongArtist, number;
private CircleImageView SongArtwork;
Expand All @@ -186,6 +211,8 @@ public SongViewHolder(View itemView) {
songView = (LinearLayout) itemView.findViewById(R.id.item_view);
itemView.setOnClickListener(this);
menu.setOnClickListener(this);
itemView.setLongClickable(true);
itemView.setOnLongClickListener(this);
}
if (layout == R.layout.detail_list) {
SongTitle = (TextView) itemView.findViewById(R.id.title);
Expand All @@ -212,8 +239,31 @@ public SongViewHolder(View itemView) {
@Override
public void onClick(View v) {
int position = getAdapterPosition();
triggerOnItemClickListener(position, v);
if (isMultiselect) {
boolean currentState = storeChecked.get(getAdapterPosition());
storeChecked.put(getAdapterPosition(), !currentState);
notifyItemChanged(getAdapterPosition());
if (currentState) {
triggerOnItemClickListener(--itemSelected, v);
storeChecked.delete(position);
} else {
triggerOnItemClickListener(++itemSelected, v);
}
} else {
triggerOnItemClickListener(position, v);
}
}
}

@Override
public boolean onLongClick(View view) {
if (!isMultiselect) {
isMultiselect = true;
storeChecked.put(getAdapterPosition(), true);
notifyItemChanged(getAdapterPosition());
triggerOnLongClickListener(++itemSelected);
return true;
}
return false;
}
}
}
Loading

0 comments on commit 210f902

Please sign in to comment.