kunfei 4 ani în urmă
părinte
comite
8001dbe792

+ 1 - 0
app/src/main/assets/updateLog.md

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

+ 25 - 15
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<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)
     fun getChapterList(book: Book): ArrayList<BookChapter> {
         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
     }
 
 }

+ 22 - 30
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)