From ce88a88c1bc7e3951e6b60f4ca4db1ad3c3045a1 Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 10 Dec 2020 20:14:06 +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 --- .../java/io/legado/app/help/storage/Backup.kt | 2 +- .../storage/{WebDavHelp.kt => BookWebDav.kt} | 41 ++++++++++++++- .../app/help/storage/SyncBookProgress.kt | 50 ------------------- .../io/legado/app/service/help/ReadBook.kt | 12 +++-- .../app/ui/book/cache/CacheViewModel.kt | 6 +-- .../app/ui/book/read/ReadBookActivity.kt | 8 ++- .../app/ui/book/read/ReadBookViewModel.kt | 29 ++++++++--- .../legado/app/ui/config/BackupRestoreUi.kt | 4 +- app/src/main/res/menu/book_read.xml | 5 ++ app/src/main/res/values-zh-rHK/strings.xml | 1 + app/src/main/res/values-zh-rTW/strings.xml | 1 + app/src/main/res/values-zh/strings.xml | 1 + app/src/main/res/values/strings.xml | 1 + 13 files changed, 91 insertions(+), 70 deletions(-) rename app/src/main/java/io/legado/app/help/storage/{WebDavHelp.kt => BookWebDav.kt} (79%) delete mode 100644 app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt diff --git a/app/src/main/java/io/legado/app/help/storage/Backup.kt b/app/src/main/java/io/legado/app/help/storage/Backup.kt index b50f5ea78..24d78276d 100644 --- a/app/src/main/java/io/legado/app/help/storage/Backup.kt +++ b/app/src/main/java/io/legado/app/help/storage/Backup.kt @@ -95,7 +95,7 @@ object Backup { } edit.commit() } - WebDavHelp.backUpWebDav(backupPath) + BookWebDav.backUpWebDav(backupPath) if (path.isContentScheme()) { copyBackup(context, Uri.parse(path), isAuto) } else { diff --git a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt b/app/src/main/java/io/legado/app/help/storage/BookWebDav.kt similarity index 79% rename from app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt rename to app/src/main/java/io/legado/app/help/storage/BookWebDav.kt index b656c1ca2..9d0843864 100644 --- a/app/src/main/java/io/legado/app/help/storage/WebDavHelp.kt +++ b/app/src/main/java/io/legado/app/help/storage/BookWebDav.kt @@ -6,6 +6,8 @@ import android.os.Looper import io.legado.app.App import io.legado.app.R import io.legado.app.constant.PreferKey +import io.legado.app.data.entities.Book +import io.legado.app.data.entities.BookProgress import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.dialogs.selector import io.legado.app.lib.webdav.HttpAuth @@ -19,8 +21,9 @@ import java.io.File import java.text.SimpleDateFormat import java.util.* -object WebDavHelp { +object BookWebDav { private const val defaultWebDavUrl = "https://dav.jianguoyun.com/dav/" + private val bookProgressUrl = "${rootWebDavUrl}bookProgress/" private val zipFilePath = "${FileUtils.getCachePath()}${File.separator}backup.zip" val rootWebDavUrl: String @@ -42,6 +45,7 @@ object WebDavHelp { if (!account.isNullOrBlank() && !password.isNullOrBlank()) { HttpAuth.auth = HttpAuth.Auth(account, password) WebDav(rootWebDavUrl).makeAsDir() + WebDav(bookProgressUrl).makeAsDir() return true } return false @@ -139,4 +143,39 @@ object WebDavHelp { } } } + + fun uploadBookProgress(book: Book) { + Coroutine.async { + val bookProgress = BookProgress( + name = book.name, + author = book.author, + durChapterIndex = book.durChapterIndex, + durChapterPos = book.durChapterPos, + durChapterTime = book.durChapterTime, + durChapterTitle = book.durChapterTitle + ) + val json = GSON.toJson(bookProgress) + val url = getUrl(book) + if (initWebDav()) { + WebDav(url).upload(json.toByteArray()) + } + } + } + + fun getBookProgress(book: Book): BookProgress? { + if (initWebDav()) { + val url = getUrl(book) + WebDav(url).download()?.let { byteArray -> + val json = String(byteArray) + GSON.fromJsonObject(json)?.let { + return it + } + } + } + return null + } + + private fun getUrl(book: Book): String { + return bookProgressUrl + book.name + "_" + book.author + ".json" + } } \ No newline at end of file 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 deleted file mode 100644 index b18efde89..000000000 --- a/app/src/main/java/io/legado/app/help/storage/SyncBookProgress.kt +++ /dev/null @@ -1,50 +0,0 @@ -package io.legado.app.help.storage - -import io.legado.app.data.entities.Book -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.GSON -import io.legado.app.utils.fromJsonObject - -@Suppress("BlockingMethodInNonBlockingContext") -object SyncBookProgress { - - private val webDavUrl = "${WebDavHelp.rootWebDavUrl}bookProgress/" - - fun uploadBookProgress(book: Book) { - Coroutine.async { - val bookProgress = BookProgress( - name = book.name, - author = book.author, - durChapterIndex = book.durChapterIndex, - durChapterPos = book.durChapterPos, - durChapterTime = book.durChapterTime, - durChapterTitle = book.durChapterTitle - ) - val json = GSON.toJson(bookProgress) - val url = getUrl(book) - if (WebDavHelp.initWebDav()) { - WebDav(webDavUrl).makeAsDir() - WebDav(url).upload(json.toByteArray()) - } - } - } - - fun getBookProgress(book: Book): BookProgress? { - if (WebDavHelp.initWebDav()) { - val url = getUrl(book) - WebDav(url).download()?.let { byteArray -> - val json = String(byteArray) - GSON.fromJsonObject(json)?.let { - return it - } - } - } - return null - } - - private fun getUrl(book: Book): String { - return webDavUrl + book.name + "_" + book.author + ".json" - } -} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/service/help/ReadBook.kt b/app/src/main/java/io/legado/app/service/help/ReadBook.kt index eda29c52e..5bbfbd7e6 100644 --- a/app/src/main/java/io/legado/app/service/help/ReadBook.kt +++ b/app/src/main/java/io/legado/app/service/help/ReadBook.kt @@ -52,10 +52,8 @@ object ReadBook { durChapterIndex = book.durChapterIndex durChapterPos = book.durChapterPos isLocalBook = book.origin == BookType.local - chapterSize = 0 - prevTextChapter = null - curTextChapter = null - nextTextChapter = null + chapterSize = book.totalChapterNum + clearTextChapter() titleDate.postValue(book.name) callBack?.upPageAnim() upWebBook(book) @@ -80,6 +78,12 @@ object ReadBook { } } + fun clearTextChapter() { + prevTextChapter = null + curTextChapter = null + nextTextChapter = null + } + fun upReadStartTime() { Coroutine.async { readRecord.readTime = readRecord.readTime + System.currentTimeMillis() - readStartTime diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index d2d6fc6fd..d0fd4f34a 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -11,7 +11,7 @@ import io.legado.app.constant.PreferKey import io.legado.app.data.entities.Book import io.legado.app.help.BookHelp import io.legado.app.help.ContentProcessor -import io.legado.app.help.storage.WebDavHelp +import io.legado.app.help.storage.BookWebDav import io.legado.app.utils.* import java.io.File @@ -47,7 +47,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { filename ).writeText(content) // 写出文件到cache目录 // 导出到webdav - WebDavHelp.exportWebDav(FileUtils.getCachePath(), filename) + BookWebDav.exportWebDav(FileUtils.getCachePath(), filename) // 上传完删除cache文件 FileUtils.deleteFile("${FileUtils.getCachePath()}${File.separator}${filename}") } @@ -68,7 +68,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { FileUtils.createFileIfNotExist(file, filename) .writeText(getAllContents(book)) if (App.INSTANCE.getPrefBoolean(PreferKey.webDavCacheBackup, false)) { - WebDavHelp.exportWebDav(file.absolutePath, filename) // 导出到webdav + BookWebDav.exportWebDav(file.absolutePath, filename) // 导出到webdav } getSrcList(book).forEach { val vFile = BookHelp.getImage(book, it.third) 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 bd0f2f4f0..0b6b29a18 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 @@ -23,7 +23,7 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadTipConfig import io.legado.app.help.storage.Backup -import io.legado.app.help.storage.SyncBookProgress +import io.legado.app.help.storage.BookWebDav import io.legado.app.lib.dialogs.alert import io.legado.app.lib.theme.accentColor import io.legado.app.receiver.TimeBatteryReceiver @@ -139,7 +139,7 @@ class ReadBookActivity : ReadBookBaseActivity(), upSystemUiVisibility() if (!BuildConfig.DEBUG) { ReadBook.book?.let { - SyncBookProgress.uploadBookProgress(it) + BookWebDav.uploadBookProgress(it) } Backup.autoBack(this) } @@ -173,6 +173,7 @@ class ReadBookActivity : ReadBookBaseActivity(), else -> when (item.itemId) { R.id.menu_enable_replace -> item.isChecked = book.getUseReplaceRule() R.id.menu_re_segment -> item.isChecked = book.getReSegment() + R.id.menu_get_progress -> BookWebDav.initWebDav() } } } @@ -234,6 +235,9 @@ class ReadBookActivity : ReadBookBaseActivity(), ) } R.id.menu_set_charset -> showCharsetConfig() + R.id.menu_get_progress -> ReadBook.book?.let { + viewModel.syncBookProgress(it) + } R.id.menu_help -> showReadMenuHelp() } return super.onCompatOptionsItemSelected(item) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index f70501f12..478175e3b 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -11,7 +11,7 @@ import io.legado.app.data.entities.SearchBook import io.legado.app.help.AppConfig import io.legado.app.help.BookHelp import io.legado.app.help.IntentDataHelp -import io.legado.app.help.storage.SyncBookProgress +import io.legado.app.help.storage.BookWebDav import io.legado.app.model.localBook.LocalBook import io.legado.app.model.webBook.WebBook import io.legado.app.service.BaseReadAloudService @@ -47,12 +47,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { private fun initBook(book: Book) { if (ReadBook.book?.bookUrl != book.bookUrl) { - SyncBookProgress.getBookProgress(book)?.let { - book.durChapterIndex = it.durChapterIndex - book.durChapterPos = it.durChapterPos - book.durChapterTime = it.durChapterTime - book.durChapterTitle = it.durChapterTitle - } + downloadProgress(book) ReadBook.resetData(book) isInitFinish = true if (!book.isLocalBook() && ReadBook.webBook == null) { @@ -105,6 +100,15 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } + private fun downloadProgress(book: Book) { + BookWebDav.getBookProgress(book)?.let { + book.durChapterIndex = it.durChapterIndex + book.durChapterPos = it.durChapterPos + book.durChapterTime = it.durChapterTime + book.durChapterTitle = it.durChapterTitle + } + } + private fun loadBookInfo( book: Book, changeDruChapterIndex: ((chapters: List) -> Unit)? = null, @@ -162,6 +166,17 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } + fun syncBookProgress(book: Book) { + execute { + downloadProgress(book) + ReadBook.durChapterIndex = book.durChapterIndex + ReadBook.durChapterPos = book.durChapterPos + ReadBook.prevTextChapter = null + ReadBook.clearTextChapter() + ReadBook.loadContent(resetPageOffset = true) + } + } + fun changeTo(newBook: Book) { execute { var oldTocSize: Int = newBook.totalChapterNum diff --git a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt index 87e63f7d7..794271d3e 100644 --- a/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt +++ b/app/src/main/java/io/legado/app/ui/config/BackupRestoreUi.kt @@ -13,9 +13,9 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.permission.Permissions import io.legado.app.help.permission.PermissionsCompat import io.legado.app.help.storage.Backup +import io.legado.app.help.storage.BookWebDav import io.legado.app.help.storage.ImportOldData import io.legado.app.help.storage.Restore -import io.legado.app.help.storage.WebDavHelp import io.legado.app.ui.filepicker.FilePicker import io.legado.app.utils.getPrefString import io.legado.app.utils.isContentScheme @@ -77,7 +77,7 @@ object BackupRestoreUi { fun restore(fragment: Fragment) { Coroutine.async(context = Main) { - WebDavHelp.showRestoreDialog(fragment.requireContext()) + BookWebDav.showRestoreDialog(fragment.requireContext()) }.onError { fragment.longToast("WebDavError:${it.localizedMessage}\n将从本地备份恢复。") val backupPath = fragment.getPrefString(PreferKey.backupPath) diff --git a/app/src/main/res/menu/book_read.xml b/app/src/main/res/menu/book_read.xml index c7d970045..f35b5b901 100644 --- a/app/src/main/res/menu/book_read.xml +++ b/app/src/main/res/menu/book_read.xml @@ -76,6 +76,11 @@ android:title="@string/book_page_anim" app:showAsAction="never" /> + + 页眉页脚 规则订阅 添加大佬们提供的规则导入地址\n添加后点击可导入规则 + 拉取云端进度 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index de80cdd18..0e73c856f 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -789,5 +789,6 @@ 页眉页脚 规则订阅 添加大佬们提供的规则导入地址\n添加后点击可导入规则 + 拉取云端进度 diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 4090b5b3a..807ebd11e 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -792,5 +792,6 @@ 页眉页脚 规则订阅 添加大佬们提供的规则导入地址\n添加后点击可导入规则 + 拉取云端进度 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8a11d1e0e..281a0a367 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -795,5 +795,6 @@ footer header 规则订阅 添加大佬们提供的规则导入地址\n添加后点击可导入规则 + 拉取云端进度