Merge pull request #4799 from Bnyro/master

fix: crash when restoring backups
This commit is contained in:
Bnyro 2023-09-17 15:17:53 +02:00 committed by GitHub
commit faa4418888
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
package com.github.libretube.ui.dialogs package com.github.libretube.ui.dialogs
import android.app.Dialog import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle import android.os.Bundle
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
@ -26,39 +27,39 @@ class BackupDialog : DialogFragment() {
@StringRes val name: Int, @StringRes val name: Int,
val onSelected: suspend (BackupFile) -> Unit val onSelected: suspend (BackupFile) -> Unit
) { ) {
object WatchHistory : BackupOption(R.string.watch_history, onSelected = { data object WatchHistory : BackupOption(R.string.watch_history, onSelected = {
it.watchHistory = Database.watchHistoryDao().getAll() it.watchHistory = Database.watchHistoryDao().getAll()
}) })
object WatchPositions : BackupOption(R.string.watch_positions, onSelected = { data object WatchPositions : BackupOption(R.string.watch_positions, onSelected = {
it.watchPositions = Database.watchPositionDao().getAll() it.watchPositions = Database.watchPositionDao().getAll()
}) })
object SearchHistory : BackupOption(R.string.search_history, onSelected = { data object SearchHistory : BackupOption(R.string.search_history, onSelected = {
it.searchHistory = Database.searchHistoryDao().getAll() it.searchHistory = Database.searchHistoryDao().getAll()
}) })
object LocalSubscriptions : BackupOption(R.string.local_subscriptions, onSelected = { data object LocalSubscriptions : BackupOption(R.string.local_subscriptions, onSelected = {
it.localSubscriptions = Database.localSubscriptionDao().getAll() it.localSubscriptions = Database.localSubscriptionDao().getAll()
}) })
object CustomInstances : BackupOption(R.string.backup_customInstances, onSelected = { data object CustomInstances : BackupOption(R.string.backup_customInstances, onSelected = {
it.customInstances = Database.customInstanceDao().getAll() it.customInstances = Database.customInstanceDao().getAll()
}) })
object PlaylistBookmarks : BackupOption(R.string.bookmarks, onSelected = { data object PlaylistBookmarks : BackupOption(R.string.bookmarks, onSelected = {
it.playlistBookmarks = Database.playlistBookmarkDao().getAll() it.playlistBookmarks = Database.playlistBookmarkDao().getAll()
}) })
object LocalPlaylists : BackupOption(R.string.local_playlists, onSelected = { data object LocalPlaylists : BackupOption(R.string.local_playlists, onSelected = {
it.localPlaylists = Database.localPlaylistsDao().getAll() it.localPlaylists = Database.localPlaylistsDao().getAll()
}) })
object SubscriptionGroups : BackupOption(R.string.channel_groups, onSelected = { data object SubscriptionGroups : BackupOption(R.string.channel_groups, onSelected = {
it.channelGroups = Database.subscriptionGroupsDao().getAll() it.channelGroups = Database.subscriptionGroupsDao().getAll()
}) })
object Preferences : BackupOption(R.string.preferences, onSelected = { file -> data object Preferences : BackupOption(R.string.preferences, onSelected = { file ->
file.preferences = PreferenceHelper.settings.all.map { (key, value) -> file.preferences = PreferenceHelper.settings.all.map { (key, value) ->
val jsonValue = when (value) { val jsonValue = when (value) {
is Number -> JsonPrimitive(value) is Number -> JsonPrimitive(value)
@ -94,20 +95,29 @@ class BackupDialog : DialogFragment() {
selected[index] = newValue selected[index] = newValue
} }
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)
.setPositiveButton(R.string.backup) { _, _ -> .setPositiveButton(R.string.backup, null)
val backupFile = BackupFile() .show()
lifecycleScope.launch(Dispatchers.IO) { .apply {
backupOptions.forEachIndexed { index, option -> getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
if (selected[index]) option.onSelected(backupFile) requireDialog().hide()
lifecycleScope.launch(Dispatchers.IO) {
val backupFile = BackupFile()
backupOptions.forEachIndexed { index, option ->
if (selected[index]) option.onSelected(backupFile)
}
val encodedBackupFile = Json.encodeToString(backupFile)
setFragmentResult(
BACKUP_DIALOG_REQUEST_KEY,
bundleOf(IntentData.backupFile to encodedBackupFile)
)
dialog?.dismiss()
} }
val encodedBackupFile = Json.encodeToString(backupFile)
setFragmentResult(
BACKUP_DIALOG_REQUEST_KEY,
bundleOf(IntentData.backupFile to encodedBackupFile)
)
} }
} }
.create()
} }
companion object { companion object {