Merge pull request #7215 from Bnyro/master

refactor: remove channel info api calls when subscribing to channels locally
This commit is contained in:
Bnyro 2025-03-16 17:23:39 +01:00 committed by GitHub
commit 246e49e145
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
10 changed files with 47 additions and 25 deletions

View File

@ -43,7 +43,10 @@ object SubscriptionHelper {
else -> PipedNoAccountFeedRepository()
}
suspend fun subscribe(channelId: String) = subscriptionsRepository.subscribe(channelId)
suspend fun subscribe(
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
) = subscriptionsRepository.subscribe(channelId, name, uploaderAvatar, verified)
suspend fun unsubscribe(channelId: String) = subscriptionsRepository.unsubscribe(channelId)
suspend fun isSubscribed(channelId: String) = subscriptionsRepository.isSubscribed(channelId)
suspend fun importSubscriptions(newChannels: List<String>) =

View File

@ -9,7 +9,9 @@ import com.github.libretube.helpers.PreferenceHelper
class AccountSubscriptionsRepository : SubscriptionsRepository {
private val token get() = PreferenceHelper.getToken()
override suspend fun subscribe(channelId: String) {
override suspend fun subscribe(
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
) {
runCatching {
RetrofitInstance.authApi.subscribe(token, Subscribe(channelId))
}

View File

@ -8,15 +8,14 @@ import com.github.libretube.ui.dialogs.ShareDialog.Companion.YOUTUBE_FRONTEND_UR
import org.schabi.newpipe.extractor.channel.ChannelInfo
class LocalSubscriptionsRepository : SubscriptionsRepository {
override suspend fun subscribe(channelId: String) {
val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}"
val channelInfo = ChannelInfo.getInfo(channelUrl)
override suspend fun subscribe(
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
) {
val localSubscription = LocalSubscription(
channelId = channelInfo.id,
name = channelInfo.name,
avatar = channelInfo.avatars.maxByOrNull { it.height }?.url,
verified = channelInfo.isVerified
channelId = channelId,
name = name,
avatar = uploaderAvatar,
verified = verified
)
Database.localSubscriptionDao().insert(localSubscription)
@ -33,7 +32,10 @@ class LocalSubscriptionsRepository: SubscriptionsRepository {
override suspend fun importSubscriptions(newChannels: List<String>) {
for (chunk in newChannels.chunked(CHANNEL_CHUNK_SIZE)) {
chunk.parallelMap { channelId ->
runCatching { subscribe(channelId) }
val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}"
val channelInfo = ChannelInfo.getInfo(channelUrl)
runCatching { subscribe(channelId, channelInfo.name, channelInfo.avatars.maxByOrNull { it.height }?.url, channelInfo.isVerified) }
}
}
}

View File

@ -7,7 +7,9 @@ import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.db.obj.LocalSubscription
class PipedLocalSubscriptionsRepository : SubscriptionsRepository {
override suspend fun subscribe(channelId: String) {
override suspend fun subscribe(
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
) {
// further meta info is not needed when using Piped local subscriptions
Database.localSubscriptionDao().insert(LocalSubscription(channelId))
}
@ -29,9 +31,9 @@ class PipedLocalSubscriptionsRepository: SubscriptionsRepository {
val channelIds = getSubscriptionChannelIds()
return when {
channelIds.size > GET_SUBSCRIPTIONS_LIMIT ->
RetrofitInstance.authApi
.unauthenticatedSubscriptions(channelIds)
channelIds.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
channelIds
)
else -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
channelIds.joinToString(",")

View File

@ -3,7 +3,7 @@ package com.github.libretube.repo
import com.github.libretube.api.obj.Subscription
interface SubscriptionsRepository {
suspend fun subscribe(channelId: String)
suspend fun subscribe(channelId: String, name: String, uploaderAvatar: String?, verified: Boolean)
suspend fun unsubscribe(channelId: String)
suspend fun isSubscribed(channelId: String): Boolean?
suspend fun importSubscriptions(newChannels: List<String>)

View File

@ -159,7 +159,12 @@ class SearchResultsAdapter(
}
var subscribed = false
binding.searchSubButton.setupSubscriptionButton(item.url.toID(), item.name?.toID()) {
binding.searchSubButton.setupSubscriptionButton(
item.url.toID(),
item.name.orEmpty(),
item.uploaderAvatar,
item.uploaderVerified ?: false
) {
subscribed = it
}

View File

@ -52,6 +52,8 @@ class SubscriptionChannelAdapter :
subscriptionSubscribe.setupSubscriptionButton(
subscription.url.toID(),
subscription.name,
subscription.avatar,
subscription.verified,
notificationBell,
true
)

View File

@ -15,7 +15,9 @@ import kotlinx.coroutines.withContext
fun TextView.setupSubscriptionButton(
channelId: String?,
channelName: String?,
channelName: String,
channelAvatar: String?,
channelVerified: Boolean,
notificationBell: MaterialButton? = null,
isSubscribed: Boolean? = null,
onIsSubscribedChange: (Boolean) -> Unit = {}
@ -53,7 +55,7 @@ fun TextView.setupSubscriptionButton(
} else {
CoroutineScope(Dispatchers.Main).launch {
withContext(Dispatchers.IO) {
SubscriptionHelper.subscribe(channelId)
SubscriptionHelper.subscribe(channelId, channelName, channelAvatar, channelVerified)
}
text = context.getString(R.string.unsubscribe)

View File

@ -150,7 +150,9 @@ class ChannelFragment : DynamicLayoutManagerFragment(R.layout.fragment_channel)
binding.channelSubscribe.setupSubscriptionButton(
channelId,
channelName,
response.name.orEmpty(),
response.avatarUrl,
response.verified,
binding.notificationBell
) { isSubscribed ->
_binding?.addToGroup?.isVisible = isSubscribed

View File

@ -1147,8 +1147,10 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
// update the subscribed state
binding.playerSubscribe.setupSubscriptionButton(
this.streams.uploaderUrl.toID(),
this.streams.uploader
streams.uploaderUrl.toID(),
streams.uploader,
streams.uploaderAvatar,
streams.uploaderVerified
)
// seekbar preview setup