diff --git a/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt b/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt index b4b9203ac..aec89d9e8 100644 --- a/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt +++ b/app/src/main/java/com/github/libretube/api/SubscriptionHelper.kt @@ -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() 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 { + var localSubscriptions = listOf() + 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(",") } } diff --git a/app/src/main/java/com/github/libretube/db/AppDatabase.kt b/app/src/main/java/com/github/libretube/db/AppDatabase.kt index e919953ea..6ecbfbe22 100644 --- a/app/src/main/java/com/github/libretube/db/AppDatabase.kt +++ b/app/src/main/java/com/github/libretube/db/AppDatabase.kt @@ -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 } diff --git a/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt b/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt index 2d92fdbcc..1bf4db5ca 100644 --- a/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt +++ b/app/src/main/java/com/github/libretube/db/DatabaseHolder.kt @@ -13,7 +13,6 @@ object DatabaseHolder { AppDatabase::class.java, DATABASE_NAME ) - .fallbackToDestructiveMigration() .build() } } diff --git a/app/src/main/java/com/github/libretube/db/dao/LocalSubscriptionDao.kt b/app/src/main/java/com/github/libretube/db/dao/LocalSubscriptionDao.kt new file mode 100644 index 000000000..a925f3535 --- /dev/null +++ b/app/src/main/java/com/github/libretube/db/dao/LocalSubscriptionDao.kt @@ -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 + + @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 +} diff --git a/app/src/main/java/com/github/libretube/db/obj/LocalSubscription.kt b/app/src/main/java/com/github/libretube/db/obj/LocalSubscription.kt new file mode 100644 index 000000000..681779ad0 --- /dev/null +++ b/app/src/main/java/com/github/libretube/db/obj/LocalSubscription.kt @@ -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 = "" +) diff --git a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt index 9fe00b195..284d05666 100644 --- a/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt +++ b/app/src/main/java/com/github/libretube/preferences/PreferenceHelper.kt @@ -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 { - val json = settings.getString(PreferenceKeys.LOCAL_SUBSCRIPTIONS, "") - return try { - val type = object : TypeReference>() {} - mapper.readValue(json, type) - } catch (e: Exception) { - listOf() - } - } - - fun setLocalSubscriptions(channels: List) { - val json = mapper.writeValueAsString(channels) - editor.putString(PreferenceKeys.LOCAL_SUBSCRIPTIONS, json).commit() - } - private fun getDefaultSharedPreferences(context: Context): SharedPreferences { return PreferenceManager.getDefaultSharedPreferences(context) }