From 83461f68ba8fb05cc473c2607505705c9b80ac86 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 15 May 2022 19:25:47 +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 --- app/src/main/assets/updateLog.md | 1 + .../legado/app/model/localBook/LocalBook.kt | 40 ++++++++------ .../app/ui/book/read/ReadBookViewModel.kt | 52 ++++++++----------- 3 files changed, 48 insertions(+), 45 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 1097c285b..9ac4967e3 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -19,6 +19,7 @@ * 书源支持文件类型 by Xwite * 启用firebase收集崩溃日志 * web端阅读实现无限滚动 by Xwite +* 进入阅读界面时如果本地书籍有更新自动刷新目录 **2022/05/11** diff --git a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt index 0cac6f128..a59b2db5d 100644 --- a/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt +++ b/app/src/main/java/io/legado/app/model/localBook/LocalBook.kt @@ -7,21 +7,17 @@ import com.script.SimpleBindings import io.legado.app.R import io.legado.app.constant.AppConst 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.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.TocEmptyException import io.legado.app.help.BookHelp import io.legado.app.help.config.AppConfig import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.* import splitties.init.appCtx -import java.io.File -import java.io.FileInputStream -import java.io.FileOutputStream -import java.io.FileNotFoundException -import java.io.InputStream +import java.io.* import java.util.regex.Pattern /** @@ -50,6 +46,20 @@ object LocalBook { throw FileNotFoundException("${uri.path} 文件不存在") } + fun getLastModified(book: Book): Result { + 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) fun getChapterList(book: Book): ArrayList { val chapters = when { @@ -228,7 +238,7 @@ object LocalBook { val lastPath = urlNoOption.substringAfterLast("/") val fileType = lastPath.substringAfterLast(".") val type = analyzeUrl.type - return type ?: fileType ?: "unknown" + return type ?: fileType } private fun saveBookFile( @@ -262,13 +272,13 @@ object LocalBook { //文件类书源 合并在线书籍信息 在线 > 本地 fun mergeBook(localBook: Book, onLineBook: Book?): Book { onLineBook ?: return localBook - val mergeBook = localBook - mergeBook.name = if (onLineBook.name.isBlank()) localBook.name else onLineBook.name - mergeBook.author = if (onLineBook.author.isBlank()) localBook.author else onLineBook.author - mergeBook.coverUrl = onLineBook.coverUrl - mergeBook.intro = if (onLineBook.intro.isNullOrBlank()) localBook.intro else onLineBook.intro - mergeBook.save() - return mergeBook + localBook.name = onLineBook.name.ifBlank { localBook.name } + localBook.author = onLineBook.author.ifBlank { localBook.author } + localBook.coverUrl = onLineBook.coverUrl + localBook.intro = + if (onLineBook.intro.isNullOrBlank()) localBook.intro else onLineBook.intro + localBook.save() + return localBook } } 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 1dde3bd62..28dccf780 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 @@ -61,41 +61,33 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } private fun initBook(book: Book) { - if (ReadBook.book?.bookUrl != book.bookUrl) { - ReadBook.resetData(book) - isInitFinish = true - if (ReadBook.chapterSize == 0) { - if (book.tocUrl.isEmpty()) { - loadBookInfo(book) - } else { - loadChapterList(book) - } + val isSameBook = ReadBook.book?.bookUrl == book.bookUrl + if (isSameBook) ReadBook.upData(book) else ReadBook.resetData(book) + isInitFinish = true + if (ReadBook.chapterSize == 0) { + if (book.tocUrl.isEmpty()) { + loadBookInfo(book) } else { - if (ReadBook.durChapterIndex > ReadBook.chapterSize - 1) { - ReadBook.durChapterIndex = ReadBook.chapterSize - 1 - } - ReadBook.loadContent(resetPageOffset = true) + loadChapterList(book) } - syncBookProgress(book) - } else { - ReadBook.upData(book) - isInitFinish = true - if (ReadBook.chapterSize == 0) { - if (book.tocUrl.isEmpty()) { - loadBookInfo(book) - } else { - loadChapterList(book) - } + } else if (book.isLocalBook() + && LocalBook.getLastModified(book).getOrDefault(0L) > book.latestChapterTime + ) { + loadChapterList(book) + } else if (isSameBook) { + if (ReadBook.curTextChapter != null) { + ReadBook.callBack?.upContent(resetPageOffset = false) } else { - if (ReadBook.curTextChapter != null) { - ReadBook.callBack?.upContent(resetPageOffset = false) - } else { - ReadBook.loadContent(resetPageOffset = true) - } + ReadBook.loadContent(resetPageOffset = true) } - if (!BaseReadAloudService.isRun) { - syncBookProgress(book) + } else { + 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) { autoChangeSource(book.name, book.author)