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

90 lines
3.0 KiB
Kotlin
Raw Normal View History

2022-08-03 17:11:45 +05:30
package com.github.libretube.util
import android.util.Log
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 {
val TAG = "SubscriptionHelper"
fun subscribe(channelId: String) {
if (PreferenceHelper.getToken() != "") {
CoroutineScope(Dispatchers.IO).launch {
try {
RetrofitInstance.authApi.subscribe(
PreferenceHelper.getToken(),
Subscribe(channelId)
)
} catch (e: Exception) {
Log.e(TAG, e.toString())
}
}
} 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) {
Log.e(TAG, e.toString())
}
}
} 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) {
Log.e(TAG, e.toString())
return null
}
return isSubscribed.subscribed
} else {
return PreferenceHelper.getLocalSubscriptions().contains(channelId)
}
}
2022-08-06 14:39:28 +05:30
suspend fun importSubscriptions(channels: List<String>) {
if (PreferenceHelper.getToken() != "") {
val response = try {
val token = PreferenceHelper.getToken()
RetrofitInstance.authApi.importSubscriptions(
false,
token,
channels
)
} catch (e: Exception) {
e.printStackTrace()
}
} else {
val newChannels = PreferenceHelper.getLocalSubscriptions().toMutableList() + channels
PreferenceHelper.setLocalSubscriptions(newChannels)
}
}
2022-08-03 17:11:45 +05:30
fun getFormattedLocalSubscriptions(): String {
return PreferenceHelper.getLocalSubscriptions().joinToString(",")
}
}