feat: 优化代码

pull/133/head
kunfei 5 years ago
parent 7993725caa
commit e117e54894
  1. 7
      app/src/main/java/io/legado/app/data/dao/BookDao.kt
  2. 7
      app/src/main/java/io/legado/app/data/entities/BookProgress.kt
  3. 41
      app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt
  4. 8
      app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt

@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData
import androidx.room.*
import io.legado.app.constant.BookType
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookProgress
@Dao
interface BookDao {
@ -67,4 +68,10 @@ interface BookDao {
@Query("update books set `group` = :newGroupId where `group` = :oldGroupId")
fun upGroup(oldGroupId: Int, newGroupId: Int)
@get:Query("select bookUrl, durChapterIndex, durChapterPos from books")
val allBookProgress: List<BookProgress>
@Query("update books set durChapterIndex = :durChapterIndex, durChapterPos = :durChapterPos where bookUrl = :bookUrl")
fun upBookProgress(bookUrl: String, durChapterIndex: Int, durChapterPos: Int)
}

@ -0,0 +1,7 @@
package io.legado.app.data.entities
data class BookProgress(
val bookUrl: String,
val durChapterIndex: Int,
var durChapterPos: Int
)

@ -0,0 +1,41 @@
package io.legado.app.help.storage
import io.legado.app.App
import io.legado.app.data.entities.BookProgress
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
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)
}
}
}
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
)
}
}
}
}

@ -25,7 +25,7 @@ object WebDavHelp {
private const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/"
private val zipFilePath = "${FileUtils.getCachePath()}${File.separator}backup.zip"
private fun getWebDavUrl(): String {
fun getWebDavUrl(): String {
var url = App.INSTANCE.getPrefString(PreferKey.webDavUrl)
if (url.isNullOrEmpty()) {
url = defaultWebDavUrl
@ -34,7 +34,7 @@ object WebDavHelp {
return url
}
private fun initWebDav(): Boolean {
fun initWebDav(): Boolean {
val account = App.INSTANCE.getPrefString(PreferKey.webDavAccount)
val password = App.INSTANCE.getPrefString(PreferKey.webDavPassword)
if (!account.isNullOrBlank() && !password.isNullOrBlank()) {
@ -82,8 +82,8 @@ object WebDavHelp {
private fun restoreWebDav(name: String, success: () -> Unit) {
Coroutine.async {
getWebDavUrl().let {
val file = WebDav(it + "legado/" + name)
file.downloadTo(zipFilePath, true)
val webDav = WebDav(it + "legado/" + name)
webDav.downloadTo(zipFilePath, true)
@Suppress("BlockingMethodInNonBlockingContext")
ZipUtils.unzipFile(zipFilePath, Backup.backupPath)
Restore.restore(Backup.backupPath)

Loading…
Cancel
Save