Skip to content

Commit 05cb424

Browse files
committed
Use LongPressMenu in BookmarkFragment (wip)
1 parent 4bf32f9 commit 05cb424

File tree

3 files changed

+106
-34
lines changed

3 files changed

+106
-34
lines changed

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;
@@ -38,6 +38,8 @@
3838
import org.schabi.newpipe.local.holder.RemoteBookmarkPlaylistItemHolder;
3939
import org.schabi.newpipe.local.playlist.LocalPlaylistManager;
4040
import org.schabi.newpipe.local.playlist.RemotePlaylistManager;
41+
import org.schabi.newpipe.ui.components.menu.LongPressAction;
42+
import org.schabi.newpipe.ui.components.menu.LongPressable;
4143
import org.schabi.newpipe.ui.emptystate.EmptyStateSpec;
4244
import org.schabi.newpipe.ui.emptystate.EmptyStateUtil;
4345
import org.schabi.newpipe.util.NavigationHelper;
@@ -163,7 +165,7 @@ public void held(final LocalItem selectedItem) {
163165
if (selectedItem instanceof PlaylistMetadataEntry) {
164166
showLocalDialog((PlaylistMetadataEntry) selectedItem);
165167
} else if (selectedItem instanceof PlaylistRemoteEntity) {
166-
showRemoteDeleteDialog((PlaylistRemoteEntity) selectedItem);
168+
showRemoteDialog((PlaylistRemoteEntity) selectedItem);
167169
}
168170
}
169171

@@ -492,42 +494,31 @@ public void onSwiped(@NonNull final RecyclerView.ViewHolder viewHolder,
492494
// Utils
493495
///////////////////////////////////////////////////////////////////////////
494496

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

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

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

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

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)