mirror of
https://github.com/libre-tube/LibreTube.git
synced 2025-04-28 16:00:31 +05:30
Convert LocalSubscriptionDao methods to suspend functions, remove unused methods.
This commit is contained in:
parent
5c82a10c3f
commit
cf9c6ffb99
@ -10,55 +10,40 @@ import com.github.libretube.constants.PreferenceKeys
|
|||||||
import com.github.libretube.db.DatabaseHolder.Companion.Database
|
import com.github.libretube.db.DatabaseHolder.Companion.Database
|
||||||
import com.github.libretube.db.obj.LocalSubscription
|
import com.github.libretube.db.obj.LocalSubscription
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
import com.github.libretube.extensions.awaitQuery
|
|
||||||
import com.github.libretube.extensions.query
|
|
||||||
import com.github.libretube.util.PreferenceHelper
|
import com.github.libretube.util.PreferenceHelper
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
object SubscriptionHelper {
|
object SubscriptionHelper {
|
||||||
|
suspend fun subscribe(channelId: String) {
|
||||||
fun subscribe(channelId: String) {
|
val token = PreferenceHelper.getToken()
|
||||||
if (PreferenceHelper.getToken() != "") {
|
if (token.isNotEmpty()) {
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
try {
|
||||||
try {
|
withContext(Dispatchers.IO) {
|
||||||
RetrofitInstance.authApi.subscribe(
|
RetrofitInstance.authApi.subscribe(token, Subscribe(channelId))
|
||||||
PreferenceHelper.getToken(),
|
|
||||||
Subscribe(channelId)
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG(), e.toString())
|
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG(), e.toString())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
query {
|
Database.localSubscriptionDao().insertAll(listOf(LocalSubscription(channelId)))
|
||||||
Database.localSubscriptionDao().insertAll(
|
|
||||||
LocalSubscription(channelId)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun unsubscribe(channelId: String) {
|
suspend fun unsubscribe(channelId: String) {
|
||||||
if (PreferenceHelper.getToken() != "") {
|
val token = PreferenceHelper.getToken()
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
if (token.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
RetrofitInstance.authApi.unsubscribe(
|
withContext(Dispatchers.IO) {
|
||||||
PreferenceHelper.getToken(),
|
RetrofitInstance.authApi.unsubscribe(token, Subscribe(channelId))
|
||||||
Subscribe(channelId)
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
Log.e(TAG(), e.toString())
|
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.e(TAG(), e.toString())
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
query {
|
Database.localSubscriptionDao().delete(LocalSubscription(channelId))
|
||||||
Database.localSubscriptionDao().delete(
|
|
||||||
LocalSubscription(channelId)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,8 +54,10 @@ object SubscriptionHelper {
|
|||||||
onUnsubscribe: () -> Unit
|
onUnsubscribe: () -> Unit
|
||||||
) {
|
) {
|
||||||
if (!PreferenceHelper.getBoolean(PreferenceKeys.CONFIRM_UNSUBSCRIBE, false)) {
|
if (!PreferenceHelper.getBoolean(PreferenceKeys.CONFIRM_UNSUBSCRIBE, false)) {
|
||||||
unsubscribe(channelId)
|
runBlocking {
|
||||||
onUnsubscribe.invoke()
|
unsubscribe(channelId)
|
||||||
|
onUnsubscribe()
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,89 +65,63 @@ object SubscriptionHelper {
|
|||||||
.setTitle(R.string.unsubscribe)
|
.setTitle(R.string.unsubscribe)
|
||||||
.setMessage(context.getString(R.string.confirm_unsubscribe, channelName))
|
.setMessage(context.getString(R.string.confirm_unsubscribe, channelName))
|
||||||
.setPositiveButton(R.string.unsubscribe) { _, _ ->
|
.setPositiveButton(R.string.unsubscribe) { _, _ ->
|
||||||
unsubscribe(channelId)
|
runBlocking {
|
||||||
onUnsubscribe.invoke()
|
unsubscribe(channelId)
|
||||||
|
onUnsubscribe()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
.show()
|
.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun isSubscribed(channelId: String): Boolean? {
|
suspend fun isSubscribed(channelId: String): Boolean? {
|
||||||
if (PreferenceHelper.getToken() != "") {
|
val token = PreferenceHelper.getToken()
|
||||||
|
if (token.isNotEmpty()) {
|
||||||
val isSubscribed = try {
|
val isSubscribed = try {
|
||||||
RetrofitInstance.authApi.isSubscribed(
|
RetrofitInstance.authApi.isSubscribed(channelId, token)
|
||||||
channelId,
|
|
||||||
PreferenceHelper.getToken()
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG(), e.toString())
|
Log.e(TAG(), e.toString())
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
return isSubscribed.subscribed
|
return isSubscribed.subscribed
|
||||||
} else {
|
} else {
|
||||||
return awaitQuery {
|
return Database.localSubscriptionDao().includes(channelId)
|
||||||
Database.localSubscriptionDao().includes(channelId)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun importSubscriptions(newChannels: List<String>) {
|
suspend fun importSubscriptions(newChannels: List<String>) {
|
||||||
if (PreferenceHelper.getToken() != "") {
|
val token = PreferenceHelper.getToken()
|
||||||
|
if (token.isNotEmpty()) {
|
||||||
try {
|
try {
|
||||||
val token = PreferenceHelper.getToken()
|
RetrofitInstance.authApi.importSubscriptions(false, token, newChannels)
|
||||||
RetrofitInstance.authApi.importSubscriptions(
|
|
||||||
false,
|
|
||||||
token,
|
|
||||||
newChannels
|
|
||||||
)
|
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val newLocalSubscriptions = mutableListOf<LocalSubscription>()
|
Database.localSubscriptionDao().insertAll(newChannels.map { LocalSubscription(it) })
|
||||||
newChannels.forEach {
|
|
||||||
newLocalSubscriptions += LocalSubscription(channelId = it)
|
|
||||||
}
|
|
||||||
query {
|
|
||||||
Database.localSubscriptionDao().insertAll(
|
|
||||||
*newChannels.map { LocalSubscription(it) }.toTypedArray()
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getLocalSubscriptions(): List<LocalSubscription> {
|
suspend fun getFormattedLocalSubscriptions(): String {
|
||||||
return awaitQuery {
|
return Database.localSubscriptionDao().getAll()
|
||||||
Database.localSubscriptionDao().getAll()
|
.joinToString(",") { it.channelId }
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun getFormattedLocalSubscriptions(): String {
|
|
||||||
val localSubscriptions = getLocalSubscriptions()
|
|
||||||
return localSubscriptions.joinToString(",") { it.channelId }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getSubscriptions(): List<Subscription> {
|
suspend fun getSubscriptions(): List<Subscription> {
|
||||||
return if (PreferenceHelper.getToken() != "") {
|
val token = PreferenceHelper.getToken()
|
||||||
RetrofitInstance.authApi.subscriptions(
|
return if (token.isNotEmpty()) {
|
||||||
PreferenceHelper.getToken()
|
RetrofitInstance.authApi.subscriptions(token)
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
RetrofitInstance.authApi.unauthenticatedSubscriptions(
|
RetrofitInstance.authApi.unauthenticatedSubscriptions(getFormattedLocalSubscriptions())
|
||||||
getFormattedLocalSubscriptions()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun getFeed(): List<StreamItem> {
|
suspend fun getFeed(): List<StreamItem> {
|
||||||
return if (PreferenceHelper.getToken() != "") {
|
val token = PreferenceHelper.getToken()
|
||||||
RetrofitInstance.authApi.getFeed(
|
return if (token.isNotEmpty()) {
|
||||||
PreferenceHelper.getToken()
|
RetrofitInstance.authApi.getFeed(token)
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
RetrofitInstance.authApi.getUnauthenticatedFeed(
|
RetrofitInstance.authApi.getUnauthenticatedFeed(getFormattedLocalSubscriptions())
|
||||||
getFormattedLocalSubscriptions()
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,20 +10,14 @@ import com.github.libretube.db.obj.LocalSubscription
|
|||||||
@Dao
|
@Dao
|
||||||
interface LocalSubscriptionDao {
|
interface LocalSubscriptionDao {
|
||||||
@Query("SELECT * FROM localSubscription")
|
@Query("SELECT * FROM localSubscription")
|
||||||
fun getAll(): List<LocalSubscription>
|
suspend fun getAll(): List<LocalSubscription>
|
||||||
|
|
||||||
@Query("SELECT * FROM localSubscription WHERE channelId LIKE :channelId LIMIT 1")
|
|
||||||
fun findById(channelId: String): LocalSubscription
|
|
||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
fun insertAll(vararg localSubscriptions: LocalSubscription)
|
suspend fun insertAll(localSubscriptions: List<LocalSubscription>)
|
||||||
|
|
||||||
@Delete
|
@Delete
|
||||||
fun delete(localSubscription: LocalSubscription)
|
suspend fun delete(localSubscription: LocalSubscription)
|
||||||
|
|
||||||
@Query("DELETE FROM localSubscription")
|
|
||||||
fun deleteAll()
|
|
||||||
|
|
||||||
@Query("SELECT EXISTS(SELECT * FROM localSubscription WHERE channelId = :channelId)")
|
@Query("SELECT EXISTS(SELECT * FROM localSubscription WHERE channelId = :channelId)")
|
||||||
fun includes(channelId: String): Boolean
|
suspend fun includes(channelId: String): Boolean
|
||||||
}
|
}
|
||||||
|
@ -4,20 +4,25 @@ import android.app.Dialog
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.annotation.StringRes
|
import androidx.annotation.StringRes
|
||||||
import androidx.fragment.app.DialogFragment
|
import androidx.fragment.app.DialogFragment
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.github.libretube.R
|
import com.github.libretube.R
|
||||||
import com.github.libretube.db.DatabaseHolder.Companion.Database
|
import com.github.libretube.db.DatabaseHolder.Companion.Database
|
||||||
import com.github.libretube.extensions.awaitQuery
|
|
||||||
import com.github.libretube.obj.BackupFile
|
import com.github.libretube.obj.BackupFile
|
||||||
import com.github.libretube.obj.PreferenceItem
|
import com.github.libretube.obj.PreferenceItem
|
||||||
import com.github.libretube.util.PreferenceHelper
|
import com.github.libretube.util.PreferenceHelper
|
||||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.serialization.json.JsonNull
|
import kotlinx.serialization.json.JsonNull
|
||||||
import kotlinx.serialization.json.JsonPrimitive
|
import kotlinx.serialization.json.JsonPrimitive
|
||||||
|
|
||||||
class BackupDialog(
|
class BackupDialog(
|
||||||
private val createBackupFile: (BackupFile) -> Unit
|
private val createBackupFile: (BackupFile) -> Unit
|
||||||
) : DialogFragment() {
|
) : DialogFragment() {
|
||||||
sealed class BackupOption(@StringRes val name: Int, val onSelected: (BackupFile) -> Unit) {
|
sealed class BackupOption(
|
||||||
|
@StringRes val name: Int,
|
||||||
|
val onSelected: suspend (BackupFile) -> Unit
|
||||||
|
) {
|
||||||
object WatchHistory : BackupOption(R.string.watch_history, onSelected = {
|
object WatchHistory : BackupOption(R.string.watch_history, onSelected = {
|
||||||
it.watchHistory = Database.watchHistoryDao().getAll()
|
it.watchHistory = Database.watchHistoryDao().getAll()
|
||||||
})
|
})
|
||||||
@ -83,9 +88,9 @@ class BackupDialog(
|
|||||||
.setNegativeButton(R.string.cancel, null)
|
.setNegativeButton(R.string.cancel, null)
|
||||||
.setPositiveButton(R.string.backup) { _, _ ->
|
.setPositiveButton(R.string.backup) { _, _ ->
|
||||||
val backupFile = BackupFile()
|
val backupFile = BackupFile()
|
||||||
awaitQuery {
|
lifecycleScope.launch(Dispatchers.IO) {
|
||||||
backupOptions.forEachIndexed { index, option ->
|
backupOptions.forEachIndexed { index, option ->
|
||||||
if (selected[index]) option.onSelected.invoke(backupFile)
|
if (selected[index]) option.onSelected(backupFile)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
createBackupFile(backupFile)
|
createBackupFile(backupFile)
|
||||||
|
@ -8,6 +8,7 @@ import com.google.android.material.button.MaterialButton
|
|||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
||||||
fun TextView.setupSubscriptionButton(
|
fun TextView.setupSubscriptionButton(
|
||||||
@ -42,10 +43,12 @@ fun TextView.setupSubscriptionButton(
|
|||||||
subscribed = false
|
subscribed = false
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
SubscriptionHelper.subscribe(channelId)
|
runBlocking {
|
||||||
this.text = context.getString(R.string.unsubscribe)
|
SubscriptionHelper.subscribe(channelId)
|
||||||
notificationBell?.visibility = View.VISIBLE
|
text = context.getString(R.string.unsubscribe)
|
||||||
subscribed = true
|
notificationBell?.visibility = View.VISIBLE
|
||||||
|
subscribed = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,13 +3,13 @@ package com.github.libretube.ui.models
|
|||||||
import android.util.Log
|
import android.util.Log
|
||||||
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.MutableLiveData
|
||||||
import androidx.lifecycle.ViewModel
|
import androidx.lifecycle.ViewModel
|
||||||
|
import androidx.lifecycle.viewModelScope
|
||||||
import com.github.libretube.api.SubscriptionHelper
|
import com.github.libretube.api.SubscriptionHelper
|
||||||
import com.github.libretube.api.obj.StreamItem
|
import com.github.libretube.api.obj.StreamItem
|
||||||
import com.github.libretube.api.obj.Subscription
|
import com.github.libretube.api.obj.Subscription
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
import com.github.libretube.extensions.toID
|
import com.github.libretube.extensions.toID
|
||||||
import com.github.libretube.util.PreferenceHelper
|
import com.github.libretube.util.PreferenceHelper
|
||||||
import kotlinx.coroutines.CoroutineScope
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
@ -27,7 +27,7 @@ class SubscriptionsViewModel : ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun fetchFeed() {
|
fun fetchFeed() {
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
val videoFeed = try {
|
val videoFeed = try {
|
||||||
SubscriptionHelper.getFeed()
|
SubscriptionHelper.getFeed()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
@ -44,7 +44,7 @@ class SubscriptionsViewModel : ViewModel() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun fetchSubscriptions() {
|
fun fetchSubscriptions() {
|
||||||
CoroutineScope(Dispatchers.IO).launch {
|
viewModelScope.launch(Dispatchers.IO) {
|
||||||
val subscriptions = try {
|
val subscriptions = try {
|
||||||
SubscriptionHelper.getSubscriptions()
|
SubscriptionHelper.getSubscriptions()
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
@ -9,9 +9,10 @@ import com.github.libretube.api.JsonHelper
|
|||||||
import com.github.libretube.constants.PreferenceKeys
|
import com.github.libretube.constants.PreferenceKeys
|
||||||
import com.github.libretube.db.DatabaseHolder.Companion.Database
|
import com.github.libretube.db.DatabaseHolder.Companion.Database
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
import com.github.libretube.extensions.query
|
|
||||||
import com.github.libretube.obj.BackupFile
|
import com.github.libretube.obj.BackupFile
|
||||||
import com.github.libretube.obj.PreferenceItem
|
import com.github.libretube.obj.PreferenceItem
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.serialization.json.booleanOrNull
|
import kotlinx.serialization.json.booleanOrNull
|
||||||
import kotlinx.serialization.json.decodeFromStream
|
import kotlinx.serialization.json.decodeFromStream
|
||||||
import kotlinx.serialization.json.encodeToStream
|
import kotlinx.serialization.json.encodeToStream
|
||||||
@ -48,7 +49,7 @@ class BackupHelper(private val context: Context) {
|
|||||||
}
|
}
|
||||||
} ?: return
|
} ?: return
|
||||||
|
|
||||||
query {
|
runBlocking(Dispatchers.IO) {
|
||||||
Database.watchHistoryDao().insertAll(
|
Database.watchHistoryDao().insertAll(
|
||||||
*backupFile.watchHistory.toTypedArray()
|
*backupFile.watchHistory.toTypedArray()
|
||||||
)
|
)
|
||||||
@ -58,9 +59,7 @@ class BackupHelper(private val context: Context) {
|
|||||||
Database.watchPositionDao().insertAll(
|
Database.watchPositionDao().insertAll(
|
||||||
*backupFile.watchPositions.toTypedArray()
|
*backupFile.watchPositions.toTypedArray()
|
||||||
)
|
)
|
||||||
Database.localSubscriptionDao().insertAll(
|
Database.localSubscriptionDao().insertAll(backupFile.localSubscriptions)
|
||||||
*backupFile.localSubscriptions.toTypedArray()
|
|
||||||
)
|
|
||||||
Database.customInstanceDao().insertAll(
|
Database.customInstanceDao().insertAll(
|
||||||
*backupFile.customInstances.toTypedArray()
|
*backupFile.customInstances.toTypedArray()
|
||||||
)
|
)
|
||||||
|
@ -84,9 +84,10 @@ class ImportHelper(
|
|||||||
*/
|
*/
|
||||||
fun exportSubscriptions(uri: Uri?) {
|
fun exportSubscriptions(uri: Uri?) {
|
||||||
if (uri == null) return
|
if (uri == null) return
|
||||||
runBlocking {
|
runBlocking(Dispatchers.IO) {
|
||||||
val subs = if (PreferenceHelper.getToken() != "") {
|
val token = PreferenceHelper.getToken()
|
||||||
RetrofitInstance.authApi.subscriptions(PreferenceHelper.getToken())
|
val subs = if (token.isNotEmpty()) {
|
||||||
|
RetrofitInstance.authApi.subscriptions(token)
|
||||||
} else {
|
} else {
|
||||||
RetrofitInstance.authApi.unauthenticatedSubscriptions(
|
RetrofitInstance.authApi.unauthenticatedSubscriptions(
|
||||||
SubscriptionHelper.getFormattedLocalSubscriptions()
|
SubscriptionHelper.getFormattedLocalSubscriptions()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user