Skip to content

Commit 9ce22db

Browse files
committed
Use LongPressMenu in BookmarkFragment
1 parent 337be32 commit 9ce22db

3 files changed

Lines changed: 113 additions & 34 deletions

File tree

app/src/main/java/org/schabi/newpipe/local/bookmark/BookmarkFragment.java

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.schabi.newpipe.local.bookmark;
22

33
import static org.schabi.newpipe.local.bookmark.MergedPlaylistManager.getMergedOrderedPlaylists;
4+
import static org.schabi.newpipe.ui.components.menu.LongPressMenuKt.openLongPressMenuInActivity;
45

5-
import android.content.DialogInterface;
66
import android.os.Bundle;
77
import android.os.Parcelable;
88
import android.text.InputType;
@@ -39,6 +39,8 @@
3939
import org.schabi.newpipe.local.holder.RemoteBookmarkPlaylistItemHolder;
4040
import org.schabi.newpipe.local.playlist.LocalPlaylistManager;
4141
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
42+
import org.schabi.newpipe.ui.components.menu.LongPressAction;
43+
import org.schabi.newpipe.ui.components.menu.LongPressable;
4244
import org.schabi.newpipe.ui.emptystate.EmptyStateSpec;
4345
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
4446
import org.schabi.newpipe.util.NavigationHelper;
@@ -162,7 +164,7 @@ public void held(final LocalItem selectedItem) {
162164
if (selectedItem instanceof PlaylistMetadataEntry) {
163165
showLocalDialog((PlaylistMetadataEntry) selectedItem);
164166
} else if (selectedItem instanceof PlaylistRemoteEntity) {
165-
showRemoteDeleteDialog((PlaylistRemoteEntity) selectedItem);
167+
showRemoteDialog((PlaylistRemoteEntity) selectedItem);
166168
}
167169
}
168170

@@ -491,42 +493,31 @@ public void onSwiped(@NonNull final RecyclerView.ViewHolder viewHolder,
491493
// Utils
492494
///////////////////////////////////////////////////////////////////////////
493495

494-
private void showRemoteDeleteDialog(final PlaylistRemoteEntity item) {
495-
showDeleteDialog(item.getOrderingName(), item);
496+
private void showRemoteDialog(final PlaylistRemoteEntity item) {
497+
openLongPressMenuInActivity(
498+
requireActivity(),
499+
LongPressable.fromPlaylistRemoteEntity(item),
500+
LongPressAction.fromPlaylistRemoteEntity(
501+
item,
502+
() -> showDeleteDialog(item.getOrderingName(), item)
503+
)
504+
);
496505
}
497506

498507
private void showLocalDialog(final PlaylistMetadataEntry selectedItem) {
499-
final String rename = getString(R.string.rename);
500-
final String delete = getString(R.string.delete);
501-
final String unsetThumbnail = getString(R.string.unset_playlist_thumbnail);
502508
final boolean isThumbnailPermanent = localPlaylistManager
503509
.getIsPlaylistThumbnailPermanent(selectedItem.getUid());
504510

505-
final ArrayList<String> items = new ArrayList<>();
506-
items.add(rename);
507-
items.add(delete);
508-
if (isThumbnailPermanent) {
509-
items.add(unsetThumbnail);
510-
}
511-
512-
final DialogInterface.OnClickListener action = (d, index) -> {
513-
if (items.get(index).equals(rename)) {
514-
showRenameDialog(selectedItem);
515-
} else if (items.get(index).equals(delete)) {
516-
showDeleteDialog(selectedItem.getOrderingName(), selectedItem);
517-
} else if (isThumbnailPermanent && items.get(index).equals(unsetThumbnail)) {
518-
final long thumbnailStreamId = localPlaylistManager
519-
.getAutomaticPlaylistThumbnailStreamId(selectedItem.getUid());
520-
localPlaylistManager
521-
.changePlaylistThumbnail(selectedItem.getUid(), thumbnailStreamId, false)
522-
.observeOn(AndroidSchedulers.mainThread())
523-
.subscribe();
524-
}
525-
};
526-
527-
new AlertDialog.Builder(activity)
528-
.setItems(items.toArray(new String[0]), action)
529-
.show();
511+
openLongPressMenuInActivity(
512+
requireActivity(),
513+
LongPressable.fromPlaylistMetadataEntry(selectedItem),
514+
LongPressAction.fromPlaylistMetadataEntry(
515+
selectedItem,
516+
() -> showRenameDialog(selectedItem),
517+
() -> showDeleteDialog(selectedItem.getOrderingName(), selectedItem),
518+
isThumbnailPermanent ? () -> unsetPermanentThumbnail(selectedItem) : null
519+
)
520+
);
530521
}
531522

532523
private void showRenameDialog(final PlaylistMetadataEntry selectedItem) {
@@ -559,4 +550,13 @@ private void showDeleteDialog(final String name, final PlaylistLocalItem item) {
559550
.setNegativeButton(R.string.cancel, null)
560551
.show();
561552
}
553+
554+
private void unsetPermanentThumbnail(final PlaylistMetadataEntry item) {
555+
final long thumbnailStreamId = localPlaylistManager
556+
.getAutomaticPlaylistThumbnailStreamId(item.getUid());
557+
localPlaylistManager
558+
.changePlaylistThumbnail(item.getUid(), thumbnailStreamId, false)
559+
.observeOn(AndroidSchedulers.mainThread())
560+
.subscribe();
561+
}
562562
}

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

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ import androidx.compose.material.icons.filled.Cast
1010
import androidx.compose.material.icons.filled.Delete
1111
import androidx.compose.material.icons.filled.Done
1212
import androidx.compose.material.icons.filled.Download
13+
import androidx.compose.material.icons.filled.Edit
1314
import androidx.compose.material.icons.filled.Headset
15+
import androidx.compose.material.icons.filled.HideImage
16+
import androidx.compose.material.icons.filled.Image
1417
import androidx.compose.material.icons.filled.OpenInBrowser
15-
import androidx.compose.material.icons.filled.Panorama
1618
import androidx.compose.material.icons.filled.Person
1719
import androidx.compose.material.icons.filled.PictureInPicture
1820
import androidx.compose.material.icons.filled.PlayArrow
@@ -21,7 +23,9 @@ import androidx.compose.material.icons.filled.Share
2123
import androidx.compose.ui.graphics.vector.ImageVector
2224
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
2325
import org.schabi.newpipe.R
26+
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry
2427
import org.schabi.newpipe.database.playlist.PlaylistStreamEntry
28+
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity
2529
import org.schabi.newpipe.database.stream.StreamStatisticsEntry
2630
import org.schabi.newpipe.database.stream.model.StreamEntity
2731
import org.schabi.newpipe.download.DownloadDialog
@@ -62,7 +66,9 @@ data class LongPressAction(
6266
ShowChannelDetails(R.string.show_channel_details, Icons.Default.Person),
6367
MarkAsWatched(R.string.mark_as_watched, Icons.Default.Done),
6468
Delete(R.string.delete, Icons.Default.Delete),
65-
SetAsPlaylistThumbnail(R.string.set_as_playlist_thumbnail, Icons.Default.Panorama),
69+
Rename(R.string.rename, Icons.Default.Edit),
70+
SetAsPlaylistThumbnail(R.string.set_as_playlist_thumbnail, Icons.Default.Image),
71+
UnsetPlaylistThumbnail(R.string.unset_playlist_thumbnail, Icons.Default.HideImage),
6672
;
6773

6874
// TODO allow actions to return disposables
@@ -106,6 +112,17 @@ data class LongPressAction(
106112
)
107113
}
108114

115+
private fun buildShareActionList(name: String, url: String, thumbnailUrl: String?): List<LongPressAction> {
116+
return listOf(
117+
Type.Share.buildAction { context ->
118+
ShareUtils.shareText(context, name, url, thumbnailUrl)
119+
},
120+
Type.OpenInBrowser.buildAction { context ->
121+
ShareUtils.openUrlInBrowser(context, url)
122+
},
123+
)
124+
}
125+
109126
@JvmStatic
110127
fun fromStreamInfoItem(
111128
item: StreamInfoItem,
@@ -209,6 +226,7 @@ data class LongPressAction(
209226
@JvmStatic
210227
fun fromPlaylistStreamEntry(
211228
item: PlaylistStreamEntry,
229+
// TODO possibly embed these two actions here
212230
onDelete: Runnable,
213231
onSetAsPlaylistThumbnail: Runnable,
214232
): List<LongPressAction> {
@@ -218,5 +236,36 @@ data class LongPressAction(
218236
Type.SetAsPlaylistThumbnail.buildAction { onSetAsPlaylistThumbnail.run() }
219237
)
220238
}
239+
240+
@JvmStatic
241+
fun fromPlaylistMetadataEntry(
242+
item: PlaylistMetadataEntry,
243+
onRename: Runnable,
244+
onDelete: Runnable,
245+
unsetPlaylistThumbnail: Runnable?,
246+
): List<LongPressAction> {
247+
return listOf(
248+
Type.Rename.buildAction { onRename.run() },
249+
Type.Delete.buildAction { onDelete.run() },
250+
Type.UnsetPlaylistThumbnail.buildAction(
251+
enabled = { unsetPlaylistThumbnail != null }
252+
) { unsetPlaylistThumbnail?.run() }
253+
)
254+
}
255+
256+
@JvmStatic
257+
fun fromPlaylistRemoteEntity(
258+
item: PlaylistRemoteEntity,
259+
onDelete: Runnable,
260+
): List<LongPressAction> {
261+
return buildShareActionList(
262+
item.orderingName ?: "",
263+
item.url ?: "",
264+
item.thumbnailUrl
265+
) +
266+
listOf(
267+
Type.Delete.buildAction { onDelete.run() },
268+
)
269+
}
221270
}
222271
}

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package org.schabi.newpipe.ui.components.menu
22

33
import androidx.compose.runtime.Stable
4+
import org.schabi.newpipe.database.playlist.PlaylistMetadataEntry
5+
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity
46
import org.schabi.newpipe.database.stream.model.StreamEntity
7+
import org.schabi.newpipe.extractor.ListExtractor
58
import org.schabi.newpipe.extractor.stream.StreamInfoItem
69
import org.schabi.newpipe.extractor.stream.StreamType
710
import org.schabi.newpipe.extractor.stream.StreamType.AUDIO_LIVE_STREAM
@@ -62,5 +65,32 @@ data class LongPressable(
6265
?: item.textualUploadDate?.let { Either.left(it) },
6366
decoration = Decoration.from(item.streamType, item.duration),
6467
)
68+
69+
@JvmStatic
70+
fun fromPlaylistMetadataEntry(item: PlaylistMetadataEntry) = LongPressable(
71+
// many fields are null because this is a local playlist
72+
title = item.orderingName ?: "",
73+
url = null,
74+
thumbnailUrl = item.thumbnailUrl,
75+
uploader = null,
76+
uploaderUrl = null,
77+
viewCount = null,
78+
uploadDate = null,
79+
decoration = Decoration.Playlist(item.streamCount),
80+
)
81+
82+
@JvmStatic
83+
fun fromPlaylistRemoteEntity(item: PlaylistRemoteEntity) = LongPressable(
84+
title = item.orderingName ?: "",
85+
url = item.url,
86+
thumbnailUrl = item.thumbnailUrl,
87+
uploader = item.uploader,
88+
uploaderUrl = null,
89+
viewCount = null,
90+
uploadDate = null,
91+
decoration = Decoration.Playlist(
92+
item.streamCount ?: ListExtractor.ITEM_COUNT_UNKNOWN
93+
),
94+
)
6595
}
6696
}

0 commit comments

Comments
 (0)