Remove ugly if-else-cascade in

Common actions and labels are now in a unique enum: StreamDialogEntry
If an action is not common to every long-press menu (e.g. delete) a custom action has to be provided using e.g. delete.setAction(...)
This commit is contained in:
Stypox 2019-07-25 00:44:12 +02:00
parent 6c24e1cc39
commit bd18d38e5c
4 changed files with 159 additions and 151 deletions

View file

@ -37,6 +37,7 @@ import org.schabi.newpipe.settings.SettingsActivity;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.OnClickGesture;
import org.schabi.newpipe.util.ShareUtils;
import org.schabi.newpipe.util.StreamDialogEntry;
import org.schabi.newpipe.util.ThemeHelper;
import java.util.ArrayList;
@ -363,63 +364,31 @@ public class StatisticsPlaylistFragment
final Context context = getContext();
final Activity activity = getActivity();
if (context == null || context.getResources() == null || activity == null) return;
final StreamInfoItem infoItem = item.toStreamInfoItem();
boolean isAudioStream = (infoItem.getStreamType() == StreamType.AUDIO_STREAM);
final String[] commands;
if (isAudioStream) {
commands = new String[]{
context.getResources().getString(R.string.enqueue_on_background),
context.getResources().getString(R.string.start_here_on_background),
context.getResources().getString(R.string.delete),
context.getResources().getString(R.string.append_playlist),
context.getResources().getString(R.string.share)
};
if (infoItem.getStreamType() == StreamType.AUDIO_STREAM) {
StreamDialogEntry.setEnabledEntries(
StreamDialogEntry.enqueue_on_background,
StreamDialogEntry.start_here_on_background,
StreamDialogEntry.delete,
StreamDialogEntry.append_playlist,
StreamDialogEntry.share);
} else {
commands = new String[]{
context.getResources().getString(R.string.enqueue_on_background),
context.getResources().getString(R.string.enqueue_on_popup),
context.getResources().getString(R.string.start_here_on_background),
context.getResources().getString(R.string.start_here_on_popup),
context.getResources().getString(R.string.delete),
context.getResources().getString(R.string.append_playlist),
context.getResources().getString(R.string.share)
};
StreamDialogEntry.setEnabledEntries(
StreamDialogEntry.enqueue_on_background,
StreamDialogEntry.enqueue_on_popup,
StreamDialogEntry.start_here_on_background,
StreamDialogEntry.start_here_on_popup,
StreamDialogEntry.delete,
StreamDialogEntry.append_playlist,
StreamDialogEntry.share);
}
StreamDialogEntry.delete.setAction((fragment, infoItemDuplicate) ->
deleteEntry(Math.max(itemListAdapter.getItemsList().indexOf(item), 0)));
final DialogInterface.OnClickListener actions = (dialogInterface, i) -> {
final int index = Math.max(itemListAdapter.getItemsList().indexOf(item), 0);
if (i == 0) {
NavigationHelper.enqueueOnBackgroundPlayer(context, new SinglePlayQueue(infoItem), false);
} else if (i == (isAudioStream ? -1 : 1)) { // disabled with audio streams
NavigationHelper.enqueueOnPopupPlayer(context, new SinglePlayQueue(infoItem), false);
} else if (i == (isAudioStream ? 1 : 2)) {
NavigationHelper.playOnBackgroundPlayer(context, new SinglePlayQueue(infoItem), true);
} else if (i == (isAudioStream ? -1 : 3)) { // disabled with audio streams
NavigationHelper.playOnPopupPlayer(context, new SinglePlayQueue(infoItem), true);
} else if (i == (isAudioStream ? 2 : 4)) {
deleteEntry(index);
} else if (i == (isAudioStream ? 3 : 5)) {
if (getFragmentManager() != null) {
PlaylistAppendDialog.fromStreamInfoItems(Collections.singletonList(infoItem))
.show(getFragmentManager(), TAG);
}
} else if (i == (isAudioStream ? 4 : 6)) {
ShareUtils.shareUrl(context, infoItem.getName(), infoItem.getUrl());
}
};
new InfoItemDialog(activity, infoItem, commands, actions).show();
new InfoItemDialog(activity, infoItem, StreamDialogEntry.getCommands(context), (dialog, which) ->
StreamDialogEntry.clickOn(which, this, infoItem)).show();
}
private void deleteEntry(final int index) {

View file

@ -37,6 +37,7 @@ import org.schabi.newpipe.util.Localization;
import org.schabi.newpipe.util.NavigationHelper;
import org.schabi.newpipe.util.OnClickGesture;
import org.schabi.newpipe.util.ShareUtils;
import org.schabi.newpipe.util.StreamDialogEntry;
import java.util.ArrayList;
import java.util.Collections;
@ -517,68 +518,35 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
final Context context = getContext();
final Activity activity = getActivity();
if (context == null || context.getResources() == null || activity == null) return;
final StreamInfoItem infoItem = item.toStreamInfoItem();
boolean isAudioStream = (infoItem.getStreamType() == StreamType.AUDIO_STREAM);
final String[] commands;
if (isAudioStream) {
commands = new String[]{
context.getResources().getString(R.string.enqueue_on_background),
context.getResources().getString(R.string.start_here_on_background),
context.getResources().getString(R.string.set_as_playlist_thumbnail),
context.getResources().getString(R.string.delete),
context.getResources().getString(R.string.append_playlist),
context.getResources().getString(R.string.share),
};
if (infoItem.getStreamType() == StreamType.AUDIO_STREAM) {
StreamDialogEntry.setEnabledEntries(
StreamDialogEntry.enqueue_on_background,
StreamDialogEntry.start_here_on_background,
StreamDialogEntry.set_as_playlist_thumbnail,
StreamDialogEntry.delete,
StreamDialogEntry.append_playlist,
StreamDialogEntry.share);
} else {
commands = new String[]{
context.getResources().getString(R.string.enqueue_on_background),
context.getResources().getString(R.string.enqueue_on_popup),
context.getResources().getString(R.string.start_here_on_background),
context.getResources().getString(R.string.start_here_on_popup),
context.getResources().getString(R.string.set_as_playlist_thumbnail),
context.getResources().getString(R.string.delete),
context.getResources().getString(R.string.append_playlist),
context.getResources().getString(R.string.share),
};
StreamDialogEntry.setEnabledEntries(
StreamDialogEntry.enqueue_on_background,
StreamDialogEntry.enqueue_on_popup,
StreamDialogEntry.start_here_on_background,
StreamDialogEntry.start_here_on_popup,
StreamDialogEntry.set_as_playlist_thumbnail,
StreamDialogEntry.delete,
StreamDialogEntry.append_playlist,
StreamDialogEntry.share);
}
StreamDialogEntry.set_as_playlist_thumbnail.setAction(
(fragment, infoItemDuplicate) -> changeThumbnailUrl(item.thumbnailUrl));
StreamDialogEntry.delete.setAction(
(fragment, infoItemDuplicate) -> deleteItem(item));
final DialogInterface.OnClickListener actions = (dialogInterface, i) -> {
final int index = Math.max(itemListAdapter.getItemsList().indexOf(item), 0);
if (i == 0) {
NavigationHelper.enqueueOnBackgroundPlayer(context, new SinglePlayQueue(infoItem), false);
} else if (i == (isAudioStream ? -1 : 1)) { // disabled with audio streams
NavigationHelper.enqueueOnPopupPlayer(context, new SinglePlayQueue(infoItem), false);
} else if (i == (isAudioStream ? 1 : 2)) {
NavigationHelper.playOnBackgroundPlayer(context, getPlayQueue(index), true);
} else if (i == (isAudioStream ? -1 : 3)) { // disabled with audio streams
NavigationHelper.playOnPopupPlayer(context, getPlayQueue(index), true);
} else if (i == (isAudioStream ? 2 : 4)) {
changeThumbnailUrl(item.thumbnailUrl);
} else if (i == (isAudioStream ? 3 : 5)) {
deleteItem(item);
} else if (i == (isAudioStream ? 4 : 6)) {
if (getFragmentManager() != null) {
PlaylistAppendDialog.fromStreamInfoItems(Collections.singletonList(infoItem))
.show(getFragmentManager(), TAG);
}
} else if (i == (isAudioStream ? 5 : 7)) {
ShareUtils.shareUrl(context, infoItem.getName(), infoItem.getUrl());
}
};
new InfoItemDialog(activity, infoItem, commands, actions).show();
new InfoItemDialog(activity, infoItem, StreamDialogEntry.getCommands(context), (dialog, which) ->
StreamDialogEntry.clickOn(which, this, infoItem)).show();
}
private void setInitialData(long playlistId, String name) {