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
|
|
|
|
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
|
2022-09-22 21:22:00 +05:30
|
|
|
import com.github.libretube.extensions.awaitQuery
|
|
|
|
import com.github.libretube.extensions.query
|
2022-09-08 21:59:00 +05:30
|
|
|
import com.github.libretube.util.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.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
|
|
|
|
object SubscriptionHelper {
|
|
|
|
|
|
|
|
fun subscribe(channelId: String) {
|
|
|
|
if (PreferenceHelper.getToken() != "") {
|
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
try {
|
|
|
|
RetrofitInstance.authApi.subscribe(
|
|
|
|
PreferenceHelper.getToken(),
|
2022-09-20 23:23:34 +05:30
|
|
|
com.github.libretube.api.obj.Subscribe(channelId)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-22 21:22:00 +05:30
|
|
|
query {
|
2022-09-18 22:54:31 +05:30
|
|
|
Database.localSubscriptionDao().insertAll(
|
2022-08-14 14:59:29 +05:30
|
|
|
LocalSubscription(channelId)
|
|
|
|
)
|
2022-09-22 21:22:00 +05:30
|
|
|
}
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun unsubscribe(channelId: String) {
|
|
|
|
if (PreferenceHelper.getToken() != "") {
|
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
try {
|
|
|
|
RetrofitInstance.authApi.unsubscribe(
|
|
|
|
PreferenceHelper.getToken(),
|
2022-09-20 23:23:34 +05:30
|
|
|
com.github.libretube.api.obj.Subscribe(channelId)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-22 21:22:00 +05:30
|
|
|
query {
|
2022-09-18 22:54:31 +05:30
|
|
|
Database.localSubscriptionDao().delete(
|
2022-08-14 14:59:29 +05:30
|
|
|
LocalSubscription(channelId)
|
|
|
|
)
|
2022-09-22 21:22:00 +05:30
|
|
|
}
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 00:04:10 +05:30
|
|
|
fun handleUnsubscribe(context: Context, channelId: String, channelName: String?, onUnsubscribe: () -> Unit) {
|
|
|
|
if (!PreferenceHelper.getBoolean(PreferenceKeys.CONFIRM_UNSUBSCRIBE, false)) {
|
|
|
|
unsubscribe(channelId)
|
|
|
|
onUnsubscribe.invoke()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
MaterialAlertDialogBuilder(context)
|
|
|
|
.setTitle(R.string.unsubscribe)
|
|
|
|
.setMessage(context.getString(R.string.confirm_unsubscribe, channelName))
|
|
|
|
.setPositiveButton(R.string.unsubscribe) { _, _ ->
|
|
|
|
unsubscribe(channelId)
|
|
|
|
onUnsubscribe.invoke()
|
|
|
|
}
|
|
|
|
.setNegativeButton(R.string.cancel, null)
|
|
|
|
.show()
|
|
|
|
}
|
|
|
|
|
2022-08-03 17:11:45 +05:30
|
|
|
suspend fun isSubscribed(channelId: String): Boolean? {
|
|
|
|
if (PreferenceHelper.getToken() != "") {
|
|
|
|
val isSubscribed = try {
|
|
|
|
RetrofitInstance.authApi.isSubscribed(
|
|
|
|
channelId,
|
|
|
|
PreferenceHelper.getToken()
|
|
|
|
)
|
|
|
|
} 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 {
|
2022-09-22 21:22:00 +05:30
|
|
|
return awaitQuery {
|
|
|
|
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>) {
|
2022-08-06 14:39:28 +05:30
|
|
|
if (PreferenceHelper.getToken() != "") {
|
2022-08-06 15:19:11 +05:30
|
|
|
try {
|
2022-08-06 14:39:28 +05:30
|
|
|
val token = PreferenceHelper.getToken()
|
|
|
|
RetrofitInstance.authApi.importSubscriptions(
|
|
|
|
false,
|
|
|
|
token,
|
2022-08-06 15:19:11 +05:30
|
|
|
newChannels
|
2022-08-06 14:39:28 +05:30
|
|
|
)
|
|
|
|
} catch (e: Exception) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
} else {
|
2022-08-14 14:59:29 +05:30
|
|
|
val newLocalSubscriptions = mutableListOf<LocalSubscription>()
|
2022-08-06 15:19:11 +05:30
|
|
|
newChannels.forEach {
|
2022-08-14 14:59:29 +05:30
|
|
|
newLocalSubscriptions += LocalSubscription(channelId = it)
|
2022-08-06 15:19:11 +05:30
|
|
|
}
|
2022-09-22 21:22:00 +05:30
|
|
|
query {
|
2022-09-18 22:54:31 +05:30
|
|
|
Database.localSubscriptionDao().insertAll(
|
2022-08-14 14:59:29 +05:30
|
|
|
*newChannels.map { LocalSubscription(it) }.toTypedArray()
|
|
|
|
)
|
2022-09-22 21:22:00 +05:30
|
|
|
}
|
2022-08-06 14:39:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 00:04:10 +05:30
|
|
|
private fun getLocalSubscriptions(): List<LocalSubscription> {
|
2022-09-22 21:22:00 +05:30
|
|
|
return awaitQuery {
|
|
|
|
Database.localSubscriptionDao().getAll()
|
|
|
|
}
|
2022-08-14 14:59:29 +05:30
|
|
|
}
|
|
|
|
|
2022-08-03 17:11:45 +05:30
|
|
|
fun getFormattedLocalSubscriptions(): String {
|
2022-08-14 14:59:29 +05:30
|
|
|
val localSubscriptions = getLocalSubscriptions()
|
2022-11-06 00:04:10 +05:30
|
|
|
return localSubscriptions.joinToString(",") { it.channelId }
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|
2022-11-18 20:27:16 +05:30
|
|
|
|
|
|
|
suspend fun getSubscriptions(): List<Subscription> {
|
|
|
|
return if (PreferenceHelper.getToken() != "") {
|
|
|
|
RetrofitInstance.authApi.subscriptions(
|
|
|
|
PreferenceHelper.getToken()
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
RetrofitInstance.authApi.unauthenticatedSubscriptions(
|
|
|
|
getFormattedLocalSubscriptions()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
suspend fun getFeed(): List<StreamItem> {
|
|
|
|
return if (PreferenceHelper.getToken() != "") {
|
|
|
|
RetrofitInstance.authApi.getFeed(
|
|
|
|
PreferenceHelper.getToken()
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
RetrofitInstance.authApi.getUnauthenticatedFeed(
|
|
|
|
getFormattedLocalSubscriptions()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2022-08-03 17:11:45 +05:30
|
|
|
}
|