更新时检测webDav书籍是否比本地新,自动重新下载

pull/2380/head
kunfei 2 years ago
parent 61dd7ae3d7
commit c860f26fdf
  1. 11
      app/src/main/java/io/legado/app/model/remote/RemoteBookManager.kt
  2. 14
      app/src/main/java/io/legado/app/model/remote/RemoteBookWebDav.kt
  3. 5
      app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt
  4. 21
      app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt

@ -2,19 +2,10 @@ package io.legado.app.model.remote
import android.net.Uri import android.net.Uri
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import kotlinx.coroutines.runBlocking
abstract class RemoteBookManager { abstract class RemoteBookManager {
protected val remoteBookFolder: String = "books" protected val remoteBookFolder: String = "books"
init {
runBlocking {
initRemoteContext()
}
}
abstract suspend fun initRemoteContext()
/** /**
* 获取书籍列表 * 获取书籍列表
*/ */
@ -25,7 +16,7 @@ abstract class RemoteBookManager {
* 根据书籍地址获取书籍信息 * 根据书籍地址获取书籍信息
*/ */
@Throws(Exception::class) @Throws(Exception::class)
abstract suspend fun getRemoteBook(path: String): RemoteBook abstract suspend fun getRemoteBook(path: String): RemoteBook?
/** /**
* @return Uri下载到本地的路径 * @return Uri下载到本地的路径

@ -12,13 +12,21 @@ import io.legado.app.model.localBook.LocalBook
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.isContentScheme import io.legado.app.utils.isContentScheme
import io.legado.app.utils.readBytes import io.legado.app.utils.readBytes
import kotlinx.coroutines.runBlocking
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
object RemoteBookWebDav : RemoteBookManager() { object RemoteBookWebDav : RemoteBookManager() {
val rootBookUrl get() = "${AppWebDav.rootWebDavUrl}${remoteBookFolder}" val rootBookUrl get() = "${AppWebDav.rootWebDavUrl}${remoteBookFolder}"
override suspend fun initRemoteContext() { init {
runBlocking {
initRemoteContext()
}
}
suspend fun initRemoteContext() {
AppWebDav.authorization?.let { AppWebDav.authorization?.let {
WebDav(rootBookUrl, it).makeAsDir() WebDav(rootBookUrl, it).makeAsDir()
} }
@ -41,10 +49,10 @@ object RemoteBookWebDav : RemoteBookManager() {
return remoteBooks return remoteBooks
} }
override suspend fun getRemoteBook(path: String): RemoteBook { override suspend fun getRemoteBook(path: String): RemoteBook? {
AppWebDav.authorization?.let { AppWebDav.authorization?.let {
val webDavFile = WebDav(path, it).getWebDavFile() val webDavFile = WebDav(path, it).getWebDavFile()
?: throw NoStackTraceException("远程书籍不存在") ?: return null
return RemoteBook(webDavFile) return RemoteBook(webDavFile)
} ?: throw NoStackTraceException("webDav没有配置") } ?: throw NoStackTraceException("webDav没有配置")
} }

@ -332,9 +332,10 @@ class BookInfoActivity :
} }
} }
tvOrigin.setOnClickListener { tvOrigin.setOnClickListener {
viewModel.bookData.value?.let { viewModel.bookData.value?.let { book ->
if (book.isLocal) return@let
startActivity<BookSourceEditActivity> { startActivity<BookSourceEditActivity> {
putExtra("sourceUrl", it.origin) putExtra("sourceUrl", book.origin)
} }
} ?: toastOnUi("Book is null") } ?: toastOnUi("Book is null")
} }

@ -15,12 +15,14 @@ import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.book.BookHelp 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.book.isLocal
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.BookCover import io.legado.app.model.BookCover
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.model.localBook.LocalBook 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.model.webBook.WebBook
import io.legado.app.utils.postEvent import io.legado.app.utils.postEvent
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
@ -108,10 +110,23 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
} }
fun refreshBook(book: Book) { fun refreshBook(book: Book) {
if (book.isLocal) { execute {
book.tocUrl = "" 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( fun loadBookInfo(

Loading…
Cancel
Save