From d1979c21e02583d055d5531dd686babd80a2391a Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 17 Sep 2023 15:18:33 +0200 Subject: [PATCH] fix: crash when restoring backups --- .../libretube/ui/dialogs/BackupDialog.kt | 50 +++++++++++-------- 1 file changed, 30 insertions(+), 20 deletions(-) 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 87d48c5ca..ed2c30fc3 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 @@ -1,6 +1,7 @@ package com.github.libretube.ui.dialogs import android.app.Dialog +import android.content.DialogInterface import android.os.Bundle import androidx.annotation.StringRes import androidx.core.os.bundleOf @@ -26,39 +27,39 @@ class BackupDialog : DialogFragment() { @StringRes val name: Int, 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() }) - object WatchPositions : BackupOption(R.string.watch_positions, onSelected = { + data object WatchPositions : BackupOption(R.string.watch_positions, onSelected = { 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() }) - object LocalSubscriptions : BackupOption(R.string.local_subscriptions, onSelected = { + data object LocalSubscriptions : BackupOption(R.string.local_subscriptions, onSelected = { 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() }) - object PlaylistBookmarks : BackupOption(R.string.bookmarks, onSelected = { + data object PlaylistBookmarks : BackupOption(R.string.bookmarks, onSelected = { 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() }) - object SubscriptionGroups : BackupOption(R.string.channel_groups, onSelected = { + data object SubscriptionGroups : BackupOption(R.string.channel_groups, onSelected = { 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) -> val jsonValue = when (value) { is Number -> JsonPrimitive(value) @@ -94,20 +95,29 @@ class BackupDialog : DialogFragment() { selected[index] = newValue } .setNegativeButton(R.string.cancel, null) - .setPositiveButton(R.string.backup) { _, _ -> - val backupFile = BackupFile() - lifecycleScope.launch(Dispatchers.IO) { - backupOptions.forEachIndexed { index, option -> - if (selected[index]) option.onSelected(backupFile) + .setPositiveButton(R.string.backup, null) + .show() + .apply { + getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener { + 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 {