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 10e21c839..b520066ff 100644 --- a/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/BackupHelper.kt @@ -11,8 +11,6 @@ import com.github.libretube.db.DatabaseHolder.Companion.Database import com.github.libretube.extensions.TAG import com.github.libretube.obj.BackupFile import com.github.libretube.obj.PreferenceItem -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.runBlocking import kotlinx.serialization.ExperimentalSerializationApi import kotlinx.serialization.json.booleanOrNull import kotlinx.serialization.json.decodeFromStream @@ -43,42 +41,38 @@ object BackupHelper { * Restore data from a [BackupFile] */ @OptIn(ExperimentalSerializationApi::class) - fun restoreAdvancedBackup(context: Context, uri: Uri?) { - val backupFile = uri?.let { - context.contentResolver.openInputStream(it)?.use { inputStream -> - JsonHelper.json.decodeFromStream(inputStream) - } + suspend fun restoreAdvancedBackup(context: Context, uri: Uri) { + val backupFile = context.contentResolver.openInputStream(uri)?.use { + JsonHelper.json.decodeFromStream(it) } ?: return - runBlocking(Dispatchers.IO) { - Database.watchHistoryDao().insertAll( - *backupFile.watchHistory.orEmpty().toTypedArray() - ) - Database.searchHistoryDao().insertAll( - *backupFile.searchHistory.orEmpty().toTypedArray() - ) - Database.watchPositionDao().insertAll( - *backupFile.watchPositions.orEmpty().toTypedArray() - ) - Database.localSubscriptionDao().insertAll(backupFile.localSubscriptions.orEmpty()) - Database.customInstanceDao().insertAll( - *backupFile.customInstances.orEmpty().toTypedArray() - ) - Database.playlistBookmarkDao().insertAll( - *backupFile.playlistBookmarks.orEmpty().toTypedArray() - ) + Database.watchHistoryDao().insertAll( + *backupFile.watchHistory.orEmpty().toTypedArray() + ) + Database.searchHistoryDao().insertAll( + *backupFile.searchHistory.orEmpty().toTypedArray() + ) + Database.watchPositionDao().insertAll( + *backupFile.watchPositions.orEmpty().toTypedArray() + ) + Database.localSubscriptionDao().insertAll(backupFile.localSubscriptions.orEmpty()) + Database.customInstanceDao().insertAll( + *backupFile.customInstances.orEmpty().toTypedArray() + ) + Database.playlistBookmarkDao().insertAll( + *backupFile.playlistBookmarks.orEmpty().toTypedArray() + ) - backupFile.localPlaylists.orEmpty().forEach { - Database.localPlaylistsDao().createPlaylist(it.playlist) - val playlistId = Database.localPlaylistsDao().getAll().last().playlist.id - it.videos.forEach { - it.playlistId = playlistId - Database.localPlaylistsDao().addPlaylistVideo(it) - } + backupFile.localPlaylists.orEmpty().forEach { + Database.localPlaylistsDao().createPlaylist(it.playlist) + val playlistId = Database.localPlaylistsDao().getAll().last().playlist.id + it.videos.forEach { + it.playlistId = playlistId + Database.localPlaylistsDao().addPlaylistVideo(it) } - - restorePreferences(context, backupFile.preferences) } + + restorePreferences(context, backupFile.preferences) } /** diff --git a/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt index 50c4ffed1..77f4150ee 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt @@ -3,6 +3,7 @@ package com.github.libretube.ui.preferences import android.os.Bundle import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts.CreateDocument +import androidx.lifecycle.lifecycleScope import androidx.preference.Preference import com.github.libretube.R import com.github.libretube.helpers.BackupHelper @@ -10,22 +11,32 @@ import com.github.libretube.helpers.ImportHelper import com.github.libretube.obj.BackupFile import com.github.libretube.ui.base.BasePreferenceFragment import com.github.libretube.ui.dialogs.BackupDialog +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch import java.time.LocalDateTime import java.time.format.DateTimeFormatter class BackupRestoreSettings : BasePreferenceFragment() { private val backupDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss") + private var backupFile = BackupFile() override val titleResourceId: Int = R.string.backup_restore // backup and restore database private val getBackupFile = registerForActivityResult(ActivityResultContracts.GetContent()) { - BackupHelper.restoreAdvancedBackup(requireContext(), it) + it?.let { + lifecycleScope.launch(Dispatchers.IO) { + BackupHelper.restoreAdvancedBackup(requireContext(), it) + } + } } private val createBackupFile = registerForActivityResult(CreateDocument(JSON)) { - BackupHelper.createAdvancedBackup(requireContext(), it, backupFile) + it?.let { + lifecycleScope.launch(Dispatchers.IO) { + BackupHelper.createAdvancedBackup(requireContext(), it, backupFile) + } + } } - private var backupFile = BackupFile() /** * result listeners for importing and exporting subscriptions