Skip to content

Commit 37517c7

Browse files
authored
Merge pull request #7570 from TeamNewPipe/improvement/infoItemDialogBuilder
Refactor generating InfoItemDialog's
2 parents a4dee77 + d3bc184 commit 37517c7

13 files changed

Lines changed: 692 additions & 598 deletions

File tree

app/src/main/java/org/schabi/newpipe/error/UserAction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ public enum UserAction {
2828
DOWNLOAD_FAILED("download failed"),
2929
PREFERENCES_MIGRATION("migration of preferences"),
3030
SHARE_TO_NEWPIPE("share to newpipe"),
31-
CHECK_FOR_NEW_APP_VERSION("check for new app version");
32-
31+
CHECK_FOR_NEW_APP_VERSION("check for new app version"),
32+
OPEN_INFO_ITEM_DIALOG("open info item dialog");
3333

3434
private final String message;
3535

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

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.schabi.newpipe.fragments.list;
22

3-
import android.app.Activity;
3+
import static org.schabi.newpipe.ktx.ViewUtils.animate;
4+
import static org.schabi.newpipe.ktx.ViewUtils.animateHideRecyclerViewAllowingScrolling;
5+
46
import android.content.Context;
57
import android.content.SharedPreferences;
68
import android.content.res.Configuration;
@@ -25,29 +27,19 @@
2527
import org.schabi.newpipe.extractor.comments.CommentsInfoItem;
2628
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
2729
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
28-
import org.schabi.newpipe.extractor.stream.StreamType;
2930
import org.schabi.newpipe.fragments.BaseStateFragment;
3031
import org.schabi.newpipe.fragments.OnScrollBelowItemsListener;
31-
import org.schabi.newpipe.info_list.InfoItemDialog;
32+
import org.schabi.newpipe.info_list.dialog.InfoItemDialog;
3233
import org.schabi.newpipe.info_list.InfoListAdapter;
33-
import org.schabi.newpipe.player.helper.PlayerHolder;
34-
import org.schabi.newpipe.util.external_communication.KoreUtils;
3534
import org.schabi.newpipe.util.NavigationHelper;
3635
import org.schabi.newpipe.util.OnClickGesture;
3736
import org.schabi.newpipe.util.StateSaver;
38-
import org.schabi.newpipe.util.StreamDialogEntry;
3937
import org.schabi.newpipe.views.SuperScrollLayoutManager;
4038

41-
import java.util.ArrayList;
42-
import java.util.Arrays;
4339
import java.util.List;
4440
import java.util.Queue;
4541
import java.util.function.Supplier;
4642

47-
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
48-
import static org.schabi.newpipe.ktx.ViewUtils.animate;
49-
import static org.schabi.newpipe.ktx.ViewUtils.animateHideRecyclerViewAllowingScrolling;
50-
5143
public abstract class BaseListFragment<I, N> extends BaseStateFragment<I>
5244
implements ListViewContract<I, N>, StateSaver.WriteRead,
5345
SharedPreferences.OnSharedPreferenceChangeListener {
@@ -268,7 +260,7 @@ public void selected(final StreamInfoItem selectedItem) {
268260

269261
@Override
270262
public void held(final StreamInfoItem selectedItem) {
271-
showStreamDialog(selectedItem);
263+
showInfoItemDialog(selectedItem);
272264
}
273265
});
274266

@@ -409,55 +401,12 @@ protected void onScrollToBottom() {
409401
}
410402
}
411403

412-
protected void showStreamDialog(final StreamInfoItem item) {
413-
final Context context = getContext();
414-
final Activity activity = getActivity();
415-
if (context == null || context.getResources() == null || activity == null) {
416-
return;
417-
}
418-
final List<StreamDialogEntry> entries = new ArrayList<>();
419-
420-
if (PlayerHolder.getInstance().isPlayQueueReady()) {
421-
entries.add(StreamDialogEntry.enqueue);
422-
423-
if (PlayerHolder.getInstance().getQueueSize() > 1) {
424-
entries.add(StreamDialogEntry.enqueue_next);
425-
}
426-
}
427-
428-
if (item.getStreamType() == StreamType.AUDIO_STREAM) {
429-
entries.addAll(Arrays.asList(
430-
StreamDialogEntry.start_here_on_background,
431-
StreamDialogEntry.append_playlist,
432-
StreamDialogEntry.share
433-
));
434-
} else {
435-
entries.addAll(Arrays.asList(
436-
StreamDialogEntry.start_here_on_background,
437-
StreamDialogEntry.start_here_on_popup,
438-
StreamDialogEntry.append_playlist,
439-
StreamDialogEntry.share
440-
));
441-
}
442-
entries.add(StreamDialogEntry.open_in_browser);
443-
if (KoreUtils.shouldShowPlayWithKodi(context, item.getServiceId())) {
444-
entries.add(StreamDialogEntry.play_with_kodi);
445-
}
446-
447-
// show "mark as watched" only when watch history is enabled
448-
if (StreamDialogEntry.shouldAddMarkAsWatched(item.getStreamType(), context)) {
449-
entries.add(
450-
StreamDialogEntry.mark_as_watched
451-
);
452-
}
453-
if (!isNullOrEmpty(item.getUploaderUrl())) {
454-
entries.add(StreamDialogEntry.show_channel_details);
404+
protected void showInfoItemDialog(final StreamInfoItem item) {
405+
try {
406+
new InfoItemDialog.Builder(getActivity(), getContext(), this, item).create().show();
407+
} catch (final IllegalArgumentException e) {
408+
InfoItemDialog.Builder.reportErrorDuringInitialization(e, item);
455409
}
456-
457-
StreamDialogEntry.setEnabledEntries(entries);
458-
459-
new InfoItemDialog(activity, item, StreamDialogEntry.getCommands(context),
460-
(dialog, which) -> StreamDialogEntry.clickOn(which, this, item)).show();
461410
}
462411

463412
/*//////////////////////////////////////////////////////////////////////////

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

Lines changed: 16 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package org.schabi.newpipe.fragments.list.playlist;
22

3-
import static org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty;
43
import static org.schabi.newpipe.ktx.ViewUtils.animate;
54
import static org.schabi.newpipe.ktx.ViewUtils.animateHideRecyclerViewAllowingScrolling;
65

7-
import android.app.Activity;
86
import android.content.Context;
97
import android.content.res.ColorStateList;
108
import android.os.Bundle;
@@ -41,24 +39,20 @@
4139
import org.schabi.newpipe.extractor.playlist.PlaylistInfo;
4240
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
4341
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
44-
import org.schabi.newpipe.extractor.stream.StreamType;
4542
import org.schabi.newpipe.fragments.list.BaseListInfoFragment;
46-
import org.schabi.newpipe.info_list.InfoItemDialog;
43+
import org.schabi.newpipe.info_list.dialog.InfoItemDialog;
4744
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
4845
import org.schabi.newpipe.player.MainPlayer.PlayerType;
49-
import org.schabi.newpipe.player.helper.PlayerHolder;
5046
import org.schabi.newpipe.player.playqueue.PlayQueue;
5147
import org.schabi.newpipe.player.playqueue.PlaylistPlayQueue;
5248
import org.schabi.newpipe.util.ExtractorHelper;
5349
import org.schabi.newpipe.util.Localization;
5450
import org.schabi.newpipe.util.NavigationHelper;
5551
import org.schabi.newpipe.util.PicassoHelper;
56-
import org.schabi.newpipe.util.StreamDialogEntry;
57-
import org.schabi.newpipe.util.external_communication.KoreUtils;
52+
import org.schabi.newpipe.info_list.dialog.StreamDialogDefaultEntry;
5853
import org.schabi.newpipe.util.external_communication.ShareUtils;
5954

6055
import java.util.ArrayList;
61-
import java.util.Arrays;
6256
import java.util.List;
6357
import java.util.concurrent.atomic.AtomicBoolean;
6458
import java.util.function.Supplier;
@@ -145,60 +139,22 @@ private PlayQueue getPlayQueueStartingAt(final StreamInfoItem infoItem) {
145139
}
146140

147141
@Override
148-
protected void showStreamDialog(final StreamInfoItem item) {
142+
protected void showInfoItemDialog(final StreamInfoItem item) {
149143
final Context context = getContext();
150-
final Activity activity = getActivity();
151-
if (context == null || context.getResources() == null || activity == null) {
152-
return;
153-
}
154-
155-
final ArrayList<StreamDialogEntry> entries = new ArrayList<>();
156-
157-
if (PlayerHolder.getInstance().isPlayQueueReady()) {
158-
entries.add(StreamDialogEntry.enqueue);
159-
160-
if (PlayerHolder.getInstance().getQueueSize() > 1) {
161-
entries.add(StreamDialogEntry.enqueue_next);
162-
}
163-
}
164-
165-
if (item.getStreamType() == StreamType.AUDIO_STREAM) {
166-
entries.addAll(Arrays.asList(
167-
StreamDialogEntry.start_here_on_background,
168-
StreamDialogEntry.append_playlist,
169-
StreamDialogEntry.share
170-
));
171-
} else {
172-
entries.addAll(Arrays.asList(
173-
StreamDialogEntry.start_here_on_background,
174-
StreamDialogEntry.start_here_on_popup,
175-
StreamDialogEntry.append_playlist,
176-
StreamDialogEntry.share
177-
));
144+
try {
145+
final InfoItemDialog.Builder dialogBuilder =
146+
new InfoItemDialog.Builder(getActivity(), context, this, item);
147+
148+
dialogBuilder
149+
.setAction(
150+
StreamDialogDefaultEntry.START_HERE_ON_BACKGROUND,
151+
(f, infoItem) -> NavigationHelper.playOnBackgroundPlayer(
152+
context, getPlayQueueStartingAt(infoItem), true))
153+
.create()
154+
.show();
155+
} catch (final IllegalArgumentException e) {
156+
InfoItemDialog.Builder.reportErrorDuringInitialization(e, item);
178157
}
179-
entries.add(StreamDialogEntry.open_in_browser);
180-
if (KoreUtils.shouldShowPlayWithKodi(context, item.getServiceId())) {
181-
entries.add(StreamDialogEntry.play_with_kodi);
182-
}
183-
184-
// show "mark as watched" only when watch history is enabled
185-
if (StreamDialogEntry.shouldAddMarkAsWatched(item.getStreamType(), context)) {
186-
entries.add(
187-
StreamDialogEntry.mark_as_watched
188-
);
189-
}
190-
if (!isNullOrEmpty(item.getUploaderUrl())) {
191-
entries.add(StreamDialogEntry.show_channel_details);
192-
}
193-
194-
StreamDialogEntry.setEnabledEntries(entries);
195-
196-
StreamDialogEntry.start_here_on_background.setCustomAction((fragment, infoItem) ->
197-
NavigationHelper.playOnBackgroundPlayer(context,
198-
getPlayQueueStartingAt(infoItem), true));
199-
200-
new InfoItemDialog(activity, item, StreamDialogEntry.getCommands(context),
201-
(dialog, which) -> StreamDialogEntry.clickOn(which, this, item)).show();
202158
}
203159

204160
@Override

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

Lines changed: 0 additions & 54 deletions
This file was deleted.

0 commit comments

Comments
 (0)