From 8aaadee0ff36a955fd3e3624a024ed3d53cd61d4 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 1 May 2022 22:22:38 +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 --- .../io/legado/app/help/storage/AppWebDav.kt | 25 ++++++++++--------- .../io/legado/app/ui/main/MainActivity.kt | 20 +++++++++++++++ .../io/legado/app/ui/main/MainViewModel.kt | 7 ++++++ 3 files changed, 40 insertions(+), 12 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt b/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt index 74c8accf0..0b084639e 100644 --- a/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt +++ b/app/src/main/java/io/legado/app/help/storage/AppWebDav.kt @@ -119,7 +119,7 @@ object AppWebDav { } @Throws(WebDavException::class) - private suspend fun restoreWebDav(name: String) { + suspend fun restoreWebDav(name: String) { authorization?.let { val webDav = WebDav(rootWebDavUrl + name, it) webDav.downloadTo(zipFilePath, true) @@ -138,21 +138,22 @@ object AppWebDav { return false } - suspend fun lastBackUp(): WebDavFile? { - authorization?.let { - var lastBackupFile: WebDavFile? = null - WebDav(rootWebDavUrl, it).listFiles().forEach { webDavFile -> - if (webDavFile.displayName.startsWith("backup")) { - if (lastBackupFile == null - || webDavFile.lastModify > lastBackupFile!!.lastModify - ) { - lastBackupFile = webDavFile + suspend fun lastBackUp(): Result { + return kotlin.runCatching { + authorization?.let { + var lastBackupFile: WebDavFile? = null + WebDav(rootWebDavUrl, it).listFiles().forEach { webDavFile -> + if (webDavFile.displayName.startsWith("backup")) { + if (lastBackupFile == null + || webDavFile.lastModify > lastBackupFile!!.lastModify + ) { + lastBackupFile = webDavFile + } } } + lastBackupFile } - return lastBackupFile } - return null } @Throws(Exception::class) diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index 7e5f3d823..8db950962 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.main import android.os.Bundle +import android.text.format.DateUtils import android.view.KeyEvent import android.view.MenuItem import android.view.ViewGroup @@ -23,7 +24,9 @@ import io.legado.app.help.BookHelp import io.legado.app.help.config.AppConfig import io.legado.app.help.config.LocalConfig import io.legado.app.help.coroutine.Coroutine +import io.legado.app.help.storage.AppWebDav import io.legado.app.help.storage.Backup +import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.elevation import io.legado.app.lib.theme.primaryColor import io.legado.app.service.BaseReadAloudService @@ -39,6 +42,9 @@ import io.legado.app.utils.setEdgeEffectColor import io.legado.app.utils.showDialogFragment import io.legado.app.utils.toastOnUi import io.legado.app.utils.viewbindingdelegate.viewBinding +import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext /** * 主界面 @@ -88,6 +94,20 @@ class MainActivity : VMBaseActivity(), binding.viewPagerMain.postDelayed({ viewModel.postLoad() }, 3000) + launch { + val lastBackupFile = withContext(IO) { AppWebDav.lastBackUp().getOrNull() } + if (System.currentTimeMillis() - LocalConfig.lastBackup > DateUtils.MINUTE_IN_MILLIS + && lastBackupFile != null + ) { + + alert("恢复", "webDav书源比本地新,是否恢复") { + cancelButton() + okButton { + viewModel.restoreWebDav(lastBackupFile.displayName) + } + } + } + } } override fun onNavigationItemSelected(item: MenuItem): Boolean = binding.run { diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index ff69c934f..7998a9f9e 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -13,6 +13,7 @@ import io.legado.app.data.entities.BookSource import io.legado.app.help.DefaultData import io.legado.app.help.config.AppConfig import io.legado.app.help.config.LocalConfig +import io.legado.app.help.storage.AppWebDav import io.legado.app.model.CacheBook import io.legado.app.model.webBook.WebBook import io.legado.app.service.CacheBookService @@ -195,6 +196,12 @@ class MainViewModel(application: Application) : BaseViewModel(application) { } } + fun restoreWebDav(name: String) { + execute { + AppWebDav.restoreWebDav(name) + } + } + fun upVersion() { execute { if (LocalConfig.needUpHttpTTS) {