From a4088189bf63e7950935a575e133df45b0df207b Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 28 Mar 2023 17:45:21 +0200 Subject: [PATCH] Allow creating backups of subscripton groups --- .../github/libretube/db/dao/SubscriptionGroupsDao.kt | 3 +++ .../com/github/libretube/db/obj/SubscriptionGroup.kt | 2 ++ .../java/com/github/libretube/helpers/BackupHelper.kt | 7 ++++--- .../main/java/com/github/libretube/obj/BackupFile.kt | 4 +++- .../com/github/libretube/ui/dialogs/BackupDialog.kt | 11 ++++++++--- app/src/main/res/values/strings.xml | 1 - 6 files changed, 20 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/com/github/libretube/db/dao/SubscriptionGroupsDao.kt b/app/src/main/java/com/github/libretube/db/dao/SubscriptionGroupsDao.kt index 22fc79a37..8e9be5f27 100644 --- a/app/src/main/java/com/github/libretube/db/dao/SubscriptionGroupsDao.kt +++ b/app/src/main/java/com/github/libretube/db/dao/SubscriptionGroupsDao.kt @@ -14,6 +14,9 @@ interface SubscriptionGroupsDao { @Insert(onConflict = OnConflictStrategy.REPLACE) suspend fun createGroup(subscriptionGroup: SubscriptionGroup) + @Insert + suspend fun insertAll(subscriptionGroups: List) + @Query("DELETE FROM subscriptionGroups WHERE name = :name") suspend fun deleteGroup(name: String) } diff --git a/app/src/main/java/com/github/libretube/db/obj/SubscriptionGroup.kt b/app/src/main/java/com/github/libretube/db/obj/SubscriptionGroup.kt index 75eedadec..7569a28ec 100644 --- a/app/src/main/java/com/github/libretube/db/obj/SubscriptionGroup.kt +++ b/app/src/main/java/com/github/libretube/db/obj/SubscriptionGroup.kt @@ -2,7 +2,9 @@ package com.github.libretube.db.obj import androidx.room.Entity import androidx.room.PrimaryKey +import kotlinx.serialization.Serializable +@Serializable @Entity(tableName = "subscriptionGroups") data class SubscriptionGroup( @PrimaryKey var name: String, diff --git a/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt b/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt index 1b4ec02dc..fb25d7676 100644 --- a/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt @@ -52,13 +52,14 @@ object BackupHelper { Database.localSubscriptionDao().insertAll(backupFile.localSubscriptions.orEmpty()) Database.customInstanceDao().insertAll(backupFile.customInstances.orEmpty()) Database.playlistBookmarkDao().insertAll(backupFile.playlistBookmarks.orEmpty()) + Database.subscriptionGroupsDao().insertAll(backupFile.channelGroups.orEmpty()) backupFile.localPlaylists?.forEach { Database.localPlaylistsDao().createPlaylist(it.playlist) val playlistId = Database.localPlaylistsDao().getAll().last().playlist.id - it.videos.forEach { - it.playlistId = playlistId - Database.localPlaylistsDao().addPlaylistVideo(it) + it.videos.forEach { playlistItem -> + playlistItem.playlistId = playlistId + Database.localPlaylistsDao().addPlaylistVideo(playlistItem) } } diff --git a/app/src/main/java/com/github/libretube/obj/BackupFile.kt b/app/src/main/java/com/github/libretube/obj/BackupFile.kt index 0768c2724..503a3f318 100644 --- a/app/src/main/java/com/github/libretube/obj/BackupFile.kt +++ b/app/src/main/java/com/github/libretube/obj/BackupFile.kt @@ -5,6 +5,7 @@ import com.github.libretube.db.obj.LocalPlaylistWithVideos import com.github.libretube.db.obj.LocalSubscription import com.github.libretube.db.obj.PlaylistBookmark import com.github.libretube.db.obj.SearchHistoryItem +import com.github.libretube.db.obj.SubscriptionGroup import com.github.libretube.db.obj.WatchHistoryItem import com.github.libretube.db.obj.WatchPosition import kotlinx.serialization.Serializable @@ -18,5 +19,6 @@ data class BackupFile( var customInstances: List? = emptyList(), var playlistBookmarks: List? = emptyList(), var localPlaylists: List? = emptyList(), - var preferences: List? = emptyList() + var preferences: List? = emptyList(), + var channelGroups: List? = emptyList() ) diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt index 8328c99be..d5626bcb3 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt @@ -17,11 +17,11 @@ import kotlinx.serialization.json.JsonNull import kotlinx.serialization.json.JsonPrimitive class BackupDialog( - private val createBackupFile: (BackupFile) -> Unit + private val createBackupFile: (BackupFile) -> Unit, ) : DialogFragment() { sealed class BackupOption( @StringRes val name: Int, - val onSelected: suspend (BackupFile) -> Unit + val onSelected: suspend (BackupFile) -> Unit, ) { object WatchHistory : BackupOption(R.string.watch_history, onSelected = { it.watchHistory = Database.watchHistoryDao().getAll() @@ -51,6 +51,10 @@ class BackupDialog( it.localPlaylists = Database.localPlaylistsDao().getAll() }) + object SubscriptionGroups : BackupOption(R.string.channel_groups, onSelected = { + it.channelGroups = Database.subscriptionGroupsDao().getAll() + }) + object Preferences : BackupOption(R.string.preferences, onSelected = { file -> file.preferences = PreferenceHelper.settings.all.map { (key, value) -> val jsonValue = when (value) { @@ -73,7 +77,8 @@ class BackupDialog( BackupOption.CustomInstances, BackupOption.PlaylistBookmarks, BackupOption.LocalPlaylists, - BackupOption.Preferences + BackupOption.SubscriptionGroups, + BackupOption.Preferences, ) val backupItems = backupOptions.map { context?.getString(it.name)!! }.toTypedArray() diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c15b448ef..8ecc5c8b0 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -460,7 +460,6 @@ New Group name Edit group - Group already exists! Download Service