2022-08-14 13:25:28 +05:30
|
|
|
package com.github.libretube.api
|
2022-08-03 17:11:45 +05:30
|
|
|
|
2022-11-06 00:04:10 +05:30
|
|
|
import android.content.Context
|
2022-08-03 17:11:45 +05:30
|
|
|
import android.util.Log
|
2022-11-06 00:04:10 +05:30
|
|
|
import com.github.libretube.R
|
2022-11-18 20:27:16 +05:30
|
|
|
import com.github.libretube.api.obj.StreamItem
|
2023-01-19 13:20:26 +05:30
|
|
|
import com.github.libretube.api.obj.Subscribe
|
2022-11-18 20:27:16 +05:30
|
|
|
import com.github.libretube.api.obj.Subscription
|
2022-11-06 00:04:10 +05:30
|
|
|
import com.github.libretube.constants.PreferenceKeys
|
2022-09-18 22:54:31 +05:30
|
|
|
import com.github.libretube.db.DatabaseHolder.Companion.Database
|
2022-08-14 14:59:29 +05:30
|
|
|
import com.github.libretube.db.obj.LocalSubscription
|
2022-08-14 13:25:28 +05:30
|
|
|
import com.github.libretube.extensions.TAG
|
2023-01-31 21:13:39 +05:30
|
|
|
import com.github.libretube.helpers.PreferenceHelper
|
2022-11-06 00:04:10 +05:30
|
|
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
2022-08-03 17:11:45 +05:30
|
|
|
import kotlinx.coroutines.Dispatchers
|
2023-01-23 11:29:14 +05:30
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
|
import kotlinx.coroutines.withContext
|
2022-08-03 17:11:45 +05:30
|
|
|
|
|
|
|
object SubscriptionHelper {
|
2023-01-23 11:29:14 +05:30
|
|
|
suspend fun subscribe(channelId: String) {
|
|
|
|
val token = PreferenceHelper.getToken()
|
|
|
|
if (token.isNotEmpty()) {
|
|
|
|
try {
|
|
|
|
withContext(Dispatchers.IO) {
|
|
|
|
RetrofitInstance.authApi.subscribe(token, Subscribe(channelId))
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|
2023-01-23 11:29:14 +05:30
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.e(TAG(), e.toString())
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|
|
|
|
} else {
|
2023-01-23 11:29:14 +05:30
|
|
|
Database.localSubscriptionDao().insertAll(listOf(LocalSubscription(channelId)))
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-23 11:29:14 +05:30
|
|
|
suspend fun unsubscribe(channelId: String) {
|
|
|
|
val token = PreferenceHelper.getToken()
|
|
|
|
if (token.isNotEmpty()) {
|
|
|
|
try {
|
|
|
|
withContext(Dispatchers.IO) {
|
|
|
|
RetrofitInstance.authApi.unsubscribe(token, Subscribe(channelId))
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|
2023-01-23 11:29:14 +05:30
|
|
|
} catch (e: Exception) {
|
|
|
|
Log.e(TAG(), e.toString())
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|
|
|
|
} else {
|
2023-01-23 11:29:14 +05:30
|
|
|
Database.localSubscriptionDao().delete(LocalSubscription(channelId))
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 21:28:34 +05:30
|
|
|
fun handleUnsubscribe(
|
|
|
|
context: Context,
|
|
|
|
channelId: String,
|
|
|
|
channelName: String?,
|
|
|
|
onUnsubscribe: () -> Unit
|
|
|
|
) {
|
2022-11-06 00:04:10 +05:30
|
|
|
if (!PreferenceHelper.getBoolean(PreferenceKeys.CONFIRM_UNSUBSCRIBE, false)) {
|
2023-01-23 11:29:14 +05:30
|
|
|
runBlocking {
|
|
|
|
unsubscribe(channelId)
|
|
|
|
onUnsubscribe()
|
|
|
|
}
|
2022-11-06 00:04:10 +05:30
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
MaterialAlertDialogBuilder(context)
|
|
|
|
.setTitle(R.string.unsubscribe)
|
|
|
|
.setMessage(context.getString(R.string.confirm_unsubscribe, channelName))
|
|
|
|
.setPositiveButton(R.string.unsubscribe) { _, _ ->
|
2023-01-23 11:29:14 +05:30
|
|
|
runBlocking {
|
|
|
|
unsubscribe(channelId)
|
|
|
|
onUnsubscribe()
|
|
|
|
}
|
2022-11-06 00:04:10 +05:30
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
2022-08-03 17:11:45 +05:30
|
|
|
suspend fun isSubscribed(channelId: String): Boolean? {
|
2023-01-23 11:29:14 +05:30
|
|
|
val token = PreferenceHelper.getToken()
|
|
|
|
if (token.isNotEmpty()) {
|
2022-08-03 17:11:45 +05:30
|
|
|
val isSubscribed = try {
|
2023-01-23 11:29:14 +05:30
|
|
|
RetrofitInstance.authApi.isSubscribed(channelId, token)
|
2022-08-03 17:11:45 +05:30
|
|
|
} catch (e: Exception) {
|
2022-08-14 13:25:28 +05:30
|
|
|
Log.e(TAG(), e.toString())
|
2022-08-03 17:11:45 +05:30
|
|
|
return null
|
|
|
|
}
|
|
|
|
return isSubscribed.subscribed
|
|
|
|
} else {
|
2023-01-23 11:29:14 +05:30
|
|
|
return Database.localSubscriptionDao().includes(channelId)
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-06 15:19:11 +05:30
|
|
|
suspend fun importSubscriptions(newChannels: List<String>) {
|
2023-01-23 11:29:14 +05:30
|
|
|
val token = PreferenceHelper.getToken()
|
|
|
|
if (token.isNotEmpty()) {
|
2022-08-06 15:19:11 +05:30
|
|
|
try {
|
2023-01-23 11:29:14 +05:30
|
|
|
RetrofitInstance.authApi.importSubscriptions(false, token, newChannels)
|
2022-08-06 14:39:28 +05:30
|
|
|
} catch (e: Exception) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-23 11:29:14 +05:30
|
|
|
Database.localSubscriptionDao().insertAll(newChannels.map { LocalSubscription(it) })
|
2022-09-22 21:22:00 +05:30
|
|
|
}
|
2022-08-14 14:59:29 +05:30
|
|
|
}
|
|
|
|
|
2023-01-23 11:29:14 +05:30
|
|
|
suspend fun getFormattedLocalSubscriptions(): String {
|
|
|
|
return Database.localSubscriptionDao().getAll()
|
|
|
|
.joinToString(",") { it.channelId }
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|
2022-11-18 20:27:16 +05:30
|
|
|
|
|
|
|
suspend fun getSubscriptions(): List<Subscription> {
|
2023-01-23 11:29:14 +05:30
|
|
|
val token = PreferenceHelper.getToken()
|
|
|
|
return if (token.isNotEmpty()) {
|
|
|
|
RetrofitInstance.authApi.subscriptions(token)
|
2022-11-18 20:27:16 +05:30
|
|
|
} else {
|
2023-01-23 11:29:14 +05:30
|
|
|
RetrofitInstance.authApi.unauthenticatedSubscriptions(getFormattedLocalSubscriptions())
|
2022-11-18 20:27:16 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
suspend fun getFeed(): List<StreamItem> {
|
2023-01-23 11:29:14 +05:30
|
|
|
val token = PreferenceHelper.getToken()
|
|
|
|
return if (token.isNotEmpty()) {
|
|
|
|
RetrofitInstance.authApi.getFeed(token)
|
2022-11-18 20:27:16 +05:30
|
|
|
} else {
|
2023-01-23 11:29:14 +05:30
|
|
|
RetrofitInstance.authApi.getUnauthenticatedFeed(getFormattedLocalSubscriptions())
|
2022-11-18 20:27:16 +05:30
|
|
|
}
|
|
|
|
}
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|