From 4a04d9ebb5fa917cf011092bc6f9931568666da5 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 13 Aug 2021 11:56:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/config/BackupConfigFragment.kt | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt b/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt index 18c274561..651c8b042 100644 --- a/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt +++ b/app/src/main/java/io/legado/app/ui/config/BackupConfigFragment.kt @@ -9,6 +9,7 @@ import android.view.MenuInflater import android.view.MenuItem import android.view.View import androidx.documentfile.provider.DocumentFile +import androidx.lifecycle.lifecycleScope import androidx.preference.EditTextPreference import androidx.preference.ListPreference import androidx.preference.Preference @@ -30,7 +31,8 @@ import io.legado.app.lib.theme.accentColor import io.legado.app.ui.document.FilePicker import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.* -import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.Dispatchers.Main +import kotlinx.coroutines.launch import splitties.init.appCtx class BackupConfigFragment : BasePreferenceFragment(), @@ -265,26 +267,38 @@ class BackupConfigFragment : BasePreferenceFragment(), } fun restore() { - Coroutine.async(context = Dispatchers.Main) { + Coroutine.async(context = Main) { BookWebDav.showRestoreDialog(requireContext()) }.onError { - longToast("WebDavError:${it.localizedMessage}\n将从本地备份恢复。") - val backupPath = getPrefString(PreferKey.backupPath) - if (backupPath?.isNotEmpty() == true) { - if (backupPath.isContentScheme()) { - val uri = Uri.parse(backupPath) - val doc = DocumentFile.fromTreeUri(requireContext(), uri) - if (doc?.canWrite() == true) { + alert { + setTitle(R.string.restore) + setMessage("WebDavError:${it.localizedMessage}\n将从本地备份恢复。") + okButton { + restoreFromLocal() + } + cancelButton() + } + } + } + + private fun restoreFromLocal() { + val backupPath = getPrefString(PreferKey.backupPath) + if (backupPath?.isNotEmpty() == true) { + if (backupPath.isContentScheme()) { + val uri = Uri.parse(backupPath) + val doc = DocumentFile.fromTreeUri(requireContext(), uri) + if (doc?.canWrite() == true) { + lifecycleScope.launch { Restore.restore(requireContext(), backupPath) - } else { - restoreDir.launch(null) } } else { - restoreUsePermission(backupPath) + restoreDir.launch(null) } } else { - restoreDir.launch(null) + restoreUsePermission(backupPath) } + } else { + restoreDir.launch(null) } }