From 40e85152ac7f0a06a914dd40b8502488fb7f60ac Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 29 Oct 2023 15:44:53 +0100 Subject: [PATCH] refactor: simplify SubscriptionsHelper --- .../libretube/api/SubscriptionHelper.kt | 43 ++++++------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt b/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt index ad31f0ab5..039f21106 100644 --- a/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt +++ b/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt @@ -13,22 +13,20 @@ import com.github.libretube.extensions.TAG import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.util.deArrow import com.google.android.material.dialog.MaterialAlertDialogBuilder -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.runBlocking -import kotlinx.coroutines.withContext 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 val token get() = PreferenceHelper.getToken() suspend fun subscribe(channelId: String) { - val token = PreferenceHelper.getToken() if (token.isNotEmpty()) { - try { - withContext(Dispatchers.IO) { - RetrofitInstance.authApi.subscribe(token, Subscribe(channelId)) - } - } catch (e: Exception) { - Log.e(TAG(), e.toString()) + runCatching { + RetrofitInstance.authApi.subscribe(token, Subscribe(channelId)) } } else { Database.localSubscriptionDao().insert(LocalSubscription(channelId)) @@ -36,14 +34,9 @@ object SubscriptionHelper { } suspend fun unsubscribe(channelId: String) { - val token = PreferenceHelper.getToken() if (token.isNotEmpty()) { - try { - withContext(Dispatchers.IO) { - RetrofitInstance.authApi.unsubscribe(token, Subscribe(channelId)) - } - } catch (e: Exception) { - Log.e(TAG(), e.toString()) + runCatching { + RetrofitInstance.authApi.unsubscribe(token, Subscribe(channelId)) } } else { Database.localSubscriptionDao().delete(LocalSubscription(channelId)) @@ -78,7 +71,6 @@ object SubscriptionHelper { } suspend fun isSubscribed(channelId: String): Boolean? { - val token = PreferenceHelper.getToken() if (token.isNotEmpty()) { val isSubscribed = try { RetrofitInstance.authApi.isSubscribed(channelId, token) @@ -93,12 +85,9 @@ object SubscriptionHelper { } suspend fun importSubscriptions(newChannels: List) { - val token = PreferenceHelper.getToken() if (token.isNotEmpty()) { - try { + runCatching { RetrofitInstance.authApi.importSubscriptions(false, token, newChannels) - } catch (e: Exception) { - e.printStackTrace() } } else { Database.localSubscriptionDao().insertAll(newChannels.map { LocalSubscription(it) }) @@ -106,15 +95,13 @@ object SubscriptionHelper { } suspend fun getSubscriptions(): List { - val token = PreferenceHelper.getToken() 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 - ) + subscriptions.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi + .unauthenticatedSubscriptions(subscriptions) else -> RetrofitInstance.authApi.unauthenticatedSubscriptions( subscriptions.joinToString(",") @@ -124,15 +111,13 @@ object SubscriptionHelper { } suspend fun getFeed(): List { - val token = PreferenceHelper.getToken() 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 - ) + subscriptions.size > GET_SUBSCRIPTIONS_LIMIT -> RetrofitInstance.authApi + .getUnauthenticatedFeed(subscriptions) else -> RetrofitInstance.authApi.getUnauthenticatedFeed( subscriptions.joinToString(",")