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:
parent
3aeba7ca8a
commit
759e9846ad
|
@ -9,6 +9,7 @@ import android.content.res.Resources;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.preference.PreferenceManager;
|
import android.preference.PreferenceManager;
|
||||||
import android.support.annotation.NonNull;
|
import android.support.annotation.NonNull;
|
||||||
|
import android.support.v4.app.Fragment;
|
||||||
import android.support.v7.app.ActionBar;
|
import android.support.v7.app.ActionBar;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.support.v7.widget.GridLayoutManager;
|
import android.support.v7.widget.GridLayoutManager;
|
||||||
|
@ -37,10 +38,14 @@ import org.schabi.newpipe.util.NavigationHelper;
|
||||||
import org.schabi.newpipe.util.OnClickGesture;
|
import org.schabi.newpipe.util.OnClickGesture;
|
||||||
import org.schabi.newpipe.util.ShareUtils;
|
import org.schabi.newpipe.util.ShareUtils;
|
||||||
import org.schabi.newpipe.util.StateSaver;
|
import org.schabi.newpipe.util.StateSaver;
|
||||||
|
import org.schabi.newpipe.util.StreamDialogEntry;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
|
import java.util.function.DoubleBinaryOperator;
|
||||||
|
|
||||||
import static org.schabi.newpipe.util.AnimationUtils.animateView;
|
import static org.schabi.newpipe.util.AnimationUtils.animateView;
|
||||||
|
|
||||||
|
@ -259,58 +264,32 @@ public abstract class BaseListFragment<I, N> extends BaseStateFragment<I> implem
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
protected void showStreamDialog(final StreamInfoItem item) {
|
protected void showStreamDialog(final StreamInfoItem item) {
|
||||||
final Context context = getContext();
|
final Context context = getContext();
|
||||||
final Activity activity = getActivity();
|
final Activity activity = getActivity();
|
||||||
if (context == null || context.getResources() == null || activity == null) return;
|
if (context == null || context.getResources() == null || activity == null) return;
|
||||||
|
|
||||||
boolean isAudioStream = (item.getStreamType() == StreamType.AUDIO_STREAM);
|
if (item.getStreamType() == StreamType.AUDIO_STREAM) {
|
||||||
|
StreamDialogEntry.setEnabledEntries(
|
||||||
final String[] commands;
|
StreamDialogEntry.enqueue_on_background,
|
||||||
if (isAudioStream) {
|
StreamDialogEntry.start_here_on_background,
|
||||||
commands = new String[]{
|
StreamDialogEntry.append_playlist,
|
||||||
context.getResources().getString(R.string.enqueue_on_background),
|
StreamDialogEntry.share);
|
||||||
context.getResources().getString(R.string.start_here_on_background),
|
|
||||||
context.getResources().getString(R.string.append_playlist),
|
|
||||||
context.getResources().getString(R.string.share)
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
commands = new String[]{
|
StreamDialogEntry.setEnabledEntries(
|
||||||
context.getResources().getString(R.string.enqueue_on_background),
|
StreamDialogEntry.enqueue_on_background,
|
||||||
context.getResources().getString(R.string.enqueue_on_popup),
|
StreamDialogEntry.enqueue_on_popup,
|
||||||
context.getResources().getString(R.string.start_here_on_background),
|
StreamDialogEntry.start_here_on_background,
|
||||||
context.getResources().getString(R.string.start_here_on_popup),
|
StreamDialogEntry.start_here_on_popup,
|
||||||
context.getResources().getString(R.string.append_playlist),
|
StreamDialogEntry.append_playlist,
|
||||||
context.getResources().getString(R.string.share)
|
StreamDialogEntry.share);
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
final DialogInterface.OnClickListener actions = (dialogInterface, i) -> {
|
new InfoItemDialog(activity, item, StreamDialogEntry.getCommands(context), (dialog, which) ->
|
||||||
if (i == 0) {
|
StreamDialogEntry.clickOn(which, this, item)).show();
|
||||||
NavigationHelper.enqueueOnBackgroundPlayer(context, new SinglePlayQueue(item), false);
|
|
||||||
|
|
||||||
} else if (i == (isAudioStream ? -1 : 1)) { // disabled with audio streams
|
|
||||||
NavigationHelper.enqueueOnPopupPlayer(context, new SinglePlayQueue(item), false);
|
|
||||||
|
|
||||||
} else if (i == (isAudioStream ? 1 : 2)) {
|
|
||||||
NavigationHelper.playOnBackgroundPlayer(context, new SinglePlayQueue(item), true);
|
|
||||||
|
|
||||||
} else if (i == (isAudioStream ? -1 : 3)) { // disabled with audio streams
|
|
||||||
NavigationHelper.playOnPopupPlayer(context, new SinglePlayQueue(item), true);
|
|
||||||
|
|
||||||
} else if (i == (isAudioStream ? 2 : 4)) {
|
|
||||||
if (getFragmentManager() != null) {
|
|
||||||
PlaylistAppendDialog.fromStreamInfoItems(Collections.singletonList(item))
|
|
||||||
.show(getFragmentManager(), TAG);
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (i == (isAudioStream ? 3 : 5)) {
|
|
||||||
ShareUtils.shareUrl(context, item.getName(), item.getUrl());
|
|
||||||
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
new InfoItemDialog(activity, item, commands, actions).show();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*//////////////////////////////////////////////////////////////////////////
|
/*//////////////////////////////////////////////////////////////////////////
|
||||||
|
|
|
@ -37,6 +37,7 @@ import org.schabi.newpipe.settings.SettingsActivity;
|
||||||
import org.schabi.newpipe.util.NavigationHelper;
|
import org.schabi.newpipe.util.NavigationHelper;
|
||||||
import org.schabi.newpipe.util.OnClickGesture;
|
import org.schabi.newpipe.util.OnClickGesture;
|
||||||
import org.schabi.newpipe.util.ShareUtils;
|
import org.schabi.newpipe.util.ShareUtils;
|
||||||
|
import org.schabi.newpipe.util.StreamDialogEntry;
|
||||||
import org.schabi.newpipe.util.ThemeHelper;
|
import org.schabi.newpipe.util.ThemeHelper;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -363,63 +364,31 @@ public class StatisticsPlaylistFragment
|
||||||
final Context context = getContext();
|
final Context context = getContext();
|
||||||
final Activity activity = getActivity();
|
final Activity activity = getActivity();
|
||||||
if (context == null || context.getResources() == null || activity == null) return;
|
if (context == null || context.getResources() == null || activity == null) return;
|
||||||
|
|
||||||
final StreamInfoItem infoItem = item.toStreamInfoItem();
|
final StreamInfoItem infoItem = item.toStreamInfoItem();
|
||||||
boolean isAudioStream = (infoItem.getStreamType() == StreamType.AUDIO_STREAM);
|
|
||||||
|
|
||||||
final String[] commands;
|
if (infoItem.getStreamType() == StreamType.AUDIO_STREAM) {
|
||||||
if (isAudioStream) {
|
StreamDialogEntry.setEnabledEntries(
|
||||||
commands = new String[]{
|
StreamDialogEntry.enqueue_on_background,
|
||||||
context.getResources().getString(R.string.enqueue_on_background),
|
StreamDialogEntry.start_here_on_background,
|
||||||
context.getResources().getString(R.string.start_here_on_background),
|
StreamDialogEntry.delete,
|
||||||
context.getResources().getString(R.string.delete),
|
StreamDialogEntry.append_playlist,
|
||||||
context.getResources().getString(R.string.append_playlist),
|
StreamDialogEntry.share);
|
||||||
context.getResources().getString(R.string.share)
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
commands = new String[]{
|
StreamDialogEntry.setEnabledEntries(
|
||||||
context.getResources().getString(R.string.enqueue_on_background),
|
StreamDialogEntry.enqueue_on_background,
|
||||||
context.getResources().getString(R.string.enqueue_on_popup),
|
StreamDialogEntry.enqueue_on_popup,
|
||||||
context.getResources().getString(R.string.start_here_on_background),
|
StreamDialogEntry.start_here_on_background,
|
||||||
context.getResources().getString(R.string.start_here_on_popup),
|
StreamDialogEntry.start_here_on_popup,
|
||||||
context.getResources().getString(R.string.delete),
|
StreamDialogEntry.delete,
|
||||||
context.getResources().getString(R.string.append_playlist),
|
StreamDialogEntry.append_playlist,
|
||||||
context.getResources().getString(R.string.share)
|
StreamDialogEntry.share);
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
StreamDialogEntry.delete.setAction((fragment, infoItemDuplicate) ->
|
||||||
|
deleteEntry(Math.max(itemListAdapter.getItemsList().indexOf(item), 0)));
|
||||||
|
|
||||||
final DialogInterface.OnClickListener actions = (dialogInterface, i) -> {
|
new InfoItemDialog(activity, infoItem, StreamDialogEntry.getCommands(context), (dialog, which) ->
|
||||||
final int index = Math.max(itemListAdapter.getItemsList().indexOf(item), 0);
|
StreamDialogEntry.clickOn(which, this, infoItem)).show();
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void deleteEntry(final int index) {
|
private void deleteEntry(final int index) {
|
||||||
|
|
|
@ -37,6 +37,7 @@ import org.schabi.newpipe.util.Localization;
|
||||||
import org.schabi.newpipe.util.NavigationHelper;
|
import org.schabi.newpipe.util.NavigationHelper;
|
||||||
import org.schabi.newpipe.util.OnClickGesture;
|
import org.schabi.newpipe.util.OnClickGesture;
|
||||||
import org.schabi.newpipe.util.ShareUtils;
|
import org.schabi.newpipe.util.ShareUtils;
|
||||||
|
import org.schabi.newpipe.util.StreamDialogEntry;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
@ -517,68 +518,35 @@ public class LocalPlaylistFragment extends BaseLocalListFragment<List<PlaylistSt
|
||||||
final Context context = getContext();
|
final Context context = getContext();
|
||||||
final Activity activity = getActivity();
|
final Activity activity = getActivity();
|
||||||
if (context == null || context.getResources() == null || activity == null) return;
|
if (context == null || context.getResources() == null || activity == null) return;
|
||||||
|
|
||||||
final StreamInfoItem infoItem = item.toStreamInfoItem();
|
final StreamInfoItem infoItem = item.toStreamInfoItem();
|
||||||
boolean isAudioStream = (infoItem.getStreamType() == StreamType.AUDIO_STREAM);
|
|
||||||
|
|
||||||
final String[] commands;
|
if (infoItem.getStreamType() == StreamType.AUDIO_STREAM) {
|
||||||
if (isAudioStream) {
|
StreamDialogEntry.setEnabledEntries(
|
||||||
commands = new String[]{
|
StreamDialogEntry.enqueue_on_background,
|
||||||
context.getResources().getString(R.string.enqueue_on_background),
|
StreamDialogEntry.start_here_on_background,
|
||||||
context.getResources().getString(R.string.start_here_on_background),
|
StreamDialogEntry.set_as_playlist_thumbnail,
|
||||||
context.getResources().getString(R.string.set_as_playlist_thumbnail),
|
StreamDialogEntry.delete,
|
||||||
context.getResources().getString(R.string.delete),
|
StreamDialogEntry.append_playlist,
|
||||||
context.getResources().getString(R.string.append_playlist),
|
StreamDialogEntry.share);
|
||||||
context.getResources().getString(R.string.share),
|
|
||||||
};
|
|
||||||
} else {
|
} else {
|
||||||
commands = new String[]{
|
StreamDialogEntry.setEnabledEntries(
|
||||||
context.getResources().getString(R.string.enqueue_on_background),
|
StreamDialogEntry.enqueue_on_background,
|
||||||
context.getResources().getString(R.string.enqueue_on_popup),
|
StreamDialogEntry.enqueue_on_popup,
|
||||||
context.getResources().getString(R.string.start_here_on_background),
|
StreamDialogEntry.start_here_on_background,
|
||||||
context.getResources().getString(R.string.start_here_on_popup),
|
StreamDialogEntry.start_here_on_popup,
|
||||||
context.getResources().getString(R.string.set_as_playlist_thumbnail),
|
StreamDialogEntry.set_as_playlist_thumbnail,
|
||||||
context.getResources().getString(R.string.delete),
|
StreamDialogEntry.delete,
|
||||||
context.getResources().getString(R.string.append_playlist),
|
StreamDialogEntry.append_playlist,
|
||||||
context.getResources().getString(R.string.share),
|
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) -> {
|
new InfoItemDialog(activity, infoItem, StreamDialogEntry.getCommands(context), (dialog, which) ->
|
||||||
final int index = Math.max(itemListAdapter.getItemsList().indexOf(item), 0);
|
StreamDialogEntry.clickOn(which, this, infoItem)).show();
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setInitialData(long playlistId, String name) {
|
private void setInitialData(long playlistId, String name) {
|
||||||
|
|
|
@ -0,0 +1,92 @@
|
||||||
|
package org.schabi.newpipe.util;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.v4.app.Fragment;
|
||||||
|
|
||||||
|
import org.schabi.newpipe.R;
|
||||||
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
||||||
|
import org.schabi.newpipe.local.dialog.PlaylistAppendDialog;
|
||||||
|
import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
public enum StreamDialogEntry {
|
||||||
|
//////////////////////////////
|
||||||
|
// enum values with actions //
|
||||||
|
//////////////////////////////
|
||||||
|
|
||||||
|
enqueue_on_background(R.string.enqueue_on_background, (fragment, item) ->
|
||||||
|
NavigationHelper.enqueueOnBackgroundPlayer(fragment.getContext(), new SinglePlayQueue(item), false)),
|
||||||
|
|
||||||
|
enqueue_on_popup(R.string.enqueue_on_popup, (fragment, item) ->
|
||||||
|
NavigationHelper.enqueueOnPopupPlayer(fragment.getContext(), new SinglePlayQueue(item), false)),
|
||||||
|
|
||||||
|
start_here_on_background(R.string.start_here_on_background, (fragment, item) ->
|
||||||
|
NavigationHelper.playOnBackgroundPlayer(fragment.getContext(), new SinglePlayQueue(item), true)),
|
||||||
|
|
||||||
|
start_here_on_popup(R.string.start_here_on_popup, (fragment, item) ->
|
||||||
|
NavigationHelper.playOnPopupPlayer(fragment.getContext(), new SinglePlayQueue(item), true)),
|
||||||
|
|
||||||
|
set_as_playlist_thumbnail(R.string.set_as_playlist_thumbnail, (fragment, item) -> {}), // has to be set manually
|
||||||
|
|
||||||
|
delete(R.string.delete, (fragment, item) -> {}), // has to be set manually
|
||||||
|
|
||||||
|
append_playlist(R.string.append_playlist, (fragment, item) -> {
|
||||||
|
if (fragment.getFragmentManager() != null) {
|
||||||
|
PlaylistAppendDialog.fromStreamInfoItems(Collections.singletonList(item))
|
||||||
|
.show(fragment.getFragmentManager(), "StreamDialogEntry@append_playlist");
|
||||||
|
}}),
|
||||||
|
|
||||||
|
share(R.string.share, (fragment, item) ->
|
||||||
|
ShareUtils.shareUrl(fragment.getContext(), item.getName(), item.getUrl()));
|
||||||
|
|
||||||
|
|
||||||
|
///////////////
|
||||||
|
// variables //
|
||||||
|
///////////////
|
||||||
|
|
||||||
|
public interface StreamDialogEntryAction {
|
||||||
|
void onClick(Fragment fragment, final StreamInfoItem infoItem);
|
||||||
|
}
|
||||||
|
|
||||||
|
private final int resource;
|
||||||
|
private StreamDialogEntryAction action;
|
||||||
|
|
||||||
|
private static StreamDialogEntry[] enabledEntries;
|
||||||
|
|
||||||
|
|
||||||
|
///////////////////////////////////////////////////////
|
||||||
|
// non-static methods to initialize and edit entries //
|
||||||
|
///////////////////////////////////////////////////////
|
||||||
|
|
||||||
|
StreamDialogEntry(final int resource, StreamDialogEntryAction action) {
|
||||||
|
this.resource = resource;
|
||||||
|
this.action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAction(StreamDialogEntryAction action) {
|
||||||
|
this.action = action;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
// static methods that act on enabled entries //
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
|
||||||
|
public static void setEnabledEntries(StreamDialogEntry... entries) {
|
||||||
|
enabledEntries = entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String[] getCommands(Context context) {
|
||||||
|
String[] commands = new String[enabledEntries.length];
|
||||||
|
for (int i = 0; i != enabledEntries.length; ++i) {
|
||||||
|
commands[i] = context.getResources().getString(enabledEntries[i].resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
return commands;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clickOn(int which, Fragment fragment, StreamInfoItem infoItem) {
|
||||||
|
enabledEntries[which].action.onClick(fragment, infoItem);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue