From c860f26fdf345fc65e8161aa0d802a5ddbe74525 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 4 Oct 2022 20:16:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E6=97=B6=E6=A3=80=E6=B5=8Bwe?= =?UTF-8?q?bDav=E4=B9=A6=E7=B1=8D=E6=98=AF=E5=90=A6=E6=AF=94=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=96=B0,=E8=87=AA=E5=8A=A8=E9=87=8D=E6=96=B0?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/model/remote/RemoteBookManager.kt | 11 +--------- .../app/model/remote/RemoteBookWebDav.kt | 14 ++++++++++--- .../app/ui/book/info/BookInfoActivity.kt | 5 +++-- .../app/ui/book/info/BookInfoViewModel.kt | 21 ++++++++++++++++--- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/remote/RemoteBookManager.kt b/app/src/main/java/io/legado/app/model/remote/RemoteBookManager.kt index 75356cdac..3a82e6006 100644 --- a/app/src/main/java/io/legado/app/model/remote/RemoteBookManager.kt +++ b/app/src/main/java/io/legado/app/model/remote/RemoteBookManager.kt @@ -2,19 +2,10 @@ package io.legado.app.model.remote import android.net.Uri import io.legado.app.data.entities.Book -import kotlinx.coroutines.runBlocking abstract class RemoteBookManager { protected val remoteBookFolder: String = "books" - init { - runBlocking { - initRemoteContext() - } - } - - abstract suspend fun initRemoteContext() - /** * 获取书籍列表 */ @@ -25,7 +16,7 @@ abstract class RemoteBookManager { * 根据书籍地址获取书籍信息 */ @Throws(Exception::class) - abstract suspend fun getRemoteBook(path: String): RemoteBook + abstract suspend fun getRemoteBook(path: String): RemoteBook? /** * @return Uri:下载到本地的路径 diff --git a/app/src/main/java/io/legado/app/model/remote/RemoteBookWebDav.kt b/app/src/main/java/io/legado/app/model/remote/RemoteBookWebDav.kt index 7dc4b35b9..35bc537ef 100644 --- a/app/src/main/java/io/legado/app/model/remote/RemoteBookWebDav.kt +++ b/app/src/main/java/io/legado/app/model/remote/RemoteBookWebDav.kt @@ -12,13 +12,21 @@ import io.legado.app.model.localBook.LocalBook import io.legado.app.utils.NetworkUtils import io.legado.app.utils.isContentScheme import io.legado.app.utils.readBytes +import kotlinx.coroutines.runBlocking import splitties.init.appCtx import java.io.File object RemoteBookWebDav : RemoteBookManager() { + val rootBookUrl get() = "${AppWebDav.rootWebDavUrl}${remoteBookFolder}" - override suspend fun initRemoteContext() { + init { + runBlocking { + initRemoteContext() + } + } + + suspend fun initRemoteContext() { AppWebDav.authorization?.let { WebDav(rootBookUrl, it).makeAsDir() } @@ -41,10 +49,10 @@ object RemoteBookWebDav : RemoteBookManager() { return remoteBooks } - override suspend fun getRemoteBook(path: String): RemoteBook { + override suspend fun getRemoteBook(path: String): RemoteBook? { AppWebDav.authorization?.let { val webDavFile = WebDav(path, it).getWebDavFile() - ?: throw NoStackTraceException("远程书籍不存在") + ?: return null return RemoteBook(webDavFile) } ?: throw NoStackTraceException("webDav没有配置") } diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt index eea307fce..406a5a9dd 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt @@ -332,9 +332,10 @@ class BookInfoActivity : } } tvOrigin.setOnClickListener { - viewModel.bookData.value?.let { + viewModel.bookData.value?.let { book -> + if (book.isLocal) return@let startActivity { - putExtra("sourceUrl", it.origin) + putExtra("sourceUrl", book.origin) } } ?: toastOnUi("Book is null") } diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index 612089ae3..fc9b94a83 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -15,12 +15,14 @@ import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource import io.legado.app.exception.NoStackTraceException import io.legado.app.help.book.BookHelp +import io.legado.app.help.book.getRemoteUrl import io.legado.app.help.book.isLocal import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.BookCover import io.legado.app.model.ReadBook import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.localBook.LocalBook +import io.legado.app.model.remote.RemoteBookWebDav import io.legado.app.model.webBook.WebBook import io.legado.app.utils.postEvent import io.legado.app.utils.toastOnUi @@ -108,10 +110,23 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } fun refreshBook(book: Book) { - if (book.isLocal) { - book.tocUrl = "" + execute { + if (book.isLocal) { + book.tocUrl = "" + book.getRemoteUrl()?.let { + val remoteBook = RemoteBookWebDav.getRemoteBook(it) + if (remoteBook == null) { + + } else if (remoteBook.lastModify > book.latestChapterTime) { + + } + } + } + }.onError { + AppLog.put("下载远程书籍<${book.name}>失败", it) + }.onFinally { + loadBookInfo(book, false) } - loadBookInfo(book, false) } fun loadBookInfo(