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. 32
      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,8 +61,8 @@ 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()) {
@ -70,32 +70,24 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} else { } else {
loadChapterList(book) loadChapterList(book)
} }
} else { } else if (book.isLocalBook()
if (ReadBook.durChapterIndex > ReadBook.chapterSize - 1) { && LocalBook.getLastModified(book).getOrDefault(0L) > book.latestChapterTime
ReadBook.durChapterIndex = ReadBook.chapterSize - 1 ) {
}
ReadBook.loadContent(resetPageOffset = true)
}
syncBookProgress(book)
} else {
ReadBook.upData(book)
isInitFinish = true
if (ReadBook.chapterSize == 0) {
if (book.tocUrl.isEmpty()) {
loadBookInfo(book)
} else {
loadChapterList(book) loadChapterList(book)
} } else if (isSameBook) {
} else {
if (ReadBook.curTextChapter != null) { if (ReadBook.curTextChapter != null) {
ReadBook.callBack?.upContent(resetPageOffset = false) ReadBook.callBack?.upContent(resetPageOffset = false)
} else { } else {
ReadBook.loadContent(resetPageOffset = true) ReadBook.loadContent(resetPageOffset = true)
} }
} else {
if (ReadBook.durChapterIndex > ReadBook.chapterSize - 1) {
ReadBook.durChapterIndex = ReadBook.chapterSize - 1
} }
if (!BaseReadAloudService.isRun) { ReadBook.loadContent(resetPageOffset = isSameBook)
syncBookProgress(book)
} }
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