refactor: simplify SubscriptionsHelper

This commit is contained in:
Bnyro 2023-10-29 15:44:53 +01:00
parent d03a58a296
commit 40e85152ac

View File

@ -13,38 +13,31 @@ import com.github.libretube.extensions.TAG
import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.util.deArrow import com.github.libretube.util.deArrow
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
object SubscriptionHelper { object SubscriptionHelper {
/**
* The maximum number of channel IDs that can be passed via a GET request for fetching
* the subscriptions list and the feed
*/
private const val GET_SUBSCRIPTIONS_LIMIT = 100 private const val GET_SUBSCRIPTIONS_LIMIT = 100
private val token get() = PreferenceHelper.getToken()
suspend fun subscribe(channelId: String) { suspend fun subscribe(channelId: String) {
val token = PreferenceHelper.getToken()
if (token.isNotEmpty()) { if (token.isNotEmpty()) {
try { runCatching {
withContext(Dispatchers.IO) {
RetrofitInstance.authApi.subscribe(token, Subscribe(channelId)) RetrofitInstance.authApi.subscribe(token, Subscribe(channelId))
} }
} catch (e: Exception) {
Log.e(TAG(), e.toString())
}
} else { } else {
Database.localSubscriptionDao().insert(LocalSubscription(channelId)) Database.localSubscriptionDao().insert(LocalSubscription(channelId))
} }
} }
suspend fun unsubscribe(channelId: String) { suspend fun unsubscribe(channelId: String) {
val token = PreferenceHelper.getToken()
if (token.isNotEmpty()) { if (token.isNotEmpty()) {
try { runCatching {
withContext(Dispatchers.IO) {
RetrofitInstance.authApi.unsubscribe(token, Subscribe(channelId)) RetrofitInstance.authApi.unsubscribe(token, Subscribe(channelId))
} }
} catch (e: Exception) {
Log.e(TAG(), e.toString())
}
} else { } else {
Database.localSubscriptionDao().delete(LocalSubscription(channelId)) Database.localSubscriptionDao().delete(LocalSubscription(channelId))
} }
@ -78,7 +71,6 @@ object SubscriptionHelper {
} }
suspend fun isSubscribed(channelId: String): Boolean? { suspend fun isSubscribed(channelId: String): Boolean? {
val token = PreferenceHelper.getToken()
if (token.isNotEmpty()) { if (token.isNotEmpty()) {
val isSubscribed = try { val isSubscribed = try {
RetrofitInstance.authApi.isSubscribed(channelId, token) RetrofitInstance.authApi.isSubscribed(channelId, token)
@ -93,12 +85,9 @@ object SubscriptionHelper {
} }
suspend fun importSubscriptions(newChannels: List<String>) { suspend fun importSubscriptions(newChannels: List<String>) {
val token = PreferenceHelper.getToken()
if (token.isNotEmpty()) { if (token.isNotEmpty()) {
try { runCatching {
RetrofitInstance.authApi.importSubscriptions(false, token, newChannels) RetrofitInstance.authApi.importSubscriptions(false, token, newChannels)
} catch (e: Exception) {
e.printStackTrace()
} }
} else { } else {
Database.localSubscriptionDao().insertAll(newChannels.map { LocalSubscription(it) }) Database.localSubscriptionDao().insertAll(newChannels.map { LocalSubscription(it) })
@ -106,15 +95,13 @@ object SubscriptionHelper {
} }
suspend fun getSubscriptions(): List<Subscription> { suspend fun getSubscriptions(): List<Subscription> {
val token = PreferenceHelper.getToken()
return if (token.isNotEmpty()) { return if (token.isNotEmpty()) {
RetrofitInstance.authApi.subscriptions(token) RetrofitInstance.authApi.subscriptions(token)
} else { } else {
val subscriptions = Database.localSubscriptionDao().getAll().map { it.channelId } val subscriptions = Database.localSubscriptionDao().getAll().map { it.channelId }
when { when {
subscriptions.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi.unauthenticatedSubscriptions( subscriptions.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi
subscriptions .unauthenticatedSubscriptions(subscriptions)
)
else -> RetrofitInstance.authApi.unauthenticatedSubscriptions( else -> RetrofitInstance.authApi.unauthenticatedSubscriptions(
subscriptions.joinToString(",") subscriptions.joinToString(",")
@ -124,15 +111,13 @@ object SubscriptionHelper {
} }
suspend fun getFeed(): List<StreamItem> { suspend fun getFeed(): List<StreamItem> {
val token = PreferenceHelper.getToken()
return if (token.isNotEmpty()) { return if (token.isNotEmpty()) {
RetrofitInstance.authApi.getFeed(token) RetrofitInstance.authApi.getFeed(token)
} else { } else {
val subscriptions = Database.localSubscriptionDao().getAll().map { it.channelId } val subscriptions = Database.localSubscriptionDao().getAll().map { it.channelId }
when { when {
subscriptions.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi.getUnauthenticatedFeed( subscriptions.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi
subscriptions .getUnauthenticatedFeed(subscriptions)
)
else -> RetrofitInstance.authApi.getUnauthenticatedFeed( else -> RetrofitInstance.authApi.getUnauthenticatedFeed(
subscriptions.joinToString(",") subscriptions.joinToString(",")