LibreTube/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt

117 lines
3.8 KiB
Kotlin
Raw Normal View History

2022-08-14 13:25:28 +05:30
package com.github.libretube.api
2022-08-03 17:11:45 +05:30
import android.util.Log
2022-09-18 22:54:31 +05:30
import com.github.libretube.db.DatabaseHolder.Companion.Database
import com.github.libretube.db.obj.LocalSubscription
2022-08-14 13:25:28 +05:30
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.await
2022-09-20 23:23:34 +05:30
import com.github.libretube.api.obj.Subscribe
2022-09-08 21:59:00 +05:30
import com.github.libretube.util.PreferenceHelper
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 {
Thread {
2022-09-18 22:54:31 +05:30
Database.localSubscriptionDao().insertAll(
LocalSubscription(channelId)
)
}.start()
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 {
Thread {
2022-09-18 22:54:31 +05:30
Database.localSubscriptionDao().delete(
LocalSubscription(channelId)
)
}.start()
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 {
var isSubscribed = false
Thread {
2022-09-18 22:54:31 +05:30
isSubscribed = Database.localSubscriptionDao().includes(channelId)
}.await()
return isSubscribed
2022-08-03 17:11:45 +05:30
}
}
suspend fun importSubscriptions(newChannels: List<String>) {
2022-08-06 14:39:28 +05:30
if (PreferenceHelper.getToken() != "") {
try {
2022-08-06 14:39:28 +05:30
val token = PreferenceHelper.getToken()
RetrofitInstance.authApi.importSubscriptions(
false,
token,
newChannels
2022-08-06 14:39:28 +05:30
)
} catch (e: Exception) {
e.printStackTrace()
}
} else {
val newLocalSubscriptions = mutableListOf<LocalSubscription>()
newChannels.forEach {
newLocalSubscriptions += LocalSubscription(channelId = it)
}
Thread {
2022-09-18 22:54:31 +05:30
Database.localSubscriptionDao().insertAll(
*newChannels.map { LocalSubscription(it) }.toTypedArray()
)
}.start()
2022-08-06 14:39:28 +05:30
}
}
fun getLocalSubscriptions(): List<LocalSubscription> {
var localSubscriptions = listOf<LocalSubscription>()
Thread {
2022-09-18 22:54:31 +05:30
localSubscriptions = Database.localSubscriptionDao().getAll()
}.await()
return localSubscriptions
}
2022-08-03 17:11:45 +05:30
fun getFormattedLocalSubscriptions(): String {
val localSubscriptions = getLocalSubscriptions()
return localSubscriptions.map { it.channelId }.joinToString(",")
2022-08-03 17:11:45 +05:30
}
}