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
6c24e1cc39
commit
bd18d38e5c
4 changed files with 159 additions and 151 deletions
|
|
@ -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…
Add table
Add a link
Reference in a new issue