mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-15 14:50:30 +05:30
Merge pull request #1060 from Bnyro/master
migrate local subscriptions to database
This commit is contained in:
commit
3e7c1ec485
@ -1,7 +1,10 @@
|
|||||||
package com.github.libretube.api
|
package com.github.libretube.api
|
||||||
|
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import com.github.libretube.db.DatabaseHolder
|
||||||
|
import com.github.libretube.db.obj.LocalSubscription
|
||||||
import com.github.libretube.extensions.TAG
|
import com.github.libretube.extensions.TAG
|
||||||
|
import com.github.libretube.extensions.await
|
||||||
import com.github.libretube.obj.Subscribe
|
import com.github.libretube.obj.Subscribe
|
||||||
import com.github.libretube.preferences.PreferenceHelper
|
import com.github.libretube.preferences.PreferenceHelper
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
@ -23,9 +26,11 @@ object SubscriptionHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val channels = PreferenceHelper.getLocalSubscriptions().toMutableList()
|
Thread {
|
||||||
channels.add(channelId)
|
DatabaseHolder.db.localSubscriptionDao().insertAll(
|
||||||
PreferenceHelper.setLocalSubscriptions(channels)
|
LocalSubscription(channelId)
|
||||||
|
)
|
||||||
|
}.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,9 +47,11 @@ object SubscriptionHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val channels = PreferenceHelper.getLocalSubscriptions().toMutableList()
|
Thread {
|
||||||
channels.remove(channelId)
|
DatabaseHolder.db.localSubscriptionDao().delete(
|
||||||
PreferenceHelper.setLocalSubscriptions(channels)
|
LocalSubscription(channelId)
|
||||||
|
)
|
||||||
|
}.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,7 +68,11 @@ object SubscriptionHelper {
|
|||||||
}
|
}
|
||||||
return isSubscribed.subscribed
|
return isSubscribed.subscribed
|
||||||
} else {
|
} else {
|
||||||
return PreferenceHelper.getLocalSubscriptions().contains(channelId)
|
var isSubscribed = false
|
||||||
|
Thread {
|
||||||
|
isSubscribed = DatabaseHolder.db.localSubscriptionDao().includes(channelId)
|
||||||
|
}.await()
|
||||||
|
return isSubscribed
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -78,15 +89,28 @@ object SubscriptionHelper {
|
|||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
val channels = PreferenceHelper.getLocalSubscriptions().toMutableList()
|
val newLocalSubscriptions = mutableListOf<LocalSubscription>()
|
||||||
newChannels.forEach {
|
newChannels.forEach {
|
||||||
if (!channels.contains(it)) channels += it
|
newLocalSubscriptions += LocalSubscription(channelId = it)
|
||||||
}
|
}
|
||||||
PreferenceHelper.setLocalSubscriptions(channels)
|
Thread {
|
||||||
|
DatabaseHolder.db.localSubscriptionDao().insertAll(
|
||||||
|
*newChannels.map { LocalSubscription(it) }.toTypedArray()
|
||||||
|
)
|
||||||
|
}.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun getLocalSubscriptions(): List<LocalSubscription> {
|
||||||
|
var localSubscriptions = listOf<LocalSubscription>()
|
||||||
|
Thread {
|
||||||
|
localSubscriptions = DatabaseHolder.db.localSubscriptionDao().getAll()
|
||||||
|
}.await()
|
||||||
|
return localSubscriptions
|
||||||
|
}
|
||||||
|
|
||||||
fun getFormattedLocalSubscriptions(): String {
|
fun getFormattedLocalSubscriptions(): String {
|
||||||
return PreferenceHelper.getLocalSubscriptions().joinToString(",")
|
val localSubscriptions = getLocalSubscriptions()
|
||||||
|
return localSubscriptions.map { it.channelId }.joinToString(",")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,12 +1,15 @@
|
|||||||
package com.github.libretube.db
|
package com.github.libretube.db
|
||||||
|
|
||||||
|
import androidx.room.AutoMigration
|
||||||
import androidx.room.Database
|
import androidx.room.Database
|
||||||
import androidx.room.RoomDatabase
|
import androidx.room.RoomDatabase
|
||||||
import com.github.libretube.db.dao.CustomInstanceDao
|
import com.github.libretube.db.dao.CustomInstanceDao
|
||||||
|
import com.github.libretube.db.dao.LocalSubscriptionDao
|
||||||
import com.github.libretube.db.dao.SearchHistoryDao
|
import com.github.libretube.db.dao.SearchHistoryDao
|
||||||
import com.github.libretube.db.dao.WatchHistoryDao
|
import com.github.libretube.db.dao.WatchHistoryDao
|
||||||
import com.github.libretube.db.dao.WatchPositionDao
|
import com.github.libretube.db.dao.WatchPositionDao
|
||||||
import com.github.libretube.db.obj.CustomInstance
|
import com.github.libretube.db.obj.CustomInstance
|
||||||
|
import com.github.libretube.db.obj.LocalSubscription
|
||||||
import com.github.libretube.db.obj.SearchHistoryItem
|
import com.github.libretube.db.obj.SearchHistoryItem
|
||||||
import com.github.libretube.db.obj.WatchHistoryItem
|
import com.github.libretube.db.obj.WatchHistoryItem
|
||||||
import com.github.libretube.db.obj.WatchPosition
|
import com.github.libretube.db.obj.WatchPosition
|
||||||
@ -16,9 +19,13 @@ import com.github.libretube.db.obj.WatchPosition
|
|||||||
WatchHistoryItem::class,
|
WatchHistoryItem::class,
|
||||||
WatchPosition::class,
|
WatchPosition::class,
|
||||||
SearchHistoryItem::class,
|
SearchHistoryItem::class,
|
||||||
CustomInstance::class
|
CustomInstance::class,
|
||||||
|
LocalSubscription::class
|
||||||
],
|
],
|
||||||
version = 6
|
version = 7,
|
||||||
|
autoMigrations = [
|
||||||
|
AutoMigration(from = 7, to = 8)
|
||||||
|
]
|
||||||
)
|
)
|
||||||
abstract class AppDatabase : RoomDatabase() {
|
abstract class AppDatabase : RoomDatabase() {
|
||||||
/**
|
/**
|
||||||
@ -40,4 +47,9 @@ abstract class AppDatabase : RoomDatabase() {
|
|||||||
* Custom Instances
|
* Custom Instances
|
||||||
*/
|
*/
|
||||||
abstract fun customInstanceDao(): CustomInstanceDao
|
abstract fun customInstanceDao(): CustomInstanceDao
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Local Subscriptions
|
||||||
|
*/
|
||||||
|
abstract fun localSubscriptionDao(): LocalSubscriptionDao
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,6 @@ object DatabaseHolder {
|
|||||||
AppDatabase::class.java,
|
AppDatabase::class.java,
|
||||||
DATABASE_NAME
|
DATABASE_NAME
|
||||||
)
|
)
|
||||||
.fallbackToDestructiveMigration()
|
|
||||||
.build()
|
.build()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,29 @@
|
|||||||
|
package com.github.libretube.db.dao
|
||||||
|
|
||||||
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Delete
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.OnConflictStrategy
|
||||||
|
import androidx.room.Query
|
||||||
|
import com.github.libretube.db.obj.LocalSubscription
|
||||||
|
|
||||||
|
@Dao
|
||||||
|
interface LocalSubscriptionDao {
|
||||||
|
@Query("SELECT * FROM localSubscription")
|
||||||
|
fun getAll(): List<LocalSubscription>
|
||||||
|
|
||||||
|
@Query("SELECT * FROM localSubscription WHERE channelId LIKE :channelId LIMIT 1")
|
||||||
|
fun findById(channelId: String): LocalSubscription
|
||||||
|
|
||||||
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
|
fun insertAll(vararg localSubscriptions: LocalSubscription)
|
||||||
|
|
||||||
|
@Delete
|
||||||
|
fun delete(localSubscription: LocalSubscription)
|
||||||
|
|
||||||
|
@Query("DELETE FROM localSubscription")
|
||||||
|
fun deleteAll()
|
||||||
|
|
||||||
|
@Query("SELECT EXISTS(SELECT * FROM localSubscription WHERE channelId = :channelId)")
|
||||||
|
fun includes(channelId: String): Boolean
|
||||||
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package com.github.libretube.db.obj
|
||||||
|
|
||||||
|
import androidx.room.Entity
|
||||||
|
import androidx.room.PrimaryKey
|
||||||
|
|
||||||
|
@Entity(tableName = "localSubscription")
|
||||||
|
data class LocalSubscription(
|
||||||
|
@PrimaryKey val channelId: String = ""
|
||||||
|
)
|
@ -3,7 +3,6 @@ package com.github.libretube.preferences
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import androidx.preference.PreferenceManager
|
import androidx.preference.PreferenceManager
|
||||||
import com.fasterxml.jackson.core.type.TypeReference
|
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper
|
import com.fasterxml.jackson.databind.ObjectMapper
|
||||||
|
|
||||||
object PreferenceHelper {
|
object PreferenceHelper {
|
||||||
@ -80,21 +79,6 @@ object PreferenceHelper {
|
|||||||
return getString(PreferenceKeys.ERROR_LOG, "")
|
return getString(PreferenceKeys.ERROR_LOG, "")
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getLocalSubscriptions(): List<String> {
|
|
||||||
val json = settings.getString(PreferenceKeys.LOCAL_SUBSCRIPTIONS, "")
|
|
||||||
return try {
|
|
||||||
val type = object : TypeReference<List<String>>() {}
|
|
||||||
mapper.readValue(json, type)
|
|
||||||
} catch (e: Exception) {
|
|
||||||
listOf()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun setLocalSubscriptions(channels: List<String>) {
|
|
||||||
val json = mapper.writeValueAsString(channels)
|
|
||||||
editor.putString(PreferenceKeys.LOCAL_SUBSCRIPTIONS, json).commit()
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
|
private fun getDefaultSharedPreferences(context: Context): SharedPreferences {
|
||||||
return PreferenceManager.getDefaultSharedPreferences(context)
|
return PreferenceManager.getDefaultSharedPreferences(context)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user