diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java index de4c17c3d..2291528c3 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java @@ -9,6 +9,7 @@ import android.content.res.Resources; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.annotation.NonNull; +import android.support.v4.app.Fragment; import android.support.v7.app.ActionBar; import android.support.v7.app.AppCompatActivity; 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.ShareUtils; 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.List; import java.util.Queue; +import java.util.function.DoubleBinaryOperator; import static org.schabi.newpipe.util.AnimationUtils.animateView; @@ -259,58 +264,32 @@ public abstract class BaseListFragment extends BaseStateFragment implem } } + + + protected void showStreamDialog(final StreamInfoItem item) { final Context context = getContext(); final Activity activity = getActivity(); if (context == null || context.getResources() == null || activity == null) return; - boolean isAudioStream = (item.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.append_playlist), - context.getResources().getString(R.string.share) - }; + if (item.getStreamType() == StreamType.AUDIO_STREAM) { + StreamDialogEntry.setEnabledEntries( + StreamDialogEntry.enqueue_on_background, + StreamDialogEntry.start_here_on_background, + 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.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.append_playlist, + StreamDialogEntry.share); } - final DialogInterface.OnClickListener actions = (dialogInterface, i) -> { - if (i == 0) { - 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(); + new InfoItemDialog(activity, item, StreamDialogEntry.getCommands(context), (dialog, which) -> + StreamDialogEntry.clickOn(which, this, item)).show(); } /*////////////////////////////////////////////////////////////////////////// diff --git a/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java index 67d00a3b1..7ebe2bbfc 100644 --- a/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java @@ -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) { diff --git a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java index ef2a0d8d5..db802272b 100644 --- a/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java +++ b/app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java @@ -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 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) { diff --git a/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java b/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java new file mode 100644 index 000000000..aed112983 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/util/StreamDialogEntry.java @@ -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); + } +}