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-08-14 13:25:28 +05:30
|
|
|
import com.github.libretube.extensions.TAG
|
2022-08-03 17:11:45 +05:30
|
|
|
import com.github.libretube.obj.Subscribe
|
|
|
|
import com.github.libretube.preferences.PreferenceHelper
|
|
|
|
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(),
|
|
|
|
Subscribe(channelId)
|
|
|
|
)
|
|
|
|
} 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 {
|
|
|
|
val channels = PreferenceHelper.getLocalSubscriptions().toMutableList()
|
|
|
|
channels.add(channelId)
|
|
|
|
PreferenceHelper.setLocalSubscriptions(channels)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun unsubscribe(channelId: String) {
|
|
|
|
if (PreferenceHelper.getToken() != "") {
|
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
|
|
|
try {
|
|
|
|
RetrofitInstance.authApi.unsubscribe(
|
|
|
|
PreferenceHelper.getToken(),
|
|
|
|
Subscribe(channelId)
|
|
|
|
)
|
|
|
|
} 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 {
|
|
|
|
val channels = PreferenceHelper.getLocalSubscriptions().toMutableList()
|
|
|
|
channels.remove(channelId)
|
|
|
|
PreferenceHelper.setLocalSubscriptions(channels)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return PreferenceHelper.getLocalSubscriptions().contains(channelId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-06 15:19:11 +05:30
|
|
|
val channels = PreferenceHelper.getLocalSubscriptions().toMutableList()
|
|
|
|
newChannels.forEach {
|
|
|
|
if (!channels.contains(it)) channels += it
|
|
|
|
}
|
|
|
|
PreferenceHelper.setLocalSubscriptions(channels)
|
2022-08-06 14:39:28 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-03 17:11:45 +05:30
|
|
|
fun getFormattedLocalSubscriptions(): String {
|
|
|
|
return PreferenceHelper.getLocalSubscriptions().joinToString(",")
|
|
|
|
}
|
|
|
|
}
|