Skip to content

Commit b869fb8

Browse files
committed
Use getPlayQueueStartingAt in BaseListFragment
Instead of overriding the whole showInfoItemDialog
1 parent 2d24b74 commit b869fb8

4 files changed

Lines changed: 45 additions & 46 deletions

File tree

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

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.schabi.newpipe.fragments.OnScrollBelowItemsListener;
3131
import org.schabi.newpipe.info_list.InfoListAdapter;
3232
import org.schabi.newpipe.info_list.ItemViewMode;
33+
import org.schabi.newpipe.player.playqueue.PlayQueue;
3334
import org.schabi.newpipe.ui.components.menu.LongPressAction;
3435
import org.schabi.newpipe.ui.components.menu.LongPressable;
3536
import org.schabi.newpipe.util.NavigationHelper;
@@ -42,6 +43,8 @@
4243
import java.util.Queue;
4344
import java.util.function.Supplier;
4445

46+
import kotlin.jvm.functions.Function0;
47+
4548
public abstract class BaseListFragment<I, N> extends BaseStateFragment<I>
4649
implements ListViewContract<I, N>, StateSaver.WriteRead,
4750
SharedPreferences.OnSharedPreferenceChangeListener {
@@ -267,8 +270,12 @@ public void selected(final StreamInfoItem selectedItem) {
267270
}
268271

269272
@Override
270-
public void held(final StreamInfoItem selectedItem) {
271-
showInfoItemDialog(selectedItem);
273+
public void held(final StreamInfoItem item) {
274+
openLongPressMenuInActivity(
275+
requireActivity(),
276+
LongPressable.fromStreamInfoItem(item),
277+
LongPressAction.fromStreamInfoItem(item, getPlayQueueStartingAt(item))
278+
);
272279
}
273280
});
274281

@@ -325,13 +332,15 @@ public void held(final PlaylistInfoItem selectedItem) {
325332
useNormalItemListScrollListener();
326333
}
327334

328-
protected void showInfoItemDialog(final StreamInfoItem item) {
329-
openLongPressMenuInActivity(
330-
requireActivity(),
331-
LongPressable.fromStreamInfoItem(item),
332-
// TODO generalize obtaining queue from here when fully migrating to Compose
333-
LongPressAction.fromStreamInfoItem(item, null)
334-
);
335+
/**
336+
* @param item an item in the list, from which the built queue should start
337+
* @return a builder for a queue containing all of the items in this list, with the queue index
338+
* set to the item passed as parameter; return {@code null} if no "start playing from here"
339+
* options should be shown
340+
*/
341+
@Nullable
342+
protected Function0<PlayQueue> getPlayQueueStartingAt(@NonNull final StreamInfoItem item) {
343+
return null; // disable "play from here" options by default (e.g. in search, kiosks)
335344
}
336345

337346
/**

app/src/main/java/org/schabi/newpipe/fragments/list/channel/ChannelTabFragment.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package org.schabi.newpipe.fragments.list.channel;
22

3-
import static org.schabi.newpipe.ui.components.menu.LongPressMenuKt.openLongPressMenuInActivity;
4-
53
import android.os.Bundle;
64
import android.util.Log;
75
import android.view.LayoutInflater;
@@ -28,8 +26,6 @@
2826
import org.schabi.newpipe.fragments.list.playlist.PlaylistControlViewHolder;
2927
import org.schabi.newpipe.player.playqueue.ChannelTabPlayQueue;
3028
import org.schabi.newpipe.player.playqueue.PlayQueue;
31-
import org.schabi.newpipe.ui.components.menu.LongPressAction;
32-
import org.schabi.newpipe.ui.components.menu.LongPressable;
3329
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
3430
import org.schabi.newpipe.util.ChannelTabHelper;
3531
import org.schabi.newpipe.util.ExtractorHelper;
@@ -41,6 +37,7 @@
4137
import java.util.stream.Collectors;
4238

4339
import io.reactivex.rxjava3.core.Single;
40+
import kotlin.jvm.functions.Function0;
4441

4542
public class ChannelTabFragment extends BaseListInfoFragment<InfoItem, ChannelTabInfo>
4643
implements PlaylistControlViewHolder {
@@ -169,19 +166,6 @@ public void handleResult(@NonNull final ChannelTabInfo result) {
169166
}
170167
}
171168

172-
@Override
173-
protected void showInfoItemDialog(final StreamInfoItem item) {
174-
openLongPressMenuInActivity(
175-
requireActivity(),
176-
LongPressable.fromStreamInfoItem(item),
177-
LongPressAction.fromStreamInfoItem(item, () -> getPlayQueueStartingAt(item))
178-
);
179-
}
180-
181-
private PlayQueue getPlayQueueStartingAt(final StreamInfoItem infoItem) {
182-
return getPlayQueue(streamItems -> Math.max(streamItems.indexOf(infoItem), 0));
183-
}
184-
185169
public PlayQueue getPlayQueue(final Function<List<StreamInfoItem>, Integer> index) {
186170
final List<StreamInfoItem> streamItems = infoListAdapter.getItemsList().stream()
187171
.filter(StreamInfoItem.class::isInstance)
@@ -196,4 +180,10 @@ public PlayQueue getPlayQueue(final Function<List<StreamInfoItem>, Integer> inde
196180
public PlayQueue getPlayQueue() {
197181
return getPlayQueue(streamItems -> 0);
198182
}
183+
184+
@Nullable
185+
@Override
186+
protected Function0<PlayQueue> getPlayQueueStartingAt(@NonNull final StreamInfoItem item) {
187+
return () -> getPlayQueue(streamItems -> Math.max(streamItems.indexOf(item), 0));
188+
}
199189
}

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static org.schabi.newpipe.extractor.utils.Utils.isBlank;
44
import static org.schabi.newpipe.ktx.ViewUtils.animate;
55
import static org.schabi.newpipe.ktx.ViewUtils.animateHideRecyclerViewAllowingScrolling;
6-
import static org.schabi.newpipe.ui.components.menu.LongPressMenuKt.openLongPressMenuInActivity;
76
import static org.schabi.newpipe.util.ServiceHelper.getServiceById;
87

98
import android.os.Bundle;
@@ -46,8 +45,6 @@
4645
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
4746
import org.schabi.newpipe.player.playqueue.PlayQueue;
4847
import org.schabi.newpipe.player.playqueue.PlaylistPlayQueue;
49-
import org.schabi.newpipe.ui.components.menu.LongPressAction;
50-
import org.schabi.newpipe.ui.components.menu.LongPressable;
5148
import org.schabi.newpipe.util.ExtractorHelper;
5249
import org.schabi.newpipe.util.Localization;
5350
import org.schabi.newpipe.util.NavigationHelper;
@@ -68,6 +65,7 @@
6865
import io.reactivex.rxjava3.core.Single;
6966
import io.reactivex.rxjava3.disposables.CompositeDisposable;
7067
import io.reactivex.rxjava3.disposables.Disposable;
68+
import kotlin.jvm.functions.Function0;
7169

7270
public class PlaylistFragment extends BaseListInfoFragment<StreamInfoItem, PlaylistInfo>
7371
implements PlaylistControlViewHolder {
@@ -144,19 +142,6 @@ protected void initViews(final View rootView, final Bundle savedInstanceState) {
144142
infoListAdapter.setUseMiniVariant(true);
145143
}
146144

147-
private PlayQueue getPlayQueueStartingAt(final StreamInfoItem infoItem) {
148-
return getPlayQueue(Math.max(infoListAdapter.getItemsList().indexOf(infoItem), 0));
149-
}
150-
151-
@Override
152-
protected void showInfoItemDialog(final StreamInfoItem item) {
153-
openLongPressMenuInActivity(
154-
activity,
155-
LongPressable.fromStreamInfoItem(item),
156-
LongPressAction.fromStreamInfoItem(item, () -> getPlayQueueStartingAt(item))
157-
);
158-
}
159-
160145
@Override
161146
public void onCreateOptionsMenu(@NonNull final Menu menu,
162147
@NonNull final MenuInflater inflater) {
@@ -361,10 +346,6 @@ public void handleResult(@NonNull final PlaylistInfo result) {
361346
PlayButtonHelper.initPlaylistControlClickListener(activity, playlistControlBinding, this);
362347
}
363348

364-
public PlayQueue getPlayQueue() {
365-
return getPlayQueue(0);
366-
}
367-
368349
private PlayQueue getPlayQueue(final int index) {
369350
final List<StreamInfoItem> infoItems = new ArrayList<>();
370351
for (final InfoItem i : infoListAdapter.getItemsList()) {
@@ -381,6 +362,17 @@ private PlayQueue getPlayQueue(final int index) {
381362
);
382363
}
383364

365+
@Override
366+
public PlayQueue getPlayQueue() {
367+
return getPlayQueue(0);
368+
}
369+
370+
@Nullable
371+
@Override
372+
protected Function0<PlayQueue> getPlayQueueStartingAt(@NonNull final StreamInfoItem item) {
373+
return () -> getPlayQueue(Math.max(infoListAdapter.getItemsList().indexOf(item), 0));
374+
}
375+
384376
/*//////////////////////////////////////////////////////////////////////////
385377
// Utils
386378
//////////////////////////////////////////////////////////////////////////*/

app/src/main/java/org/schabi/newpipe/ui/components/menu/LongPressAction.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,14 @@ data class LongPressAction(
141141
)
142142
}
143143

144+
/**
145+
* Instead of queueFromHere, this function could possibly take a
146+
* `() -> List<StreamInfoItem/StreamEntity/...>` plus the `StreamInfoItem/StreamEntity/...`
147+
* that was long-pressed, and take care of searching through the list to find the item
148+
* index, and finally take care of building the queue. It would deduplicate some code in
149+
* fragments, but it's probably not possible to do because of all the different types of
150+
* the items involved.
151+
*/
144152
private fun buildPlayerFromHereActionList(queueFromHere: () -> PlayQueue): List<LongPressAction> {
145153
return listOf(
146154
Type.BackgroundFromHere.buildAction { context ->

0 commit comments

Comments
 (0)