pull/1890/head
kunfei 3 years ago
parent e26c2dcbbf
commit 83461f68ba
  1. 1
      app/src/main/assets/updateLog.md
  2. 40
      app/src/main/java/io/legado/app/model/localBook/LocalBook.kt
  3. 52
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@ -19,6 +19,7 @@
* 书源支持文件类型 by Xwite * 书源支持文件类型 by Xwite
* 启用firebase收集崩溃日志 * 启用firebase收集崩溃日志
* web端阅读实现无限滚动 by Xwite * web端阅读实现无限滚动 by Xwite
* 进入阅读界面时如果本地书籍有更新自动刷新目录
**2022/05/11** **2022/05/11**

@ -7,21 +7,17 @@ import com.script.SimpleBindings
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.data.appDb import io.legado.app.data.appDb
import io.legado.app.data.entities.BaseSource
import io.legado.app.data.entities.Book import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookChapter
import io.legado.app.data.entities.BaseSource
import io.legado.app.exception.TocEmptyException
import io.legado.app.exception.NoStackTraceException import io.legado.app.exception.NoStackTraceException
import io.legado.app.exception.TocEmptyException
import io.legado.app.help.BookHelp import io.legado.app.help.BookHelp
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.* import io.legado.app.utils.*
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.*
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.FileNotFoundException
import java.io.InputStream
import java.util.regex.Pattern import java.util.regex.Pattern
/** /**
@ -50,6 +46,20 @@ object LocalBook {
throw FileNotFoundException("${uri.path} 文件不存在") throw FileNotFoundException("${uri.path} 文件不存在")
} }
fun getLastModified(book: Book): Result<Long> {
return kotlin.runCatching {
val uri = Uri.parse(book.bookUrl)
if (uri.isContentScheme()) {
return@runCatching DocumentFile.fromSingleUri(appCtx, uri)!!.lastModified()
}
val file = File(uri.path!!)
if (file.exists()) {
return@runCatching File(uri.path!!).lastModified()
}
throw FileNotFoundException("${uri.path} 文件不存在")
}
}
@Throws(Exception::class) @Throws(Exception::class)
fun getChapterList(book: Book): ArrayList<BookChapter> { fun getChapterList(book: Book): ArrayList<BookChapter> {
val chapters = when { val chapters = when {
@ -228,7 +238,7 @@ object LocalBook {
val lastPath = urlNoOption.substringAfterLast("/") val lastPath = urlNoOption.substringAfterLast("/")
val fileType = lastPath.substringAfterLast(".") val fileType = lastPath.substringAfterLast(".")
val type = analyzeUrl.type val type = analyzeUrl.type
return type ?: fileType ?: "unknown" return type ?: fileType
} }
private fun saveBookFile( private fun saveBookFile(
@ -262,13 +272,13 @@ object LocalBook {
//文件类书源 合并在线书籍信息 在线 > 本地 //文件类书源 合并在线书籍信息 在线 > 本地
fun mergeBook(localBook: Book, onLineBook: Book?): Book { fun mergeBook(localBook: Book, onLineBook: Book?): Book {
onLineBook ?: return localBook onLineBook ?: return localBook
val mergeBook = localBook localBook.name = onLineBook.name.ifBlank { localBook.name }
mergeBook.name = if (onLineBook.name.isBlank()) localBook.name else onLineBook.name localBook.author = onLineBook.author.ifBlank { localBook.author }
mergeBook.author = if (onLineBook.author.isBlank()) localBook.author else onLineBook.author localBook.coverUrl = onLineBook.coverUrl
mergeBook.coverUrl = onLineBook.coverUrl localBook.intro =
mergeBook.intro = if (onLineBook.intro.isNullOrBlank()) localBook.intro else onLineBook.intro if (onLineBook.intro.isNullOrBlank()) localBook.intro else onLineBook.intro
mergeBook.save() localBook.save()
return mergeBook return localBook
} }
} }

@ -61,41 +61,33 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
private fun initBook(book: Book) { private fun initBook(book: Book) {
if (ReadBook.book?.bookUrl != book.bookUrl) { val isSameBook = ReadBook.book?.bookUrl == book.bookUrl
ReadBook.resetData(book) if (isSameBook) ReadBook.upData(book) else ReadBook.resetData(book)
isInitFinish = true isInitFinish = true
if (ReadBook.chapterSize == 0) { if (ReadBook.chapterSize == 0) {
if (book.tocUrl.isEmpty()) { if (book.tocUrl.isEmpty()) {
loadBookInfo(book) loadBookInfo(book)
} else {
loadChapterList(book)
}
} else { } else {
if (ReadBook.durChapterIndex > ReadBook.chapterSize - 1) { loadChapterList(book)
ReadBook.durChapterIndex = ReadBook.chapterSize - 1
}
ReadBook.loadContent(resetPageOffset = true)
} }
syncBookProgress(book) } else if (book.isLocalBook()
} else { && LocalBook.getLastModified(book).getOrDefault(0L) > book.latestChapterTime
ReadBook.upData(book) ) {
isInitFinish = true loadChapterList(book)
if (ReadBook.chapterSize == 0) { } else if (isSameBook) {
if (book.tocUrl.isEmpty()) { if (ReadBook.curTextChapter != null) {
loadBookInfo(book) ReadBook.callBack?.upContent(resetPageOffset = false)
} else {
loadChapterList(book)
}
} else { } else {
if (ReadBook.curTextChapter != null) { ReadBook.loadContent(resetPageOffset = true)
ReadBook.callBack?.upContent(resetPageOffset = false)
} else {
ReadBook.loadContent(resetPageOffset = true)
}
} }
if (!BaseReadAloudService.isRun) { } else {
syncBookProgress(book) if (ReadBook.durChapterIndex > ReadBook.chapterSize - 1) {
ReadBook.durChapterIndex = ReadBook.chapterSize - 1
} }
ReadBook.loadContent(resetPageOffset = isSameBook)
}
if (!isSameBook || !BaseReadAloudService.isRun) {
syncBookProgress(book)
} }
if (!book.isLocalBook() && ReadBook.bookSource == null) { if (!book.isLocalBook() && ReadBook.bookSource == null) {
autoChangeSource(book.name, book.author) autoChangeSource(book.name, book.author)

Loading…
Cancel
Save