Merge pull request #1060 from Bnyro/master

migrate local subscriptions to database
This commit is contained in:
Bnyro 2022-08-14 11:39:14 +02:00 committed by GitHub
commit 3e7c1ec485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 87 additions and 30 deletions

View File

@ -1,7 +1,10 @@
package com.github.libretube.api
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.await
import com.github.libretube.obj.Subscribe
import com.github.libretube.preferences.PreferenceHelper
import kotlinx.coroutines.CoroutineScope
@ -23,9 +26,11 @@ object SubscriptionHelper {
}
}
} else {
val channels = PreferenceHelper.getLocalSubscriptions().toMutableList()
channels.add(channelId)
PreferenceHelper.setLocalSubscriptions(channels)
Thread {
DatabaseHolder.db.localSubscriptionDao().insertAll(
LocalSubscription(channelId)
)
}.start()
}
}
@ -42,9 +47,11 @@ object SubscriptionHelper {
}
}
} else {
val channels = PreferenceHelper.getLocalSubscriptions().toMutableList()
channels.remove(channelId)
PreferenceHelper.setLocalSubscriptions(channels)
Thread {
DatabaseHolder.db.localSubscriptionDao().delete(
LocalSubscription(channelId)
)
}.start()
}
}
@ -61,7 +68,11 @@ object SubscriptionHelper {
}
return isSubscribed.subscribed
} 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()
}
} else {
val channels = PreferenceHelper.getLocalSubscriptions().toMutableList()
val newLocalSubscriptions = mutableListOf<LocalSubscription>()
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 {
return PreferenceHelper.getLocalSubscriptions().joinToString(",")
val localSubscriptions = getLocalSubscriptions()
return localSubscriptions.map { it.channelId }.joinToString(",")
}
}

View File

@ -1,12 +1,15 @@
package com.github.libretube.db
import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.RoomDatabase
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.WatchHistoryDao
import com.github.libretube.db.dao.WatchPositionDao
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.WatchHistoryItem
import com.github.libretube.db.obj.WatchPosition
@ -16,9 +19,13 @@ import com.github.libretube.db.obj.WatchPosition
WatchHistoryItem::class,
WatchPosition::class,
SearchHistoryItem::class,
CustomInstance::class
CustomInstance::class,
LocalSubscription::class
],
version = 6
version = 7,
autoMigrations = [
AutoMigration(from = 7, to = 8)
]
)
abstract class AppDatabase : RoomDatabase() {
/**
@ -40,4 +47,9 @@ abstract class AppDatabase : RoomDatabase() {
* Custom Instances
*/
abstract fun customInstanceDao(): CustomInstanceDao
/**
* Local Subscriptions
*/
abstract fun localSubscriptionDao(): LocalSubscriptionDao
}

View File

@ -13,7 +13,6 @@ object DatabaseHolder {
AppDatabase::class.java,
DATABASE_NAME
)
.fallbackToDestructiveMigration()
.build()
}
}

View File

@ -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
}

View File

@ -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 = ""
)

View File

@ -3,7 +3,6 @@ package com.github.libretube.preferences
import android.content.Context
import android.content.SharedPreferences
import androidx.preference.PreferenceManager
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
object PreferenceHelper {
@ -80,21 +79,6 @@ object PreferenceHelper {
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 {
return PreferenceManager.getDefaultSharedPreferences(context)
}