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

@ -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<I, N> extends BaseStateFragment<I> 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();
}
/*//////////////////////////////////////////////////////////////////////////