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.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<BookProgress>(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<BookProgress>(json)?.forEach {
App.db.bookDao().upBookProgress(
it.bookUrl,
it.durChapterIndex,
it.durChapterPos,
it.durChapterTime
)
}
}
}
}

@ -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

Loading…
Cancel
Save