feat: 优化代码

pull/133/head
kunfei 5 years ago
parent df14e99bf3
commit 3b653cebbf
  1. 40
      app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt
  2. 1
      app/src/main/java/io/legado/app/lib/webdav/WebDav.kt

@ -2,39 +2,45 @@ package io.legado.app.help.storage
import io.legado.app.App import io.legado.app.App
import io.legado.app.data.entities.BookProgress 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.lib.webdav.WebDav
import io.legado.app.utils.FileUtils import io.legado.app.utils.FileUtils
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray import io.legado.app.utils.fromJsonArray
@Suppress("BlockingMethodInNonBlockingContext")
object SyncBookProgress { object SyncBookProgress {
private val file = FileUtils.createFileIfNotExist(App.INSTANCE.cacheDir, "bookProgress.json") private val file = FileUtils.createFileIfNotExist(App.INSTANCE.cacheDir, "bookProgress.json")
private val webDavUrl = WebDavHelp.getWebDavUrl() + "legado/bookProgress.json" private val webDavUrl = WebDavHelp.getWebDavUrl() + "legado/bookProgress.json"
fun uploadBookProgress() { fun uploadBookProgress() {
val value = App.db.bookDao().allBookProgress Coroutine.async {
if (value.isNotEmpty()) { val value = App.db.bookDao().allBookProgress
val json = GSON.toJson(value) if (value.isNotEmpty()) {
file.writeText(json) val json = GSON.toJson(value)
if (WebDavHelp.initWebDav()) { file.writeText(json)
WebDav(WebDavHelp.getWebDavUrl() + "legado").makeAsDir() if (WebDavHelp.initWebDav()) {
WebDav(webDavUrl).upload(file.absolutePath) WebDav(WebDavHelp.getWebDavUrl() + "legado").makeAsDir()
WebDav(webDavUrl).upload(file.absolutePath)
}
} }
} }
} }
fun downloadBookProgress() { fun downloadBookProgress() {
WebDav(webDavUrl).downloadTo(file.absolutePath, true) Coroutine.async {
if (file.exists()) { WebDav(webDavUrl).downloadTo(file.absolutePath, true)
val json = file.readText() if (file.exists()) {
GSON.fromJsonArray<BookProgress>(json)?.forEach { val json = file.readText()
App.db.bookDao().upBookProgress( GSON.fromJsonArray<BookProgress>(json)?.forEach {
it.bookUrl, App.db.bookDao().upBookProgress(
it.durChapterIndex, it.bookUrl,
it.durChapterPos, it.durChapterIndex,
it.durChapterTime it.durChapterPos,
) it.durChapterTime
)
}
} }
} }
} }

@ -202,7 +202,6 @@ constructor(urlStr: String) {
* 上传文件 * 上传文件
*/ */
@Throws(IOException::class) @Throws(IOException::class)
@JvmOverloads
fun upload(localPath: String, contentType: String? = null): Boolean { fun upload(localPath: String, contentType: String? = null): Boolean {
val file = File(localPath) val file = File(localPath)
if (!file.exists()) return false if (!file.exists()) return false

Loading…
Cancel
Save