Skip to content

Commit 2cf03b6

Browse files
committed
Use LongPressMenu in BookmarkFragment (wip)
1 parent de44a78 commit 2cf03b6

3 files changed

Lines changed: 106 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.getName(), item);
496+
private void showRemoteDialog(final PlaylistRemoteEntity item) {
497+
openLongPressMenuInActivity(
498+
requireActivity(),
499+
LongPressable.fromPlaylistRemoteEntity(item),
500+
LongPressAction.fromPlaylistRemoteEntity(
501+
item,
502+
() -> showDeleteDialog(item.getName(), 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.name, 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.name, 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: 47 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
@@ -59,7 +63,9 @@ data class LongPressAction(
5963
ShowChannelDetails(R.string.show_channel_details, Icons.Default.Person),
6064
MarkAsWatched(R.string.mark_as_watched, Icons.Default.Done),
6165
Delete(R.string.delete, Icons.Default.Delete),
62-
SetAsPlaylistThumbnail(R.string.set_as_playlist_thumbnail, Icons.Default.Panorama),
66+
Rename(R.string.rename, Icons.Default.Edit),
67+
SetAsPlaylistThumbnail(R.string.set_as_playlist_thumbnail, Icons.Default.Image),
68+
UnsetPlaylistThumbnail(R.string.unset_playlist_thumbnail, Icons.Default.HideImage),
6369
;
6470

6571
// TODO allow actions to return disposables
@@ -103,6 +109,17 @@ data class LongPressAction(
103109
)
104110
}
105111

112+
private fun buildShareActionList(name: String, url: String, thumbnailUrl: String?): List<LongPressAction> {
113+
return listOf(
114+
Type.Share.buildAction { context ->
115+
ShareUtils.shareText(context, name, url, thumbnailUrl)
116+
},
117+
Type.OpenInBrowser.buildAction { context ->
118+
ShareUtils.openUrlInBrowser(context, url)
119+
},
120+
)
121+
}
122+
106123
@JvmStatic
107124
fun fromStreamInfoItem(
108125
item: StreamInfoItem,
@@ -196,6 +213,7 @@ data class LongPressAction(
196213
@JvmStatic
197214
fun fromPlaylistStreamEntry(
198215
item: PlaylistStreamEntry,
216+
// TODO possibly embed these two actions here
199217
onDelete: Runnable,
200218
onSetAsPlaylistThumbnail: Runnable,
201219
): List<LongPressAction> {
@@ -205,5 +223,32 @@ data class LongPressAction(
205223
Type.SetAsPlaylistThumbnail.buildAction { onSetAsPlaylistThumbnail.run() }
206224
)
207225
}
226+
227+
@JvmStatic
228+
fun fromPlaylistMetadataEntry(
229+
item: PlaylistMetadataEntry,
230+
onRename: Runnable,
231+
onDelete: Runnable,
232+
unsetPlaylistThumbnail: Runnable?,
233+
): List<LongPressAction> {
234+
return listOf(
235+
Type.Rename.buildAction { onRename.run() },
236+
Type.Delete.buildAction { onDelete.run() },
237+
Type.UnsetPlaylistThumbnail.buildAction(
238+
enabled = { unsetPlaylistThumbnail != null }
239+
) { unsetPlaylistThumbnail?.run() }
240+
)
241+
}
242+
243+
@JvmStatic
244+
fun fromPlaylistRemoteEntity(
245+
item: PlaylistRemoteEntity,
246+
onDelete: Runnable,
247+
): List<LongPressAction> {
248+
return buildShareActionList(item.name, item.url, item.thumbnailUrl) +
249+
listOf(
250+
Type.Delete.buildAction { onDelete.run() },
251+
)
252+
}
208253
}
209254
}

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
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
57
import org.schabi.newpipe.extractor.stream.StreamInfoItem
68
import org.schabi.newpipe.extractor.stream.StreamType
@@ -62,5 +64,30 @@ data class LongPressable(
6264
?: item.textualUploadDate?.let { Either.left(it) },
6365
decoration = Decoration.from(item.streamType, item.duration),
6466
)
67+
68+
@JvmStatic
69+
fun fromPlaylistMetadataEntry(item: PlaylistMetadataEntry) = LongPressable(
70+
// many fields are null because this is a local playlist
71+
title = item.name,
72+
url = null,
73+
thumbnailUrl = item.thumbnailUrl,
74+
uploader = null,
75+
uploaderUrl = null,
76+
viewCount = null,
77+
uploadDate = null,
78+
decoration = Decoration.Playlist(item.streamCount),
79+
)
80+
81+
@JvmStatic
82+
fun fromPlaylistRemoteEntity(item: PlaylistRemoteEntity) = LongPressable(
83+
title = item.name,
84+
url = item.url,
85+
thumbnailUrl = item.thumbnailUrl,
86+
uploader = item.uploader,
87+
uploaderUrl = null,
88+
viewCount = null,
89+
uploadDate = null,
90+
decoration = Decoration.Playlist(item.streamCount),
91+
)
6592
}
6693
}

0 commit comments

Comments
 (0)