mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-27 23:40:33 +05:30
Merge pull request #7215 from Bnyro/master
refactor: remove channel info api calls when subscribing to channels locally
This commit is contained in:
commit
246e49e145
@ -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>) =
|
||||
|
@ -6,10 +6,12 @@ import com.github.libretube.api.obj.Subscription
|
||||
import com.github.libretube.extensions.toID
|
||||
import com.github.libretube.helpers.PreferenceHelper
|
||||
|
||||
class AccountSubscriptionsRepository: SubscriptionsRepository {
|
||||
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))
|
||||
}
|
||||
|
@ -7,16 +7,15 @@ import com.github.libretube.extensions.parallelMap
|
||||
import com.github.libretube.ui.dialogs.ShareDialog.Companion.YOUTUBE_FRONTEND_URL
|
||||
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)
|
||||
|
||||
class LocalSubscriptionsRepository : SubscriptionsRepository {
|
||||
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) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,8 +6,10 @@ import com.github.libretube.api.obj.Subscription
|
||||
import com.github.libretube.db.DatabaseHolder.Database
|
||||
import com.github.libretube.db.obj.LocalSubscription
|
||||
|
||||
class PipedLocalSubscriptionsRepository: SubscriptionsRepository {
|
||||
override suspend fun subscribe(channelId: String) {
|
||||
class PipedLocalSubscriptionsRepository : SubscriptionsRepository {
|
||||
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(",")
|
||||
|
@ -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>)
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -52,6 +52,8 @@ class SubscriptionChannelAdapter :
|
||||
subscriptionSubscribe.setupSubscriptionButton(
|
||||
subscription.url.toID(),
|
||||
subscription.name,
|
||||
subscription.avatar,
|
||||
subscription.verified,
|
||||
notificationBell,
|
||||
true
|
||||
)
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user