Skip to content

Commit 50e2385

Browse files
committed
Add default entries automatically
1 parent 1cd3ef5 commit 50e2385

6 files changed

Lines changed: 53 additions & 37 deletions

File tree

app/src/main/java/org/schabi/newpipe/fragments/list/BaseListFragment.java

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -409,13 +409,7 @@ protected void showStreamDialog(final StreamInfoItem item) {
409409
return;
410410
}
411411

412-
final InfoItemDialog.Builder dialogBuilder = new InfoItemDialog.Builder(
413-
activity, this, item);
414-
415-
dialogBuilder.addDefaultEntriesAtBeginning();
416-
dialogBuilder.addDefaultEntriesAtEnd();
417-
418-
dialogBuilder.create().show();
412+
new InfoItemDialog.Builder(activity, this, item).create().show();
419413
}
420414

421415
/*//////////////////////////////////////////////////////////////////////////

app/src/main/java/org/schabi/newpipe/fragments/list/playlist/PlaylistFragment.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,6 @@ protected void showStreamDialog(final StreamInfoItem item) {
145145
final InfoItemDialog.Builder dialogBuilder = new InfoItemDialog.Builder(
146146
activity, this, item);
147147

148-
dialogBuilder.addDefaultEntriesAtBeginning();
149-
dialogBuilder.addDefaultEntriesAtEnd();
150-
151148
dialogBuilder.setAction(StreamDialogDefaultEntry.START_HERE_ON_BACKGROUND,
152149
(fragment, infoItem) -> NavigationHelper.playOnBackgroundPlayer(
153150
context, getPlayQueueStartingAt(infoItem), true));

app/src/main/java/org/schabi/newpipe/info_list/InfoItemDialog.java

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,39 @@ public void show() {
7676
*/
7777
public static class Builder {
7878
@NonNull private final Activity activity;
79-
@NonNull private final StreamInfoItem item;
79+
@NonNull private final StreamInfoItem infoItem;
8080
@NonNull private final Fragment fragment;
8181
@NonNull private final List<StreamDialogEntry> entries = new ArrayList<>();
82+
private final boolean addDefaultEntriesAutomatically;
8283

8384
public Builder(@NonNull final Activity activity,
8485
@NonNull final Fragment fragment,
85-
@NonNull final StreamInfoItem item) {
86+
@NonNull final StreamInfoItem infoItem) {
87+
this(activity, fragment, infoItem, true);
88+
}
89+
90+
/**
91+
* <p>Create an instance of this Builder</p>
92+
* @param activity
93+
* @param fragment
94+
* @param infoItem
95+
* @param addDefaultEntriesAutomatically whether default entries added with
96+
* {@link #addDefaultEntriesAtBeginning()} and
97+
* {@link #addDefaultEntriesAtEnd()}
98+
* are added automatically when generating
99+
* the {@link InfoItemDialog}.
100+
*/
101+
public Builder(@NonNull final Activity activity,
102+
@NonNull final Fragment fragment,
103+
@NonNull final StreamInfoItem infoItem,
104+
final boolean addDefaultEntriesAutomatically) {
86105
this.activity = activity;
87106
this.fragment = fragment;
88-
this.item = item;
107+
this.infoItem = infoItem;
108+
this.addDefaultEntriesAutomatically = addDefaultEntriesAutomatically;
109+
if (addDefaultEntriesAutomatically) {
110+
addDefaultEntriesAtBeginning();
111+
}
89112
}
90113

91114
public void addEntry(@NonNull final StreamDialogDefaultEntry entry) {
@@ -98,17 +121,26 @@ public void addAllEntries(@NonNull final StreamDialogDefaultEntry... newEntries)
98121
}
99122
}
100123

124+
/**
125+
* <p>Change an entries' action that is called when the entry is selected.</p>
126+
* <p><strong>Warning:</strong> Only use this method when the entry has been already added.
127+
* Changing the action of an entry which has not been added to the Builder yet
128+
* does not have an effect.</p>
129+
* @param entry the entry to change
130+
* @param action the action to perform when the entry is selected
131+
*/
101132
public void setAction(@NonNull final StreamDialogDefaultEntry entry,
102133
@NonNull final StreamDialogEntry.StreamDialogEntryAction action) {
103134
for (int i = 0; i < entries.size(); i++) {
104135
if (entries.get(i).resource == entry.resource) {
105136
entries.set(i, new StreamDialogEntry(entry.resource, action));
137+
return;
106138
}
107139
}
108140
}
109141

110142
public void addChannelDetailsEntryIfPossible() {
111-
if (!isNullOrEmpty(item.getUploaderUrl())) {
143+
if (!isNullOrEmpty(infoItem.getUploaderUrl())) {
112144
addEntry(StreamDialogDefaultEntry.SHOW_CHANNEL_DETAILS);
113145
}
114146
}
@@ -125,30 +157,29 @@ public void addEnqueueEntriesIfNeeded() {
125157

126158
public void addStartHereEntries() {
127159
addEntry(StreamDialogDefaultEntry.START_HERE_ON_BACKGROUND);
128-
if (item.getStreamType() != StreamType.AUDIO_STREAM
129-
&& item.getStreamType() != StreamType.AUDIO_LIVE_STREAM) {
160+
if (infoItem.getStreamType() != StreamType.AUDIO_STREAM
161+
&& infoItem.getStreamType() != StreamType.AUDIO_LIVE_STREAM) {
130162
addEntry(StreamDialogDefaultEntry.START_HERE_ON_POPUP);
131163
}
132164
}
133165

134166
/**
135167
* Adds {@link StreamDialogDefaultEntry.MARK_AS_WATCHED} if the watch history is enabled
136168
* and the stream is not a livestream.
137-
* @param streamType the item's stream type
138169
*/
139-
public void addMarkAsWatchedEntryIfNeeded(final StreamType streamType) {
170+
public void addMarkAsWatchedEntryIfNeeded() {
140171
final boolean isWatchHistoryEnabled = PreferenceManager
141172
.getDefaultSharedPreferences(activity)
142173
.getBoolean(activity.getString(R.string.enable_watch_history_key), false);
143-
if (streamType != StreamType.AUDIO_LIVE_STREAM
144-
&& streamType != StreamType.LIVE_STREAM
145-
&& isWatchHistoryEnabled) {
174+
if (isWatchHistoryEnabled
175+
&& infoItem.getStreamType() != StreamType.LIVE_STREAM
176+
&& infoItem.getStreamType() != StreamType.AUDIO_LIVE_STREAM) {
146177
addEntry(StreamDialogDefaultEntry.MARK_AS_WATCHED);
147178
}
148179
}
149180

150181
public void addPlayWithKodiEntryIfNeeded() {
151-
if (KoreUtils.shouldShowPlayWithKodi(activity, item.getServiceId())) {
182+
if (KoreUtils.shouldShowPlayWithKodi(activity, infoItem.getServiceId())) {
152183
addEntry(StreamDialogDefaultEntry.PLAY_WITH_KODI);
153184
}
154185
}
@@ -165,7 +196,7 @@ public void addDefaultEntriesAtEnd() {
165196
StreamDialogDefaultEntry.OPEN_IN_BROWSER
166197
);
167198
addPlayWithKodiEntryIfNeeded();
168-
addMarkAsWatchedEntryIfNeeded(item.getStreamType());
199+
addMarkAsWatchedEntryIfNeeded();
169200
addChannelDetailsEntryIfPossible();
170201
}
171202

@@ -174,7 +205,10 @@ public void addDefaultEntriesAtEnd() {
174205
* @return a new instance of {@link InfoItemDialog}
175206
*/
176207
public InfoItemDialog create() {
177-
return new InfoItemDialog(this.activity, this.fragment, this.item, this.entries);
208+
if (addDefaultEntriesAutomatically) {
209+
addDefaultEntriesAtEnd();
210+
}
211+
return new InfoItemDialog(this.activity, this.fragment, this.infoItem, this.entries);
178212
}
179213
}
180214
}

app/src/main/java/org/schabi/newpipe/local/feed/FeedFragment.kt

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -357,12 +357,7 @@ class FeedFragment : BaseStateFragment<FeedState>() {
357357
val activity: Activity? = getActivity()
358358
if (context == null || context.resources == null || activity == null) return
359359

360-
val dialogBuilder = InfoItemDialog.Builder(activity, this, item)
361-
362-
dialogBuilder.addDefaultEntriesAtBeginning()
363-
dialogBuilder.addDefaultEntriesAtEnd()
364-
365-
dialogBuilder.create().show()
360+
InfoItemDialog.Builder(activity, this, item).create().show()
366361
}
367362

368363
private val listenerStreamItem = object : OnItemClickListener, OnItemLongClickListener {

app/src/main/java/org/schabi/newpipe/local/history/StatisticsPlaylistFragment.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,8 @@ private void showStreamDialog(final StreamStatisticsEntry item) {
335335
final InfoItemDialog.Builder dialogBuilder = new InfoItemDialog.Builder(
336336
activity, this, infoItem);
337337

338-
// set entries
339-
dialogBuilder.addDefaultEntriesAtBeginning();
338+
// set entries in the middle; the others are added automatically
340339
dialogBuilder.addEntry(StreamDialogDefaultEntry.DELETE);
341-
dialogBuilder.addDefaultEntriesAtEnd();
342340

343341
// set custom actions
344342
dialogBuilder.setAction(StreamDialogDefaultEntry.START_HERE_ON_BACKGROUND,

app/src/main/java/org/schabi/newpipe/local/playlist/LocalPlaylistFragment.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -750,15 +750,13 @@ protected void showStreamItemDialog(final PlaylistStreamEntry item) {
750750
final InfoItemDialog.Builder dialogBuilder = new InfoItemDialog.Builder(
751751
activity, this, infoItem);
752752

753-
// set entries
754-
dialogBuilder.addDefaultEntriesAtBeginning();
753+
// add entries in the middle
755754
dialogBuilder.addAllEntries(
756755
StreamDialogDefaultEntry.SET_AS_PLAYLIST_THUMBNAIL,
757756
StreamDialogDefaultEntry.DELETE
758757
);
759-
dialogBuilder.addDefaultEntriesAtEnd();
760758

761-
// set custom actions
759+
// set custom actions; all entries modified here have already been added within the builder
762760
dialogBuilder.setAction(StreamDialogDefaultEntry.START_HERE_ON_BACKGROUND,
763761
(fragment, infoItemDuplicate) -> NavigationHelper.playOnBackgroundPlayer(
764762
context, getPlayQueueStartingAt(item), true));

0 commit comments

Comments
 (0)