From 64e55854f72eb7c51642655957cf0f258e0d212f Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 29 Aug 2020 17:52:20 +0800 Subject: [PATCH] =?UTF-8?q?WebDav=E6=81=A2=E5=A4=8D=E5=87=BA=E9=94=99?= =?UTF-8?q?=E6=97=B6=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/legado/app/help/storage/WebDavHelp.kt | 22 ++++++-------- .../legado/app/ui/config/BackupRestoreUi.kt | 29 ++++++++++--------- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt index 190ec2176..e728c28de 100644 --- a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt +++ b/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt @@ -51,28 +51,25 @@ object WebDavHelp { return false } + @Throws(Exception::class) private fun getWebDavFileNames(): ArrayList { val url = rootWebDavUrl val names = arrayListOf() if (initWebDav()) { - try { - var files = WebDav(url).listFiles() - files = files.reversed() - for (index: Int in 0 until min(10, files.size)) { - files[index].displayName?.let { - names.add(it) - } + var files = WebDav(url).listFiles() + files = files.reversed() + for (index: Int in 0 until min(10, files.size)) { + files[index].displayName?.let { + names.add(it) } - } catch (e: Exception) { - return names } } return names } - suspend fun showRestoreDialog(context: Context): Boolean { + suspend fun showRestoreDialog(context: Context) { val names = withContext(IO) { getWebDavFileNames() } - return if (names.isNotEmpty()) { + if (names.isNotEmpty()) { withContext(Main) { context.selector( title = context.getString(R.string.select_restore_file), @@ -83,9 +80,8 @@ object WebDavHelp { } } } - true } else { - false + throw Exception("Web dav no back up file") } } diff --git a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt index 391a76ddf..39939e8cf 100644 --- a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt +++ b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt @@ -19,6 +19,7 @@ import io.legado.app.help.storage.WebDavHelp import io.legado.app.ui.filechooser.FilePicker import io.legado.app.utils.getPrefString import io.legado.app.utils.isContentPath +import io.legado.app.utils.longToast import io.legado.app.utils.toast import kotlinx.coroutines.Dispatchers.Main import org.jetbrains.anko.toast @@ -77,24 +78,24 @@ object BackupRestoreUi { fun restore(fragment: Fragment) { Coroutine.async(context = Main) { - val restoreFromWebDav = WebDavHelp.showRestoreDialog(fragment.requireContext()) - if (!restoreFromWebDav) { - val backupPath = fragment.getPrefString(PreferKey.backupPath) - if (backupPath?.isNotEmpty() == true) { - if (backupPath.isContentPath()) { - val uri = Uri.parse(backupPath) - val doc = DocumentFile.fromTreeUri(fragment.requireContext(), uri) - if (doc?.canWrite() == true) { - Restore.restore(fragment.requireContext(), backupPath) - } else { - selectBackupFolder(fragment, restoreSelectRequestCode) - } + WebDavHelp.showRestoreDialog(fragment.requireContext()) + }.onError { + fragment.longToast("WebDavError:${it.localizedMessage},将从本地备份恢复。") + val backupPath = fragment.getPrefString(PreferKey.backupPath) + if (backupPath?.isNotEmpty() == true) { + if (backupPath.isContentPath()) { + val uri = Uri.parse(backupPath) + val doc = DocumentFile.fromTreeUri(fragment.requireContext(), uri) + if (doc?.canWrite() == true) { + Restore.restore(fragment.requireContext(), backupPath) } else { - restoreUsePermission(fragment, backupPath) + selectBackupFolder(fragment, restoreSelectRequestCode) } } else { - selectBackupFolder(fragment, restoreSelectRequestCode) + restoreUsePermission(fragment, backupPath) } + } else { + selectBackupFolder(fragment, restoreSelectRequestCode) } } }