mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
refactor: bind subscriptions and feed repositories to interfaces
This commit is contained in:
parent
116bfabdf1
commit
f54d0fd8a0
@ -1,16 +1,15 @@
|
|||||||
package com.github.libretube.api
|
package com.github.libretube.api
|
||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.util.Log
|
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.api.obj.StreamItem
|
|
||||||
import com.github.libretube.api.obj.Subscribe
|
|
||||||
import com.github.libretube.api.obj.Subscription
|
|
||||||
import com.github.libretube.constants.PreferenceKeys
|
import com.github.libretube.constants.PreferenceKeys
|
||||||
import com.github.libretube.db.DatabaseHolder.Database
|
|
||||||
import com.github.libretube.db.obj.LocalSubscription
|
|
||||||
import com.github.libretube.extensions.TAG
|
|
||||||
import com.github.libretube.helpers.PreferenceHelper
|
import com.github.libretube.helpers.PreferenceHelper
|
||||||
|
import com.github.libretube.repo.AccountSubscriptionsRepository
|
||||||
|
import com.github.libretube.repo.FeedRepository
|
||||||
|
import com.github.libretube.repo.LocalSubscriptionsRepository
|
||||||
|
import com.github.libretube.repo.PipedAccountFeedRepository
|
||||||
|
import com.github.libretube.repo.PipedNoAccountFeedRepository
|
||||||
|
import com.github.libretube.repo.SubscriptionsRepository
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
|
||||||
@ -19,28 +18,24 @@ object SubscriptionHelper {
|
|||||||
* The maximum number of channel IDs that can be passed via a GET request for fetching
|
* The maximum number of channel IDs that can be passed via a GET request for fetching
|
||||||
* the subscriptions list and the feed
|
* the subscriptions list and the feed
|
||||||
*/
|
*/
|
||||||
private const val GET_SUBSCRIPTIONS_LIMIT = 100
|
const val GET_SUBSCRIPTIONS_LIMIT = 100
|
||||||
|
|
||||||
private val token get() = PreferenceHelper.getToken()
|
private val token get() = PreferenceHelper.getToken()
|
||||||
|
private val subscriptionsRepository: SubscriptionsRepository get() = when {
|
||||||
suspend fun subscribe(channelId: String) {
|
token.isNotEmpty() -> AccountSubscriptionsRepository()
|
||||||
if (token.isNotEmpty()) {
|
else -> LocalSubscriptionsRepository()
|
||||||
runCatching {
|
|
||||||
RetrofitInstance.authApi.subscribe(token, Subscribe(channelId))
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Database.localSubscriptionDao().insert(LocalSubscription(channelId))
|
|
||||||
}
|
}
|
||||||
|
private val feedRepository: FeedRepository get() = when {
|
||||||
|
token.isNotEmpty() -> PipedAccountFeedRepository()
|
||||||
|
else -> PipedNoAccountFeedRepository()
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun unsubscribe(channelId: String) {
|
suspend fun subscribe(channelId: String) = subscriptionsRepository.subscribe(channelId)
|
||||||
if (token.isNotEmpty()) {
|
suspend fun unsubscribe(channelId: String) = subscriptionsRepository.unsubscribe(channelId)
|
||||||
runCatching {
|
suspend fun isSubscribed(channelId: String) = subscriptionsRepository.isSubscribed(channelId)
|
||||||
RetrofitInstance.authApi.unsubscribe(token, Subscribe(channelId))
|
suspend fun importSubscriptions(newChannels: List<String>) = subscriptionsRepository.importSubscriptions(newChannels)
|
||||||
}
|
suspend fun getSubscriptions() = subscriptionsRepository.getSubscriptions()
|
||||||
} else {
|
suspend fun getFeed() = feedRepository.getFeed(false)
|
||||||
Database.localSubscriptionDao().delete(LocalSubscription(channelId))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun handleUnsubscribe(
|
fun handleUnsubscribe(
|
||||||
context: Context,
|
context: Context,
|
||||||
@ -68,62 +63,4 @@ object SubscriptionHelper {
|
|||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
.show()
|
.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun isSubscribed(channelId: String): Boolean? {
|
|
||||||
if (token.isNotEmpty()) {
|
|
||||||
val isSubscribed = try {
|
|
||||||
RetrofitInstance.authApi.isSubscribed(channelId, token)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG(), e.toString())
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
return isSubscribed.subscribed
|
|
||||||
} else {
|
|
||||||
return Database.localSubscriptionDao().includes(channelId)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun importSubscriptions(newChannels: List<String>) {
|
|
||||||
if (token.isNotEmpty()) {
|
|
||||||
runCatching {
|
|
||||||
RetrofitInstance.authApi.importSubscriptions(false, token, newChannels)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Database.localSubscriptionDao().insertAll(newChannels.map { LocalSubscription(it) })
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun getSubscriptions(): List<Subscription> {
|
|
||||||
return if (token.isNotEmpty()) {
|
|
||||||
RetrofitInstance.authApi.subscriptions(token)
|
|
||||||
} else {
|
|
||||||
val subscriptions = Database.localSubscriptionDao().getAll().map { it.channelId }
|
|
||||||
when {
|
|
||||||
subscriptions.size > GET_SUBSCRIPTIONS_LIMIT ->
|
|
||||||
RetrofitInstance.authApi
|
|
||||||
.unauthenticatedSubscriptions(subscriptions)
|
|
||||||
|
|
||||||
else -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
|
|
||||||
subscriptions.joinToString(",")
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
suspend fun getFeed(): List<StreamItem> {
|
|
||||||
return if (token.isNotEmpty()) {
|
|
||||||
RetrofitInstance.authApi.getFeed(token)
|
|
||||||
} else {
|
|
||||||
val subscriptions = Database.localSubscriptionDao().getAll().map { it.channelId }
|
|
||||||
when {
|
|
||||||
subscriptions.size > GET_SUBSCRIPTIONS_LIMIT ->
|
|
||||||
RetrofitInstance.authApi
|
|
||||||
.getUnauthenticatedFeed(subscriptions)
|
|
||||||
|
|
||||||
else -> RetrofitInstance.authApi.getUnauthenticatedFeed(
|
|
||||||
subscriptions.joinToString(",")
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,36 @@
|
|||||||
|
package com.github.libretube.repo
|
||||||
|
|
||||||
|
import com.github.libretube.api.RetrofitInstance
|
||||||
|
import com.github.libretube.api.obj.Subscribe
|
||||||
|
import com.github.libretube.api.obj.Subscription
|
||||||
|
import com.github.libretube.helpers.PreferenceHelper
|
||||||
|
|
||||||
|
class AccountSubscriptionsRepository: SubscriptionsRepository {
|
||||||
|
private val token get() = PreferenceHelper.getToken()
|
||||||
|
|
||||||
|
override suspend fun subscribe(channelId: String) {
|
||||||
|
runCatching {
|
||||||
|
RetrofitInstance.authApi.subscribe(token, Subscribe(channelId))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun unsubscribe(channelId: String) {
|
||||||
|
runCatching {
|
||||||
|
RetrofitInstance.authApi.unsubscribe(token, Subscribe(channelId))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun isSubscribed(channelId: String): Boolean? {
|
||||||
|
return runCatching {
|
||||||
|
RetrofitInstance.authApi.isSubscribed(channelId, token)
|
||||||
|
}.getOrNull()?.subscribed
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun importSubscriptions(newChannels: List<String>) {
|
||||||
|
RetrofitInstance.authApi.importSubscriptions(false, token, newChannels)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getSubscriptions(): List<Subscription> {
|
||||||
|
return RetrofitInstance.authApi.subscriptions(token)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package com.github.libretube.repo
|
||||||
|
|
||||||
|
import com.github.libretube.api.obj.StreamItem
|
||||||
|
|
||||||
|
interface FeedRepository {
|
||||||
|
suspend fun getFeed(forceRefresh: Boolean): List<StreamItem>
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package com.github.libretube.repo
|
||||||
|
|
||||||
|
import com.github.libretube.api.RetrofitInstance
|
||||||
|
import com.github.libretube.api.SubscriptionHelper.GET_SUBSCRIPTIONS_LIMIT
|
||||||
|
import com.github.libretube.api.obj.Subscription
|
||||||
|
import com.github.libretube.db.DatabaseHolder.Database
|
||||||
|
import com.github.libretube.db.obj.LocalSubscription
|
||||||
|
|
||||||
|
class LocalSubscriptionsRepository: SubscriptionsRepository {
|
||||||
|
override suspend fun subscribe(channelId: String) {
|
||||||
|
Database.localSubscriptionDao().insert(LocalSubscription(channelId))
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun unsubscribe(channelId: String) {
|
||||||
|
Database.localSubscriptionDao().delete(LocalSubscription(channelId))
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun isSubscribed(channelId: String): Boolean {
|
||||||
|
return Database.localSubscriptionDao().includes(channelId)
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun importSubscriptions(newChannels: List<String>) {
|
||||||
|
Database.localSubscriptionDao().insertAll(newChannels.map { LocalSubscription(it) })
|
||||||
|
}
|
||||||
|
|
||||||
|
override suspend fun getSubscriptions(): List<Subscription> {
|
||||||
|
val subscriptions = Database.localSubscriptionDao().getAll().map { it.channelId }
|
||||||
|
|
||||||
|
return when {
|
||||||
|
subscriptions.size > GET_SUBSCRIPTIONS_LIMIT ->
|
||||||
|
RetrofitInstance.authApi
|
||||||
|
.unauthenticatedSubscriptions(subscriptions)
|
||||||
|
|
||||||
|
else -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
|
||||||
|
subscriptions.joinToString(",")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.github.libretube.repo
|
||||||
|
|
||||||
|
import com.github.libretube.api.RetrofitInstance
|
||||||
|
import com.github.libretube.api.obj.StreamItem
|
||||||
|
import com.github.libretube.helpers.PreferenceHelper
|
||||||
|
|
||||||
|
class PipedAccountFeedRepository: FeedRepository {
|
||||||
|
override suspend fun getFeed(forceRefresh: Boolean): List<StreamItem> {
|
||||||
|
val token = PreferenceHelper.getToken()
|
||||||
|
|
||||||
|
return RetrofitInstance.authApi.getFeed(token)
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.github.libretube.repo
|
||||||
|
|
||||||
|
import com.github.libretube.api.RetrofitInstance
|
||||||
|
import com.github.libretube.api.SubscriptionHelper
|
||||||
|
import com.github.libretube.api.SubscriptionHelper.GET_SUBSCRIPTIONS_LIMIT
|
||||||
|
import com.github.libretube.api.obj.StreamItem
|
||||||
|
import com.github.libretube.extensions.toID
|
||||||
|
|
||||||
|
class PipedNoAccountFeedRepository: FeedRepository {
|
||||||
|
override suspend fun getFeed(forceRefresh: Boolean): List<StreamItem> {
|
||||||
|
val subscriptions = SubscriptionHelper.getSubscriptions().map { it.url.toID() }
|
||||||
|
|
||||||
|
return when {
|
||||||
|
subscriptions.size > GET_SUBSCRIPTIONS_LIMIT ->
|
||||||
|
RetrofitInstance.authApi
|
||||||
|
.getUnauthenticatedFeed(subscriptions)
|
||||||
|
|
||||||
|
else -> RetrofitInstance.authApi.getUnauthenticatedFeed(
|
||||||
|
subscriptions.joinToString(",")
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.github.libretube.repo
|
||||||
|
|
||||||
|
import com.github.libretube.api.obj.Subscription
|
||||||
|
|
||||||
|
interface SubscriptionsRepository {
|
||||||
|
suspend fun subscribe(channelId: String)
|
||||||
|
suspend fun unsubscribe(channelId: String)
|
||||||
|
suspend fun isSubscribed(channelId: String): Boolean?
|
||||||
|
suspend fun importSubscriptions(newChannels: List<String>)
|
||||||
|
suspend fun getSubscriptions(): List<Subscription>
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user