Skip to content

Commit ab3c29c

Browse files
committed
Replace InfoItemDialog with LongPressMenu
1 parent 07aa327 commit ab3c29c

11 files changed

Lines changed: 166 additions & 322 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.schabi.newpipe.ktx.ViewUtils.animate;
44
import static org.schabi.newpipe.ktx.ViewUtils.animateHideRecyclerViewAllowingScrolling;
5+
import static org.schabi.newpipe.ui.components.menu.LongPressMenuKt.openLongPressMenuInActivity;
56

67
import android.content.Context;
78
import android.content.SharedPreferences;
@@ -27,7 +28,8 @@
2728
import org.schabi.newpipe.fragments.OnScrollBelowItemsListener;
2829
import org.schabi.newpipe.info_list.InfoListAdapter;
2930
import org.schabi.newpipe.info_list.ItemViewMode;
30-
import org.schabi.newpipe.info_list.dialog.InfoItemDialog;
31+
import org.schabi.newpipe.ui.components.menu.LongPressAction;
32+
import org.schabi.newpipe.ui.components.menu.LongPressable;
3133
import org.schabi.newpipe.util.NavigationHelper;
3234
import org.schabi.newpipe.util.OnClickGesture;
3335
import org.schabi.newpipe.util.StateSaver;
@@ -387,11 +389,11 @@ protected void onScrollToBottom() {
387389
}
388390

389391
protected void showInfoItemDialog(final StreamInfoItem item) {
390-
try {
391-
new InfoItemDialog.Builder(getActivity(), getContext(), this, item).create().show();
392-
} catch (final IllegalArgumentException e) {
393-
InfoItemDialog.Builder.reportErrorDuringInitialization(e, item);
394-
}
392+
openLongPressMenuInActivity(
393+
requireActivity(),
394+
LongPressable.fromStreamInfoItem(item),
395+
LongPressAction.fromStreamInfoItem(item)
396+
);
395397
}
396398

397399
/*//////////////////////////////////////////////////////////////////////////

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

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
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.getLongPressMenuView;
6+
import static org.schabi.newpipe.ui.components.menu.LongPressMenuKt.openLongPressMenuInActivity;
77
import static org.schabi.newpipe.util.ServiceHelper.getServiceById;
88

9-
import android.content.Context;
109
import android.os.Bundle;
1110
import android.text.TextUtils;
1211
import android.util.Log;
@@ -43,8 +42,6 @@
4342
import org.schabi.newpipe.extractor.stream.Description;
4443
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
4544
import org.schabi.newpipe.fragments.list.BaseListInfoFragment;
46-
import org.schabi.newpipe.info_list.dialog.InfoItemDialog;
47-
import org.schabi.newpipe.info_list.dialog.StreamDialogDefaultEntry;
4845
import org.schabi.newpipe.local.dialog.PlaylistDialog;
4946
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
5047
import org.schabi.newpipe.player.playqueue.PlayQueue;
@@ -153,35 +150,12 @@ private PlayQueue getPlayQueueStartingAt(final StreamInfoItem infoItem) {
153150

154151
@Override
155152
protected void showInfoItemDialog(final StreamInfoItem item) {
156-
activity.addContentView(
157-
getLongPressMenuView(
158-
requireContext(),
159-
LongPressable.from(item),
160-
LongPressAction.buildActionList(item, false)
161-
),
162-
new ViewGroup.LayoutParams(
163-
ViewGroup.LayoutParams.MATCH_PARENT,
164-
ViewGroup.LayoutParams.MATCH_PARENT
165-
)
153+
openLongPressMenuInActivity(
154+
activity,
155+
LongPressable.fromStreamInfoItem(item),
156+
// TODO handle play queue starting at
157+
LongPressAction.fromStreamInfoItem(item)
166158
);
167-
if (Context.class.getSimpleName().startsWith("C")) {
168-
return;
169-
}
170-
final Context context = getContext();
171-
try {
172-
final InfoItemDialog.Builder dialogBuilder =
173-
new InfoItemDialog.Builder(getActivity(), context, this, item);
174-
175-
dialogBuilder
176-
.setAction(
177-
StreamDialogDefaultEntry.START_HERE_ON_BACKGROUND,
178-
(f, infoItem) -> NavigationHelper.playOnBackgroundPlayer(
179-
context, getPlayQueueStartingAt(infoItem), true))
180-
.create()
181-
.show();
182-
} catch (final IllegalArgumentException e) {
183-
InfoItemDialog.Builder.reportErrorDuringInitialization(e, item);
184-
}
185159
}
186160

187161
@Override

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

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
package org.schabi.newpipe.local.feed
2121

2222
import android.annotation.SuppressLint
23-
import android.app.Activity
2423
import android.content.Context
2524
import android.content.Intent
2625
import android.content.SharedPreferences
@@ -63,17 +62,18 @@ import org.schabi.newpipe.error.ErrorUtil
6362
import org.schabi.newpipe.error.UserAction
6463
import org.schabi.newpipe.extractor.exceptions.AccountTerminatedException
6564
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException
66-
import org.schabi.newpipe.extractor.stream.StreamInfoItem
6765
import org.schabi.newpipe.extractor.utils.Utils.isNullOrEmpty
6866
import org.schabi.newpipe.fragments.BaseStateFragment
6967
import org.schabi.newpipe.info_list.ItemViewMode
70-
import org.schabi.newpipe.info_list.dialog.InfoItemDialog
7168
import org.schabi.newpipe.ktx.animate
7269
import org.schabi.newpipe.ktx.animateHideRecyclerViewAllowingScrolling
7370
import org.schabi.newpipe.ktx.slideUp
7471
import org.schabi.newpipe.local.feed.item.StreamItem
7572
import org.schabi.newpipe.local.feed.service.FeedLoadService
7673
import org.schabi.newpipe.local.subscription.SubscriptionManager
74+
import org.schabi.newpipe.ui.components.menu.LongPressAction
75+
import org.schabi.newpipe.ui.components.menu.LongPressable
76+
import org.schabi.newpipe.ui.components.menu.openLongPressMenuInActivity
7777
import org.schabi.newpipe.ui.emptystate.setEmptyStateComposable
7878
import org.schabi.newpipe.util.DeviceUtils
7979
import org.schabi.newpipe.util.Localization
@@ -368,14 +368,6 @@ class FeedFragment : BaseStateFragment<FeedState>() {
368368
feedBinding.loadingProgressBar.max = progressState.maxProgress
369369
}
370370

371-
private fun showInfoItemDialog(item: StreamInfoItem) {
372-
val context = context
373-
val activity: Activity? = getActivity()
374-
if (context == null || context.resources == null || activity == null) return
375-
376-
InfoItemDialog.Builder(activity, context, this, item).create().show()
377-
}
378-
379371
private val listenerStreamItem = object : OnItemClickListener, OnItemLongClickListener {
380372
override fun onItemClick(item: Item<*>, view: View) {
381373
if (item is StreamItem && !isRefreshing) {
@@ -389,7 +381,11 @@ class FeedFragment : BaseStateFragment<FeedState>() {
389381

390382
override fun onItemLongClick(item: Item<*>, view: View): Boolean {
391383
if (item is StreamItem && !isRefreshing) {
392-
showInfoItemDialog(item.streamWithState.stream.toStreamInfoItem())
384+
openLongPressMenuInActivity(
385+
requireActivity(),
386+
LongPressable.fromStreamEntity(item.streamWithState.stream),
387+
LongPressAction.fromStreamEntity(item.streamWithState.stream),
388+
)
393389
return true
394390
}
395391
return false

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

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.schabi.newpipe.local.history;
22

3-
import android.content.Context;
3+
import static org.schabi.newpipe.ui.components.menu.LongPressMenuKt.openLongPressMenuInActivity;
4+
45
import android.os.Bundle;
56
import android.os.Parcelable;
67
import android.view.LayoutInflater;
@@ -9,14 +10,12 @@
910
import android.view.MenuItem;
1011
import android.view.View;
1112
import android.view.ViewGroup;
12-
import android.widget.Toast;
1313

1414
import androidx.annotation.NonNull;
1515
import androidx.annotation.Nullable;
1616
import androidx.viewbinding.ViewBinding;
1717

1818
import com.evernote.android.state.State;
19-
import com.google.android.material.snackbar.Snackbar;
2019

2120
import org.reactivestreams.Subscriber;
2221
import org.reactivestreams.Subscription;
@@ -30,12 +29,12 @@
3029
import org.schabi.newpipe.error.UserAction;
3130
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
3231
import org.schabi.newpipe.fragments.list.playlist.PlaylistControlViewHolder;
33-
import org.schabi.newpipe.info_list.dialog.InfoItemDialog;
34-
import org.schabi.newpipe.info_list.dialog.StreamDialogDefaultEntry;
3532
import org.schabi.newpipe.local.BaseLocalListFragment;
3633
import org.schabi.newpipe.player.playqueue.PlayQueue;
3734
import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
3835
import org.schabi.newpipe.settings.HistorySettingsFragment;
36+
import org.schabi.newpipe.ui.components.menu.LongPressAction;
37+
import org.schabi.newpipe.ui.components.menu.LongPressable;
3938
import org.schabi.newpipe.util.NavigationHelper;
4039
import org.schabi.newpipe.util.OnClickGesture;
4140
import org.schabi.newpipe.util.PlayButtonHelper;
@@ -48,7 +47,6 @@
4847

4948
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
5049
import io.reactivex.rxjava3.disposables.CompositeDisposable;
51-
import io.reactivex.rxjava3.disposables.Disposable;
5250

5351
public class StatisticsPlaylistFragment
5452
extends BaseLocalListFragment<List<StreamStatisticsEntry>, Void>
@@ -318,50 +316,11 @@ private PlayQueue getPlayQueueStartingAt(final StreamStatisticsEntry infoItem) {
318316
}
319317

320318
private void showInfoItemDialog(final StreamStatisticsEntry item) {
321-
final Context context = getContext();
322-
final StreamInfoItem infoItem = item.toStreamInfoItem();
323-
324-
try {
325-
final InfoItemDialog.Builder dialogBuilder =
326-
new InfoItemDialog.Builder(getActivity(), context, this, infoItem);
327-
328-
// set entries in the middle; the others are added automatically
329-
dialogBuilder
330-
.addEntry(StreamDialogDefaultEntry.DELETE)
331-
.setAction(
332-
StreamDialogDefaultEntry.DELETE,
333-
(f, i) -> deleteEntry(
334-
Math.max(itemListAdapter.getItemsList().indexOf(item), 0)))
335-
.create()
336-
.show();
337-
} catch (final IllegalArgumentException e) {
338-
InfoItemDialog.Builder.reportErrorDuringInitialization(e, infoItem);
339-
}
340-
}
341-
342-
private void deleteEntry(final int index) {
343-
final LocalItem infoItem = itemListAdapter.getItemsList().get(index);
344-
if (infoItem instanceof StreamStatisticsEntry) {
345-
final StreamStatisticsEntry entry = (StreamStatisticsEntry) infoItem;
346-
final Disposable onDelete = recordManager
347-
.deleteStreamHistoryAndState(entry.getStreamId())
348-
.observeOn(AndroidSchedulers.mainThread())
349-
.subscribe(
350-
() -> {
351-
if (getView() != null) {
352-
Snackbar.make(getView(), R.string.one_item_deleted,
353-
Snackbar.LENGTH_SHORT).show();
354-
} else {
355-
Toast.makeText(getContext(),
356-
R.string.one_item_deleted,
357-
Toast.LENGTH_SHORT).show();
358-
}
359-
},
360-
throwable -> showSnackBarError(new ErrorInfo(throwable,
361-
UserAction.DELETE_FROM_HISTORY, "Deleting item")));
362-
363-
disposables.add(onDelete);
364-
}
319+
openLongPressMenuInActivity(
320+
requireActivity(),
321+
LongPressable.fromStreamEntity(item.getStreamEntity()),
322+
LongPressAction.fromStreamStatisticsEntry(item)
323+
);
365324
}
366325

367326
@Override

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

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.schabi.newpipe.error.ErrorUtil.showUiErrorSnackbar;
44
import static org.schabi.newpipe.ktx.ViewUtils.animate;
5+
import static org.schabi.newpipe.ui.components.menu.LongPressMenuKt.openLongPressMenuInActivity;
56
import static org.schabi.newpipe.util.ThemeHelper.shouldUseGridLayout;
67

78
import android.content.Context;
@@ -45,12 +46,12 @@
4546
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
4647
import org.schabi.newpipe.fragments.MainFragment;
4748
import org.schabi.newpipe.fragments.list.playlist.PlaylistControlViewHolder;
48-
import org.schabi.newpipe.info_list.dialog.InfoItemDialog;
49-
import org.schabi.newpipe.info_list.dialog.StreamDialogDefaultEntry;
5049
import org.schabi.newpipe.local.BaseLocalListFragment;
5150
import org.schabi.newpipe.local.history.HistoryRecordManager;
5251
import org.schabi.newpipe.player.playqueue.PlayQueue;
5352
import org.schabi.newpipe.player.playqueue.SinglePlayQueue;
53+
import org.schabi.newpipe.ui.components.menu.LongPressAction;
54+
import org.schabi.newpipe.ui.components.menu.LongPressable;
5455
import org.schabi.newpipe.util.Localization;
5556
import org.schabi.newpipe.util.NavigationHelper;
5657
import org.schabi.newpipe.util.OnClickGesture;
@@ -786,39 +787,16 @@ private PlayQueue getPlayQueueStartingAt(final PlaylistStreamEntry infoItem) {
786787
}
787788

788789
protected void showInfoItemDialog(final PlaylistStreamEntry item) {
789-
final StreamInfoItem infoItem = item.toStreamInfoItem();
790-
791-
try {
792-
final Context context = getContext();
793-
final InfoItemDialog.Builder dialogBuilder =
794-
new InfoItemDialog.Builder(getActivity(), context, this, infoItem);
795-
796-
// add entries in the middle
797-
dialogBuilder.addAllEntries(
798-
StreamDialogDefaultEntry.SET_AS_PLAYLIST_THUMBNAIL,
799-
StreamDialogDefaultEntry.DELETE
800-
);
801-
802-
// set custom actions
803-
// all entries modified below have already been added within the builder
804-
dialogBuilder
805-
.setAction(
806-
StreamDialogDefaultEntry.START_HERE_ON_BACKGROUND,
807-
(f, i) -> NavigationHelper.playOnBackgroundPlayer(
808-
context, getPlayQueueStartingAt(item), true))
809-
.setAction(
810-
StreamDialogDefaultEntry.SET_AS_PLAYLIST_THUMBNAIL,
811-
(f, i) ->
812-
changeThumbnailStreamId(item.getStreamEntity().getUid(),
813-
true))
814-
.setAction(
815-
StreamDialogDefaultEntry.DELETE,
816-
(f, i) -> deleteItem(item))
817-
.create()
818-
.show();
819-
} catch (final IllegalArgumentException e) {
820-
InfoItemDialog.Builder.reportErrorDuringInitialization(e, infoItem);
821-
}
790+
openLongPressMenuInActivity(
791+
requireActivity(),
792+
LongPressable.fromStreamEntity(item.getStreamEntity()),
793+
// TODO getPlayQueueStartingAt(), resumePlayback=true
794+
LongPressAction.fromPlaylistStreamEntry(
795+
item,
796+
() -> deleteItem(item),
797+
() -> changeThumbnailStreamId(item.getStreamEntity().getUid(), true)
798+
)
799+
);
822800
}
823801

824802
private void setInitialData(final long pid, final String title) {

app/src/main/java/org/schabi/newpipe/ui/components/items/ItemList.kt

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,7 @@ import androidx.compose.foundation.lazy.LazyListScope
55
import androidx.compose.foundation.lazy.rememberLazyListState
66
import androidx.compose.material3.adaptive.currentWindowAdaptiveInfo
77
import androidx.compose.runtime.Composable
8-
import androidx.compose.runtime.getValue
9-
import androidx.compose.runtime.mutableStateOf
108
import androidx.compose.runtime.remember
11-
import androidx.compose.runtime.setValue
129
import androidx.compose.ui.Modifier
1310
import androidx.compose.ui.input.nestedscroll.nestedScroll
1411
import androidx.compose.ui.platform.LocalContext
@@ -50,20 +47,6 @@ fun ItemList(
5047
}
5148
}
5249

53-
// Handle long clicks for stream items
54-
// TODO: Adjust the menu display depending on where it was triggered
55-
var selectedStream by remember { mutableStateOf<StreamInfoItem?>(null) }
56-
val onLongClick = remember {
57-
{ stream: StreamInfoItem ->
58-
selectedStream = stream
59-
}
60-
}
61-
val onDismissPopup = remember {
62-
{
63-
selectedStream = null
64-
}
65-
}
66-
6750
val showProgress = DependentPreferenceHelper.getPositionsInListsEnabled(context)
6851
val nestedScrollModifier = Modifier.nestedScroll(rememberNestedScrollInteropConnection())
6952

@@ -80,10 +63,7 @@ fun ItemList(
8063
val item = items[it]
8164

8265
if (item is StreamInfoItem) {
83-
val isSelected = selectedStream == item
84-
StreamListItem(
85-
item, showProgress, isSelected, onClick, onLongClick, onDismissPopup
86-
)
66+
StreamListItem(item, showProgress, onClick)
8767
} else if (item is PlaylistInfoItem) {
8868
PlaylistListItem(item, onClick)
8969
}

0 commit comments

Comments
 (0)