Skip to content

Commit 0830095

Browse files
committed
Add Subscribe button to LongPressMenu
1 parent 920d630 commit 0830095

6 files changed

Lines changed: 50 additions & 19 deletions

File tree

app/src/main/java/org/schabi/newpipe/database/subscription/SubscriptionEntity.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,5 +83,17 @@ data class SubscriptionEntity(
8383
subscriberCount = info.subscriberCount
8484
)
8585
}
86+
87+
@Ignore
88+
fun from(info: ChannelInfoItem): SubscriptionEntity {
89+
return SubscriptionEntity(
90+
serviceId = info.serviceId,
91+
url = info.url,
92+
name = info.name,
93+
avatarUrl = ImageStrategy.imageListToDbUrl(info.thumbnails),
94+
description = info.description,
95+
subscriberCount = info.subscriberCount
96+
)
97+
}
8698
}
8799
}

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

Lines changed: 7 additions & 3 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.local.subscription.SubscriptionManager;
3334
import org.schabi.newpipe.player.playqueue.PlayQueue;
3435
import org.schabi.newpipe.ui.components.menu.LongPressAction;
3536
import org.schabi.newpipe.ui.components.menu.LongPressable;
@@ -293,11 +294,14 @@ public void selected(final ChannelInfoItem selectedItem) {
293294
}
294295

295296
@Override
296-
public void held(final ChannelInfoItem selectedItem) {
297+
public void held(final ChannelInfoItem item) {
298+
final boolean isSubscribed = new SubscriptionManager(requireContext())
299+
.blockingIsSubscribed(item.getServiceId(), item.getUrl());
300+
297301
openLongPressMenuInActivity(
298302
requireActivity(),
299-
LongPressable.fromChannelInfoItem(selectedItem),
300-
LongPressAction.fromChannelInfoItem(selectedItem, false)
303+
LongPressable.fromChannelInfoItem(item),
304+
LongPressAction.fromChannelInfoItem(item, isSubscribed)
301305
);
302306
}
303307
});

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -361,13 +361,7 @@ private Consumer<List<SubscriptionEntity>> getSubscribeUpdateMonitor(final Chann
361361
if (DEBUG) {
362362
Log.d(TAG, "No subscription to this channel!");
363363
}
364-
final SubscriptionEntity channel = new SubscriptionEntity();
365-
channel.setServiceId(info.getServiceId());
366-
channel.setUrl(info.getUrl());
367-
channel.setName(info.getName());
368-
channel.setAvatarUrl(ImageStrategy.imageListToDbUrl(info.getAvatars()));
369-
channel.setDescription(info.getDescription());
370-
channel.setSubscriberCount(info.getSubscriberCount());
364+
final SubscriptionEntity channel = SubscriptionEntity.from(info);
371365
channelSubscription = null;
372366
updateNotifyButton(null);
373367
subscribeButtonMonitor = monitorSubscribeButton(mapOnSubscribe(channel));

app/src/main/java/org/schabi/newpipe/local/subscription/SubscriptionManager.kt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ class SubscriptionManager(context: Context) {
119119
subscriptionTable.delete(subscriptionEntity)
120120
}
121121

122+
fun blockingIsSubscribed(serviceId: Int, url: String): Boolean {
123+
return !subscriptionTable.getSubscription(serviceId, url).isEmpty.blockingGet()
124+
}
125+
122126
/**
123127
* Fetches the list of videos for the provided channel and saves them in the database, so that
124128
* they will be considered as "old"/"already seen" streams and the user will never be notified

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.annotation.StringRes
88
import androidx.appcompat.app.AlertDialog
99
import androidx.compose.material.icons.Icons
1010
import androidx.compose.material.icons.automirrored.filled.PlaylistAdd
11+
import androidx.compose.material.icons.filled.AddCircle
1112
import androidx.compose.material.icons.filled.AddToQueue
1213
import androidx.compose.material.icons.filled.Cast
1314
import androidx.compose.material.icons.filled.Delete
@@ -23,6 +24,7 @@ import androidx.compose.material.icons.filled.Person
2324
import androidx.compose.material.icons.filled.PictureInPicture
2425
import androidx.compose.material.icons.filled.PlayArrow
2526
import androidx.compose.material.icons.filled.QueuePlayNext
27+
import androidx.compose.material.icons.filled.RemoveCircle
2628
import androidx.compose.material.icons.filled.Share
2729
import androidx.compose.ui.graphics.vector.ImageVector
2830
import androidx.core.net.toUri
@@ -40,6 +42,7 @@ import org.schabi.newpipe.database.playlist.PlaylistStreamEntry
4042
import org.schabi.newpipe.database.playlist.model.PlaylistRemoteEntity
4143
import org.schabi.newpipe.database.stream.StreamStatisticsEntry
4244
import org.schabi.newpipe.database.stream.model.StreamEntity
45+
import org.schabi.newpipe.database.subscription.SubscriptionEntity
4346
import org.schabi.newpipe.databinding.DialogEditTextBinding
4447
import org.schabi.newpipe.download.DownloadDialog
4548
import org.schabi.newpipe.extractor.InfoItem
@@ -105,9 +108,10 @@ data class LongPressAction(
105108
Rename(19, R.string.rename, Icons.Default.Edit),
106109
SetAsPlaylistThumbnail(20, R.string.set_as_playlist_thumbnail, Icons.Default.Image),
107110
UnsetPlaylistThumbnail(21, R.string.unset_playlist_thumbnail, Icons.Default.HideImage),
108-
Delete(22, R.string.delete, Icons.Default.Delete),
109-
Unsubscribe(23, R.string.unsubscribe, Icons.Default.Delete),
110-
Remove(24, R.string.play_queue_remove, Icons.Default.Delete);
111+
Subscribe(22, R.string.subscribe_button_title, Icons.Default.AddCircle),
112+
Unsubscribe(23, R.string.unsubscribe, Icons.Default.RemoveCircle),
113+
Delete(24, R.string.delete, Icons.Default.Delete),
114+
Remove(25, R.string.play_queue_remove, Icons.Default.Delete);
111115

112116
fun buildAction(
113117
enabled: () -> Boolean = { true },
@@ -465,7 +469,7 @@ data class LongPressAction(
465469
@JvmStatic
466470
fun fromChannelInfoItem(
467471
item: ChannelInfoItem,
468-
showUnsubscribe: Boolean
472+
isSubscribed: Boolean
469473
): List<LongPressAction> {
470474
return buildPlayerActionList { ChannelTabPlayQueue(item.serviceId, item.url) } +
471475
buildPlayerShuffledActionList { ChannelTabPlayQueue(item.serviceId, item.url) } +
@@ -480,7 +484,7 @@ data class LongPressAction(
480484
)
481485
}
482486
) +
483-
if (showUnsubscribe) {
487+
if (isSubscribed) {
484488
listOf(
485489
Type.Unsubscribe.buildAction { context ->
486490
withContext(Dispatchers.IO) {
@@ -496,7 +500,19 @@ data class LongPressAction(
496500
}
497501
)
498502
} else {
499-
listOf()
503+
listOf(
504+
Type.Subscribe.buildAction { context ->
505+
withContext(Dispatchers.IO) {
506+
SubscriptionManager(context)
507+
.insertSubscription(SubscriptionEntity.from(item))
508+
}
509+
Toast.makeText(
510+
context,
511+
context.getString(R.string.subscribed_button_title),
512+
Toast.LENGTH_SHORT
513+
).show()
514+
}
515+
)
500516
}
501517
}
502518

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Rename
2222
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.SetAsPlaylistThumbnail
2323
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Share
2424
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.ShowDetails
25+
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Subscribe
2526
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.UnsetPlaylistThumbnail
2627
import org.schabi.newpipe.ui.components.menu.LongPressAction.Type.Unsubscribe
2728

@@ -43,9 +44,9 @@ fun storeIsHeaderEnabledToSettings(context: Context, enabled: Boolean) {
4344
// also be done by clicking on the uploader name in the long press menu header.
4445
// PlayWithKodi is only added by default if it is enabled in settings.
4546
private val DefaultEnabledActions: List<LongPressAction.Type> = listOf(
46-
ShowDetails, Enqueue, EnqueueNext, Background, Popup, BackgroundFromHere,
47-
BackgroundShuffled, Download, AddToPlaylist, Share, OpenInBrowser, MarkAsWatched,
48-
Rename, SetAsPlaylistThumbnail, UnsetPlaylistThumbnail, Delete, Unsubscribe, Remove
47+
ShowDetails, Enqueue, EnqueueNext, Background, Popup, BackgroundFromHere, BackgroundShuffled,
48+
Download, AddToPlaylist, Share, OpenInBrowser, MarkAsWatched, Rename, SetAsPlaylistThumbnail,
49+
UnsetPlaylistThumbnail, Subscribe, Unsubscribe, Delete, Remove
4950
)
5051

5152
private fun getShowPlayWithKodi(context: Context): Boolean {

0 commit comments

Comments
 (0)