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()
|
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 unsubscribe(channelId: String) = subscriptionsRepository.unsubscribe(channelId)
|
||||||
suspend fun isSubscribed(channelId: String) = subscriptionsRepository.isSubscribed(channelId)
|
suspend fun isSubscribed(channelId: String) = subscriptionsRepository.isSubscribed(channelId)
|
||||||
suspend fun importSubscriptions(newChannels: List<String>) =
|
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.extensions.toID
|
||||||
import com.github.libretube.helpers.PreferenceHelper
|
import com.github.libretube.helpers.PreferenceHelper
|
||||||
|
|
||||||
class AccountSubscriptionsRepository: SubscriptionsRepository {
|
class AccountSubscriptionsRepository : SubscriptionsRepository {
|
||||||
private val token get() = PreferenceHelper.getToken()
|
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 {
|
runCatching {
|
||||||
RetrofitInstance.authApi.subscribe(token, Subscribe(channelId))
|
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 com.github.libretube.ui.dialogs.ShareDialog.Companion.YOUTUBE_FRONTEND_URL
|
||||||
import org.schabi.newpipe.extractor.channel.ChannelInfo
|
import org.schabi.newpipe.extractor.channel.ChannelInfo
|
||||||
|
|
||||||
class LocalSubscriptionsRepository: SubscriptionsRepository {
|
class LocalSubscriptionsRepository : SubscriptionsRepository {
|
||||||
override suspend fun subscribe(channelId: String) {
|
override suspend fun subscribe(
|
||||||
val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}"
|
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
|
||||||
val channelInfo = ChannelInfo.getInfo(channelUrl)
|
) {
|
||||||
|
|
||||||
val localSubscription = LocalSubscription(
|
val localSubscription = LocalSubscription(
|
||||||
channelId = channelInfo.id,
|
channelId = channelId,
|
||||||
name = channelInfo.name,
|
name = name,
|
||||||
avatar = channelInfo.avatars.maxByOrNull { it.height }?.url,
|
avatar = uploaderAvatar,
|
||||||
verified = channelInfo.isVerified
|
verified = verified
|
||||||
)
|
)
|
||||||
|
|
||||||
Database.localSubscriptionDao().insert(localSubscription)
|
Database.localSubscriptionDao().insert(localSubscription)
|
||||||
@ -33,7 +32,10 @@ class LocalSubscriptionsRepository: SubscriptionsRepository {
|
|||||||
override suspend fun importSubscriptions(newChannels: List<String>) {
|
override suspend fun importSubscriptions(newChannels: List<String>) {
|
||||||
for (chunk in newChannels.chunked(CHANNEL_CHUNK_SIZE)) {
|
for (chunk in newChannels.chunked(CHANNEL_CHUNK_SIZE)) {
|
||||||
chunk.parallelMap { channelId ->
|
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.DatabaseHolder.Database
|
||||||
import com.github.libretube.db.obj.LocalSubscription
|
import com.github.libretube.db.obj.LocalSubscription
|
||||||
|
|
||||||
class PipedLocalSubscriptionsRepository: SubscriptionsRepository {
|
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
|
// further meta info is not needed when using Piped local subscriptions
|
||||||
Database.localSubscriptionDao().insert(LocalSubscription(channelId))
|
Database.localSubscriptionDao().insert(LocalSubscription(channelId))
|
||||||
}
|
}
|
||||||
@ -29,9 +31,9 @@ class PipedLocalSubscriptionsRepository: SubscriptionsRepository {
|
|||||||
val channelIds = getSubscriptionChannelIds()
|
val channelIds = getSubscriptionChannelIds()
|
||||||
|
|
||||||
return when {
|
return when {
|
||||||
channelIds.size > GET_SUBSCRIPTIONS_LIMIT ->
|
channelIds.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
|
||||||
RetrofitInstance.authApi
|
channelIds
|
||||||
.unauthenticatedSubscriptions(channelIds)
|
)
|
||||||
|
|
||||||
else -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
|
else -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
|
||||||
channelIds.joinToString(",")
|
channelIds.joinToString(",")
|
||||||
|
@ -3,7 +3,7 @@ package com.github.libretube.repo
|
|||||||
import com.github.libretube.api.obj.Subscription
|
import com.github.libretube.api.obj.Subscription
|
||||||
|
|
||||||
interface SubscriptionsRepository {
|
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 unsubscribe(channelId: String)
|
||||||
suspend fun isSubscribed(channelId: String): Boolean?
|
suspend fun isSubscribed(channelId: String): Boolean?
|
||||||
suspend fun importSubscriptions(newChannels: List<String>)
|
suspend fun importSubscriptions(newChannels: List<String>)
|
||||||
|
@ -159,7 +159,12 @@ class SearchResultsAdapter(
|
|||||||
}
|
}
|
||||||
|
|
||||||
var subscribed = false
|
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
|
subscribed = it
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -52,6 +52,8 @@ class SubscriptionChannelAdapter :
|
|||||||
subscriptionSubscribe.setupSubscriptionButton(
|
subscriptionSubscribe.setupSubscriptionButton(
|
||||||
subscription.url.toID(),
|
subscription.url.toID(),
|
||||||
subscription.name,
|
subscription.name,
|
||||||
|
subscription.avatar,
|
||||||
|
subscription.verified,
|
||||||
notificationBell,
|
notificationBell,
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
|
@ -15,7 +15,9 @@ import kotlinx.coroutines.withContext
|
|||||||
|
|
||||||
fun TextView.setupSubscriptionButton(
|
fun TextView.setupSubscriptionButton(
|
||||||
channelId: String?,
|
channelId: String?,
|
||||||
channelName: String?,
|
channelName: String,
|
||||||
|
channelAvatar: String?,
|
||||||
|
channelVerified: Boolean,
|
||||||
notificationBell: MaterialButton? = null,
|
notificationBell: MaterialButton? = null,
|
||||||
isSubscribed: Boolean? = null,
|
isSubscribed: Boolean? = null,
|
||||||
onIsSubscribedChange: (Boolean) -> Unit = {}
|
onIsSubscribedChange: (Boolean) -> Unit = {}
|
||||||
@ -53,7 +55,7 @@ fun TextView.setupSubscriptionButton(
|
|||||||
} else {
|
} else {
|
||||||
CoroutineScope(Dispatchers.Main).launch {
|
CoroutineScope(Dispatchers.Main).launch {
|
||||||
withContext(Dispatchers.IO) {
|
withContext(Dispatchers.IO) {
|
||||||
SubscriptionHelper.subscribe(channelId)
|
SubscriptionHelper.subscribe(channelId, channelName, channelAvatar, channelVerified)
|
||||||
}
|
}
|
||||||
|
|
||||||
text = context.getString(R.string.unsubscribe)
|
text = context.getString(R.string.unsubscribe)
|
||||||
|
@ -150,7 +150,9 @@ class ChannelFragment : DynamicLayoutManagerFragment(R.layout.fragment_channel)
|
|||||||
|
|
||||||
binding.channelSubscribe.setupSubscriptionButton(
|
binding.channelSubscribe.setupSubscriptionButton(
|
||||||
channelId,
|
channelId,
|
||||||
channelName,
|
response.name.orEmpty(),
|
||||||
|
response.avatarUrl,
|
||||||
|
response.verified,
|
||||||
binding.notificationBell
|
binding.notificationBell
|
||||||
) { isSubscribed ->
|
) { isSubscribed ->
|
||||||
_binding?.addToGroup?.isVisible = isSubscribed
|
_binding?.addToGroup?.isVisible = isSubscribed
|
||||||
|
@ -1147,8 +1147,10 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
|
|||||||
|
|
||||||
// update the subscribed state
|
// update the subscribed state
|
||||||
binding.playerSubscribe.setupSubscriptionButton(
|
binding.playerSubscribe.setupSubscriptionButton(
|
||||||
this.streams.uploaderUrl.toID(),
|
streams.uploaderUrl.toID(),
|
||||||
this.streams.uploader
|
streams.uploader,
|
||||||
|
streams.uploaderAvatar,
|
||||||
|
streams.uploaderVerified
|
||||||
)
|
)
|
||||||
|
|
||||||
// seekbar preview setup
|
// seekbar preview setup
|
||||||
|
Loading…
x
Reference in New Issue
Block a user