From eada533161f1f06ded237987120fd034a8fb917f Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 21 Mar 2022 10:52:58 +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/src/main/java/io/legado/app/App.kt | 2 ++ .../io/legado/app/help/storage/AppWebDav.kt | 20 +++++++++---------- .../legado/app/help/storage/BackupConfig.kt | 6 +++++- .../app/ui/book/read/ReadBookActivity.kt | 13 +++++------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/io/legado/app/App.kt b/app/src/main/java/io/legado/app/App.kt index 7a5592006..bc819a113 100644 --- a/app/src/main/java/io/legado/app/App.kt +++ b/app/src/main/java/io/legado/app/App.kt @@ -43,6 +43,8 @@ class App : MultiDexApplication() { registerActivityLifecycleCallbacks(LifecycleHelp) defaultSharedPreferences.registerOnSharedPreferenceChangeListener(AppConfig) Coroutine.async { + //WebDav初始化 + AppWebDav.initConfig() //初始化封面 BookCover.toString() //清除过期数据 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 1a7dd0768..62639f539 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 @@ -28,6 +28,8 @@ object AppWebDav { private val bookProgressUrl = "${rootWebDavUrl}bookProgress/" private val zipFilePath = "${appCtx.externalFiles.absolutePath}${File.separator}backup.zip" + var isOk = false + private val rootWebDavUrl: String get() { val configUrl = appCtx.getPrefString(PreferKey.webDavUrl)?.trim() @@ -49,23 +51,21 @@ object AppWebDav { } @Throws(Exception::class) - suspend fun initWebDav(): Boolean { + suspend fun initConfig() { + isOk = false val account = appCtx.getPrefString(PreferKey.webDavAccount) val password = appCtx.getPrefString(PreferKey.webDavPassword) if (!account.isNullOrBlank() && !password.isNullOrBlank()) { HttpAuth.auth = HttpAuth.Auth(account, password) - WebDav(rootWebDavUrl).makeAsDir() - WebDav(bookProgressUrl).makeAsDir() - return true + isOk = WebDav(rootWebDavUrl).makeAsDir() && WebDav(bookProgressUrl).makeAsDir() } - return false } @Throws(Exception::class) private suspend fun getWebDavFileNames(): ArrayList { val url = rootWebDavUrl val names = arrayListOf() - if (initWebDav()) { + if (isOk) { var files = WebDav(url).listFiles() files = files.reversed() files.forEach { @@ -120,7 +120,7 @@ object AppWebDav { suspend fun backUpWebDav(path: String) { try { - if (initWebDav() && NetworkUtils.isAvailable()) { + if (isOk && NetworkUtils.isAvailable()) { val paths = arrayListOf(*Backup.backupFileNames) for (i in 0 until paths.size) { paths[i] = path + File.separator + paths[i] @@ -138,7 +138,7 @@ object AppWebDav { suspend fun exportWebDav(byteArray: ByteArray, fileName: String) { try { - if (initWebDav() && NetworkUtils.isAvailable()) { + if (isOk && NetworkUtils.isAvailable()) { // 默认导出到legado文件夹下exports目录 val exportsWebDavUrl = rootWebDavUrl + EncoderUtils.escape("exports") + "/" // 在legado文件夹创建exports目录,如果不存在的话 @@ -161,7 +161,7 @@ object AppWebDav { val bookProgress = BookProgress(book) val json = GSON.toJson(bookProgress) val url = getProgressUrl(book) - if (initWebDav()) { + if (isOk) { WebDav(url).upload(json.toByteArray(), "application/json") } } @@ -175,7 +175,7 @@ object AppWebDav { * 获取书籍进度 */ suspend fun getBookProgress(book: Book): BookProgress? { - if (initWebDav() && NetworkUtils.isAvailable()) { + if (isOk && NetworkUtils.isAvailable()) { val url = getProgressUrl(book) WebDav(url).download()?.let { byteArray -> val json = String(byteArray) diff --git a/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt b/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt index 201182242..aa9a2a8ff 100644 --- a/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt +++ b/app/src/main/java/io/legado/app/help/storage/BackupConfig.kt @@ -22,7 +22,11 @@ object BackupConfig { PreferKey.themeMode, PreferKey.bookshelfLayout, PreferKey.showRss, - PreferKey.threadCount + PreferKey.threadCount, + PreferKey.webDavUrl, + PreferKey.webDavDir, + PreferKey.webDavAccount, + PreferKey.webDavPassword ) //配置忽略标题 diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 800f14687..206ed2163 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -60,8 +60,11 @@ import io.legado.app.ui.replace.ReplaceRuleActivity import io.legado.app.ui.replace.edit.ReplaceEditActivity import io.legado.app.ui.widget.dialog.TextDialog import io.legado.app.utils.* -import kotlinx.coroutines.* import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.Job +import kotlinx.coroutines.delay +import kotlinx.coroutines.isActive +import kotlinx.coroutines.launch class ReadBookActivity : BaseReadBookActivity(), View.OnTouchListener, @@ -245,13 +248,7 @@ class ReadBookActivity : BaseReadBookActivity(), } } } - launch { - menu.findItem(R.id.menu_get_progress)?.isVisible = - withContext(IO) { - runCatching { AppWebDav.initWebDav() } - .getOrElse { false } - } - } + menu.findItem(R.id.menu_get_progress)?.isVisible = AppWebDav.isOk } }