Switch around arguments in MultiSelectActionHandler

This commit is contained in:
ByteHamster 2022-05-08 11:08:34 +02:00
parent 03220d0774
commit 072e172996
5 changed files with 51 additions and 44 deletions

View File

@ -115,8 +115,8 @@ public class CompletedDownloadsFragment extends Fragment
}
});
speedDialView.setOnActionSelectedListener(actionItem -> {
new EpisodeMultiSelectActionHandler(((MainActivity) getActivity()), adapter.getSelectedItems())
.handleAction(actionItem.getId());
new EpisodeMultiSelectActionHandler(((MainActivity) getActivity()), actionItem.getId())
.handleAction(adapter.getSelectedItems());
adapter.endSelectMode();
return true;
});

View File

@ -246,8 +246,8 @@ public abstract class EpisodesListFragment extends Fragment implements EpisodeIt
}
});
speedDialView.setOnActionSelectedListener(actionItem -> {
new EpisodeMultiSelectActionHandler(((MainActivity) getActivity()), listAdapter.getSelectedItems())
.handleAction(actionItem.getId());
new EpisodeMultiSelectActionHandler(((MainActivity) getActivity()), actionItem.getId())
.handleAction(listAdapter.getSelectedItems());
listAdapter.endSelectMode();
return true;
});

View File

@ -197,8 +197,8 @@ public class FeedItemlistFragment extends Fragment implements AdapterView.OnItem
}
});
speedDialBinding.fabSD.setOnActionSelectedListener(actionItem -> {
new EpisodeMultiSelectActionHandler(((MainActivity) getActivity()), adapter.getSelectedItems())
.handleAction(actionItem.getId());
new EpisodeMultiSelectActionHandler(((MainActivity) getActivity()), actionItem.getId())
.handleAction(adapter.getSelectedItems());
adapter.endSelectMode();
return true;
});

View File

@ -505,8 +505,8 @@ public class QueueFragment extends Fragment implements Toolbar.OnMenuItemClickLi
}
});
speedDialView.setOnActionSelectedListener(actionItem -> {
new EpisodeMultiSelectActionHandler(((MainActivity) getActivity()), recyclerAdapter.getSelectedItems())
.handleAction(actionItem.getId());
new EpisodeMultiSelectActionHandler(((MainActivity) getActivity()), actionItem.getId())
.handleAction(recyclerAdapter.getSelectedItems());
recyclerAdapter.endSelectMode();
return true;
});

View File

@ -21,35 +21,37 @@ import de.danoeh.antennapod.model.feed.FeedItem;
public class EpisodeMultiSelectActionHandler {
private static final String TAG = "EpisodeSelectHandler";
private final MainActivity activity;
private final List<FeedItem> selectedItems;
private final int actionId;
private int totalNumItems = 0;
private Snackbar snackbar = null;
public EpisodeMultiSelectActionHandler(MainActivity activity, List<FeedItem> selectedItems) {
public EpisodeMultiSelectActionHandler(MainActivity activity, int actionId) {
this.activity = activity;
this.selectedItems = selectedItems;
this.actionId = actionId;
}
public void handleAction(int id) {
if (id == R.id.add_to_queue_batch) {
queueChecked();
} else if (id == R.id.remove_from_queue_batch) {
removeFromQueueChecked();
} else if (id == R.id.mark_read_batch) {
markedCheckedPlayed();
} else if (id == R.id.mark_unread_batch) {
markedCheckedUnplayed();
} else if (id == R.id.download_batch) {
downloadChecked();
} else if (id == R.id.delete_batch) {
deleteChecked();
public void handleAction(List<FeedItem> items) {
if (actionId == R.id.add_to_queue_batch) {
queueChecked(items);
} else if (actionId == R.id.remove_from_queue_batch) {
removeFromQueueChecked(items);
} else if (actionId == R.id.mark_read_batch) {
markedCheckedPlayed(items);
} else if (actionId == R.id.mark_unread_batch) {
markedCheckedUnplayed(items);
} else if (actionId == R.id.download_batch) {
downloadChecked(items);
} else if (actionId == R.id.delete_batch) {
deleteChecked(items);
} else {
Log.e(TAG, "Unrecognized speed dial action item. Do nothing. id=" + id);
Log.e(TAG, "Unrecognized speed dial action item. Do nothing. id=" + actionId);
}
}
private void queueChecked() {
private void queueChecked(List<FeedItem> items) {
// Check if an episode actually contains any media files before adding it to queue
LongList toQueue = new LongList(selectedItems.size());
for (FeedItem episode : selectedItems) {
LongList toQueue = new LongList(items.size());
for (FeedItem episode : items) {
if (episode.hasMedia()) {
toQueue.add(episode.getId());
}
@ -58,28 +60,28 @@ public class EpisodeMultiSelectActionHandler {
showMessage(R.plurals.added_to_queue_batch_label, toQueue.size());
}
private void removeFromQueueChecked() {
long[] checkedIds = getSelectedIds();
private void removeFromQueueChecked(List<FeedItem> items) {
long[] checkedIds = getSelectedIds(items);
DBWriter.removeQueueItem(activity, true, checkedIds);
showMessage(R.plurals.removed_from_queue_batch_label, checkedIds.length);
}
private void markedCheckedPlayed() {
long[] checkedIds = getSelectedIds();
private void markedCheckedPlayed(List<FeedItem> items) {
long[] checkedIds = getSelectedIds(items);
DBWriter.markItemPlayed(FeedItem.PLAYED, checkedIds);
showMessage(R.plurals.marked_read_batch_label, checkedIds.length);
}
private void markedCheckedUnplayed() {
long[] checkedIds = getSelectedIds();
private void markedCheckedUnplayed(List<FeedItem> items) {
long[] checkedIds = getSelectedIds(items);
DBWriter.markItemPlayed(FeedItem.UNPLAYED, checkedIds);
showMessage(R.plurals.marked_unread_batch_label, checkedIds.length);
}
private void downloadChecked() {
private void downloadChecked(List<FeedItem> items) {
// download the check episodes in the same order as they are currently displayed
List<DownloadRequest> requests = new ArrayList<>();
for (FeedItem episode : selectedItems) {
for (FeedItem episode : items) {
if (episode.hasMedia() && !episode.getFeed().isLocalFeed()) {
requests.add(DownloadRequestCreator.create(episode.getMedia()).build());
}
@ -88,9 +90,9 @@ public class EpisodeMultiSelectActionHandler {
showMessage(R.plurals.downloading_batch_label, requests.size());
}
private void deleteChecked() {
private void deleteChecked(List<FeedItem> items) {
int countHasMedia = 0;
for (FeedItem feedItem : selectedItems) {
for (FeedItem feedItem : items) {
if (feedItem.hasMedia() && feedItem.getMedia().isDownloaded()) {
countHasMedia++;
DBWriter.deleteFeedMediaOfItem(activity, feedItem.getMedia().getId());
@ -100,14 +102,19 @@ public class EpisodeMultiSelectActionHandler {
}
private void showMessage(@PluralsRes int msgId, int numItems) {
activity.showSnackbarAbovePlayer(activity.getResources()
.getQuantityString(msgId, numItems, numItems), Snackbar.LENGTH_LONG);
totalNumItems += numItems;
String text = activity.getResources().getQuantityString(msgId, totalNumItems, totalNumItems);
if (snackbar != null) {
snackbar.setText(text);
} else {
snackbar = activity.showSnackbarAbovePlayer(text, Snackbar.LENGTH_LONG);
}
}
private long[] getSelectedIds() {
long[] checkedIds = new long[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); ++i) {
checkedIds[i] = selectedItems.get(i).getId();
private long[] getSelectedIds(List<FeedItem> items) {
long[] checkedIds = new long[items.size()];
for (int i = 0; i < items.size(); ++i) {
checkedIds[i] = items.get(i).getId();
}
return checkedIds;
}