Skip to content

Commit 246e49e

Browse files
authored
Merge pull request libre-tube#7215 from Bnyro/master
refactor: remove channel info api calls when subscribing to channels locally
2 parents 69c4ab8 + 964ba84 commit 246e49e

10 files changed

Lines changed: 47 additions & 25 deletions

app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ object SubscriptionHelper {
4343
else -> PipedNoAccountFeedRepository()
4444
}
4545

46-
suspend fun subscribe(channelId: String) = subscriptionsRepository.subscribe(channelId)
46+
suspend fun subscribe(
47+
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
48+
) = subscriptionsRepository.subscribe(channelId, name, uploaderAvatar, verified)
49+
4750
suspend fun unsubscribe(channelId: String) = subscriptionsRepository.unsubscribe(channelId)
4851
suspend fun isSubscribed(channelId: String) = subscriptionsRepository.isSubscribed(channelId)
4952
suspend fun importSubscriptions(newChannels: List<String>) =

app/src/main/java/com/github/libretube/repo/AccountSubscriptionsRepository.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import com.github.libretube.api.obj.Subscription
66
import com.github.libretube.extensions.toID
77
import com.github.libretube.helpers.PreferenceHelper
88

9-
class AccountSubscriptionsRepository: SubscriptionsRepository {
9+
class AccountSubscriptionsRepository : SubscriptionsRepository {
1010
private val token get() = PreferenceHelper.getToken()
1111

12-
override suspend fun subscribe(channelId: String) {
12+
override suspend fun subscribe(
13+
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
14+
) {
1315
runCatching {
1416
RetrofitInstance.authApi.subscribe(token, Subscribe(channelId))
1517
}

app/src/main/java/com/github/libretube/repo/LocalSubscriptionsRepository.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@ import com.github.libretube.extensions.parallelMap
77
import com.github.libretube.ui.dialogs.ShareDialog.Companion.YOUTUBE_FRONTEND_URL
88
import org.schabi.newpipe.extractor.channel.ChannelInfo
99

10-
class LocalSubscriptionsRepository: SubscriptionsRepository {
11-
override suspend fun subscribe(channelId: String) {
12-
val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}"
13-
val channelInfo = ChannelInfo.getInfo(channelUrl)
14-
10+
class LocalSubscriptionsRepository : SubscriptionsRepository {
11+
override suspend fun subscribe(
12+
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
13+
) {
1514
val localSubscription = LocalSubscription(
16-
channelId = channelInfo.id,
17-
name = channelInfo.name,
18-
avatar = channelInfo.avatars.maxByOrNull { it.height }?.url,
19-
verified = channelInfo.isVerified
15+
channelId = channelId,
16+
name = name,
17+
avatar = uploaderAvatar,
18+
verified = verified
2019
)
2120

2221
Database.localSubscriptionDao().insert(localSubscription)
@@ -33,7 +32,10 @@ class LocalSubscriptionsRepository: SubscriptionsRepository {
3332
override suspend fun importSubscriptions(newChannels: List<String>) {
3433
for (chunk in newChannels.chunked(CHANNEL_CHUNK_SIZE)) {
3534
chunk.parallelMap { channelId ->
36-
runCatching { subscribe(channelId) }
35+
val channelUrl = "$YOUTUBE_FRONTEND_URL/channel/${channelId}"
36+
val channelInfo = ChannelInfo.getInfo(channelUrl)
37+
38+
runCatching { subscribe(channelId, channelInfo.name, channelInfo.avatars.maxByOrNull { it.height }?.url, channelInfo.isVerified) }
3739
}
3840
}
3941
}

app/src/main/java/com/github/libretube/repo/PipedLocalSubscriptionsRepository.kt

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ import com.github.libretube.api.obj.Subscription
66
import com.github.libretube.db.DatabaseHolder.Database
77
import com.github.libretube.db.obj.LocalSubscription
88

9-
class PipedLocalSubscriptionsRepository: SubscriptionsRepository {
10-
override suspend fun subscribe(channelId: String) {
9+
class PipedLocalSubscriptionsRepository : SubscriptionsRepository {
10+
override suspend fun subscribe(
11+
channelId: String, name: String, uploaderAvatar: String?, verified: Boolean
12+
) {
1113
// further meta info is not needed when using Piped local subscriptions
1214
Database.localSubscriptionDao().insert(LocalSubscription(channelId))
1315
}
@@ -29,9 +31,9 @@ class PipedLocalSubscriptionsRepository: SubscriptionsRepository {
2931
val channelIds = getSubscriptionChannelIds()
3032

3133
return when {
32-
channelIds.size > GET_SUBSCRIPTIONS_LIMIT ->
33-
RetrofitInstance.authApi
34-
.unauthenticatedSubscriptions(channelIds)
34+
channelIds.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
35+
channelIds
36+
)
3537

3638
else -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
3739
channelIds.joinToString(",")

app/src/main/java/com/github/libretube/repo/SubscriptionsRepository.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package com.github.libretube.repo
33
import com.github.libretube.api.obj.Subscription
44

55
interface SubscriptionsRepository {
6-
suspend fun subscribe(channelId: String)
6+
suspend fun subscribe(channelId: String, name: String, uploaderAvatar: String?, verified: Boolean)
77
suspend fun unsubscribe(channelId: String)
88
suspend fun isSubscribed(channelId: String): Boolean?
99
suspend fun importSubscriptions(newChannels: List<String>)

app/src/main/java/com/github/libretube/ui/adapters/SearchResultsAdapter.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,12 @@ class SearchResultsAdapter(
159159
}
160160

161161
var subscribed = false
162-
binding.searchSubButton.setupSubscriptionButton(item.url.toID(), item.name?.toID()) {
162+
binding.searchSubButton.setupSubscriptionButton(
163+
item.url.toID(),
164+
item.name.orEmpty(),
165+
item.uploaderAvatar,
166+
item.uploaderVerified ?: false
167+
) {
163168
subscribed = it
164169
}
165170

app/src/main/java/com/github/libretube/ui/adapters/SubscriptionChannelAdapter.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ class SubscriptionChannelAdapter :
5252
subscriptionSubscribe.setupSubscriptionButton(
5353
subscription.url.toID(),
5454
subscription.name,
55+
subscription.avatar,
56+
subscription.verified,
5557
notificationBell,
5658
true
5759
)

app/src/main/java/com/github/libretube/ui/extensions/SetupSubscriptionButton.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import kotlinx.coroutines.withContext
1515

1616
fun TextView.setupSubscriptionButton(
1717
channelId: String?,
18-
channelName: String?,
18+
channelName: String,
19+
channelAvatar: String?,
20+
channelVerified: Boolean,
1921
notificationBell: MaterialButton? = null,
2022
isSubscribed: Boolean? = null,
2123
onIsSubscribedChange: (Boolean) -> Unit = {}
@@ -53,7 +55,7 @@ fun TextView.setupSubscriptionButton(
5355
} else {
5456
CoroutineScope(Dispatchers.Main).launch {
5557
withContext(Dispatchers.IO) {
56-
SubscriptionHelper.subscribe(channelId)
58+
SubscriptionHelper.subscribe(channelId, channelName, channelAvatar, channelVerified)
5759
}
5860

5961
text = context.getString(R.string.unsubscribe)

app/src/main/java/com/github/libretube/ui/fragments/ChannelFragment.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ class ChannelFragment : DynamicLayoutManagerFragment(R.layout.fragment_channel)
150150

151151
binding.channelSubscribe.setupSubscriptionButton(
152152
channelId,
153-
channelName,
153+
response.name.orEmpty(),
154+
response.avatarUrl,
155+
response.verified,
154156
binding.notificationBell
155157
) { isSubscribed ->
156158
_binding?.addToGroup?.isVisible = isSubscribed

app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,8 +1147,10 @@ class PlayerFragment : Fragment(R.layout.fragment_player), OnlinePlayerOptions {
11471147

11481148
// update the subscribed state
11491149
binding.playerSubscribe.setupSubscriptionButton(
1150-
this.streams.uploaderUrl.toID(),
1151-
this.streams.uploader
1150+
streams.uploaderUrl.toID(),
1151+
streams.uploader,
1152+
streams.uploaderAvatar,
1153+
streams.uploaderVerified
11521154
)
11531155

11541156
// seekbar preview setup

0 commit comments

Comments
 (0)