From 3b653cebbf5832ec0588dd7fccbaa41d2498f847 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 3 Mar 2020 22:11:55 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/help/storage/SyncBookProgress.kt | 40 +++++++++++-------- .../java/io/legado/app/lib/webdav/WebDav.kt | 1 - 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt b/app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt index b01a11f0e..ee8bba384 100644 --- a/app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt +++ b/app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt @@ -2,39 +2,45 @@ package io.legado.app.help.storage import io.legado.app.App import io.legado.app.data.entities.BookProgress +import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.webdav.WebDav import io.legado.app.utils.FileUtils import io.legado.app.utils.GSON import io.legado.app.utils.fromJsonArray +@Suppress("BlockingMethodInNonBlockingContext") object SyncBookProgress { private val file = FileUtils.createFileIfNotExist(App.INSTANCE.cacheDir, "bookProgress.json") private val webDavUrl = WebDavHelp.getWebDavUrl() + "legado/bookProgress.json" fun uploadBookProgress() { - val value = App.db.bookDao().allBookProgress - if (value.isNotEmpty()) { - val json = GSON.toJson(value) - file.writeText(json) - if (WebDavHelp.initWebDav()) { - WebDav(WebDavHelp.getWebDavUrl() + "legado").makeAsDir() - WebDav(webDavUrl).upload(file.absolutePath) + Coroutine.async { + val value = App.db.bookDao().allBookProgress + if (value.isNotEmpty()) { + val json = GSON.toJson(value) + file.writeText(json) + if (WebDavHelp.initWebDav()) { + WebDav(WebDavHelp.getWebDavUrl() + "legado").makeAsDir() + WebDav(webDavUrl).upload(file.absolutePath) + } } } } fun downloadBookProgress() { - WebDav(webDavUrl).downloadTo(file.absolutePath, true) - if (file.exists()) { - val json = file.readText() - GSON.fromJsonArray(json)?.forEach { - App.db.bookDao().upBookProgress( - it.bookUrl, - it.durChapterIndex, - it.durChapterPos, - it.durChapterTime - ) + Coroutine.async { + WebDav(webDavUrl).downloadTo(file.absolutePath, true) + if (file.exists()) { + val json = file.readText() + GSON.fromJsonArray(json)?.forEach { + App.db.bookDao().upBookProgress( + it.bookUrl, + it.durChapterIndex, + it.durChapterPos, + it.durChapterTime + ) + } } } } diff --git a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt index 32b79ce79..47aa59344 100644 --- a/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt +++ b/app/src/main/java/io/legado/app/lib/webdav/WebDav.kt @@ -202,7 +202,6 @@ constructor(urlStr: String) { * 上传文件 */ @Throws(IOException::class) - @JvmOverloads fun upload(localPath: String, contentType: String? = null): Boolean { val file = File(localPath) if (!file.exists()) return false