Allow creating backups of subscripton groups

This commit is contained in:
Bnyro 2023-03-28 17:45:21 +02:00
parent 4402bf1baf
commit a4088189bf
6 changed files with 20 additions and 8 deletions

View File

@ -14,6 +14,9 @@ interface SubscriptionGroupsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun createGroup(subscriptionGroup: SubscriptionGroup)
@Insert
suspend fun insertAll(subscriptionGroups: List<SubscriptionGroup>)
@Query("DELETE FROM subscriptionGroups WHERE name = :name")
suspend fun deleteGroup(name: String)
}

View File

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

View File

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

View File

@ -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<CustomInstance>? = emptyList(),
var playlistBookmarks: List<PlaylistBookmark>? = emptyList(),
var localPlaylists: List<LocalPlaylistWithVideos>? = emptyList(),
var preferences: List<PreferenceItem>? = emptyList()
var preferences: List<PreferenceItem>? = emptyList(),
var channelGroups: List<SubscriptionGroup>? = emptyList()
)

View File

@ -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()

View File

@ -460,7 +460,6 @@
<string name="new_group">New</string>
<string name="group_name">Group name</string>
<string name="edit_group">Edit group</string>
<string name="group_already_exists">Group already exists!</string>
<!-- Notification channel strings -->
<string name="download_channel_name">Download Service</string>