فهرست منبع

Merge pull request #1592 from Xwite/master

txt均分目录添加开关
kunfei 4 سال پیش
والد
کامیت
3f481b2235

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

@@ -15,7 +15,7 @@
 
 * 校验失效分组具体到搜索发现目录正文
 * txt文件初次解析目录不选择禁用的正则
-* txt单章字数超102400均分txt
+* txt单章字数超102400均分txt,添加开关
 * 修复tts被回收后无法继续朗读的bug,重新初始化tts
 
 **2022/02/03**

+ 9 - 0
app/src/main/java/io/legado/app/data/entities/Book.kt

@@ -160,6 +160,14 @@ data class Book(
         config().reSegment = reSegment
     }
 
+    fun getLimitContentLength(): Boolean {
+        return config().limitContentLength
+    }
+
+    fun setLimitContentLength(limitContentLength: Boolean) {
+        config().limitContentLength = limitContentLength
+    }
+
     fun getPageAnim(): Int {
         return config().pageAnim
     }
@@ -270,6 +278,7 @@ data class Book(
         var reverseToc: Boolean = false,
         var pageAnim: Int = -1,
         var reSegment: Boolean = false,
+        var limitContentLength: Boolean = true, //txt规则解析目录时超过规定的最大字数时均分txt
         var imageStyle: String? = null,
         var useReplaceRule: Boolean = AppConfig.replaceEnableDefault,// 正文使用净化替换规则
         var delTag: Long = 0L,//去除标签

+ 2 - 1
app/src/main/java/io/legado/app/model/ReadBook.kt

@@ -31,6 +31,7 @@ object ReadBook : CoroutineScope by MainScope() {
     var book: Book? = null
     var callBack: CallBack? = null
     var inBookshelf = false
+    var tocChanged = false
     var chapterSize = 0
     var durChapterIndex = 0
     var durChapterPos = 0
@@ -65,7 +66,7 @@ object ReadBook : CoroutineScope by MainScope() {
     fun upData(book: Book) {
         ReadBook.book = book
         chapterSize = appDb.bookChapterDao.getChapterCount(book.bookUrl)
-        if (durChapterIndex != book.durChapterIndex) {
+        if (durChapterIndex != book.durChapterIndex || tocChanged) {
             durChapterIndex = book.durChapterIndex
             durChapterPos = book.durChapterPos
             clearTextChapter()

+ 6 - 6
app/src/main/java/io/legado/app/model/localBook/LocalBook.kt

@@ -128,9 +128,9 @@ object LocalBook {
         val m1 = Pattern
             .compile("(.*?)《([^《》]+)》(.*)")
             .matcher(tempFileName)
-        //匹配 书名 by 作者名.txt
+        //匹配 书名 作者:作者名.txt
         val m2 = Pattern
-            .compile("(^)(.+) by (.+)$")
+            .compile("(^)(.+) 作者:(.+)$")
             .matcher(tempFileName)
 
         (m1.takeIf { m1.find() } ?: m2.takeIf { m2.find() }).run {
@@ -152,13 +152,13 @@ object LocalBook {
                     name = bookMess["name"] ?: tempFileName
                     author = bookMess["author"]?.takeIf { it.length != tempFileName.length } ?: ""
                 } catch (e: Exception) {
-                    name = tempFileName.replace(AppPattern.nameRegex, "")
-                    author = tempFileName.replace(AppPattern.authorRegex, "")
+                    name = BookHelp.formatBookName(tempFileName)
+                    author = BookHelp.formatBookAuthor(tempFileName.replace(name, ""))
                         .takeIf { it.length != tempFileName.length } ?: ""
                 }
             } else {
-                name = tempFileName.replace(AppPattern.nameRegex, "")
-                author = tempFileName.replace(AppPattern.authorRegex, "")
+                name = BookHelp.formatBookName(tempFileName)
+                author = BookHelp.formatBookAuthor(tempFileName.replace(name, ""))
                     .takeIf { it.length != tempFileName.length } ?: ""
             }
 

+ 2 - 1
app/src/main/java/io/legado/app/model/localBook/TextFile.kt

@@ -47,6 +47,7 @@ class TextFile(private val book: Book) {
     private val maxLengthWithNoToc = 10 * 1024
     //使用正则划分目录,每个章节的最大允许长度
     private val maxLengthWithToc = 102400
+    private val limitContentLength = book.getLimitContentLength()
 
     private val tocRules = arrayListOf<Pattern>()
     private var charset: Charset = book.fileCharset()
@@ -128,7 +129,7 @@ class TextFile(private val book: Book) {
                     val chapterContent = blockContent.substring(seekPos, chapterStart)
                     val chapterLength = chapterContent.toByteArray(charset).size
                     val lastStart = toc.lastOrNull()?.start ?: curOffset
-                    if (curOffset + chapterLength - lastStart > maxLengthWithToc) {
+                    if (limitContentLength && curOffset + chapterLength - lastStart > maxLengthWithToc) {
                         bis.close()
                         return analyze()
                     }

+ 16 - 0
app/src/main/java/io/legado/app/ui/book/info/BookInfoActivity.kt

@@ -91,6 +91,7 @@ class BookInfoActivity :
             viewModel.upEditBook()
         }
     }
+    private var tocChanged = false
 
     override val binding by viewBinding(ActivityBookInfoBinding::inflate)
     override val viewModel by viewModels<BookInfoViewModel>()
@@ -118,12 +119,16 @@ class BookInfoActivity :
     override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
         menu.findItem(R.id.menu_can_update)?.isChecked =
             viewModel.bookData.value?.canUpdate ?: true
+        menu.findItem(R.id.menu_limit_content_length)?.isChecked =
+            viewModel.bookData.value?.getLimitContentLength() ?: false
         menu.findItem(R.id.menu_login)?.isVisible =
             !viewModel.bookSource?.loginUrl.isNullOrBlank()
         menu.findItem(R.id.menu_set_source_variable)?.isVisible =
             viewModel.bookSource != null
         menu.findItem(R.id.menu_set_book_variable)?.isVisible =
             viewModel.bookSource != null
+        menu.findItem(R.id.menu_limit_content_length)?.isVisible =
+            viewModel.bookSource == null
         return super.onMenuOpened(featureId, menu)
     }
 
@@ -183,6 +188,15 @@ class BookInfoActivity :
             }
             R.id.menu_clear_cache -> viewModel.clearCache()
             R.id.menu_log -> showDialogFragment<AppLogDialog>()
+            R.id.menu_limit_content_length -> {
+                upLoading(true)
+                tocChanged = true
+                viewModel.bookData.value?.let {
+                    it.setLimitContentLength(!item.isChecked)
+                    viewModel.loadBookInfo(it, false)
+                }
+                if (item.isChecked) longToastOnUi(R.string.need_more_time_load_content)
+            }
         }
         return super.onCompatOptionsItemSelected(item)
     }
@@ -429,8 +443,10 @@ class BookInfoActivity :
                 Intent(this, ReadBookActivity::class.java)
                     .putExtra("bookUrl", book.bookUrl)
                     .putExtra("inBookshelf", viewModel.inBookshelf)
+                    .putExtra("tocChanged", tocChanged)
             )
         }
+        tocChanged = false
     }
 
     override val oldBook: Book?

+ 2 - 0
app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt

@@ -118,6 +118,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
             if (book.isLocalBook()) {
                 LocalBook.getChapterList(book).let {
                     appDb.bookDao.update(book)
+                    appDb.bookChapterDao.delByBook(book.bookUrl)
                     appDb.bookChapterDao.insert(*it.toTypedArray())
                     chapterListData.postValue(it)
                 }
@@ -127,6 +128,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) {
                         .onSuccess(IO) {
                             if (inBookshelf) {
                                 appDb.bookDao.update(book)
+                                appDb.bookChapterDao.delByBook(book.bookUrl)
                                 appDb.bookChapterDao.insert(*it.toTypedArray())
                             }
                             if (changeDruChapterIndex == null) {

+ 1 - 0
app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt

@@ -40,6 +40,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
     fun initData(intent: Intent) {
         execute {
             ReadBook.inBookshelf = intent.getBooleanExtra("inBookshelf", true)
+            ReadBook.tocChanged = intent.getBooleanExtra("tocChanged", false)
             val bookUrl = intent.getStringExtra("bookUrl")
             val book = when {
                 bookUrl.isNullOrEmpty() -> appDb.bookDao.lastReadBook

+ 6 - 0
app/src/main/res/menu/book_info.xml

@@ -55,6 +55,12 @@
         android:checkable="true"
         app:showAsAction="never" />
 
+    <item
+        android:id="@+id/menu_limit_content_length"
+        android:title="@string/limit_content_length"
+        android:checkable="true"
+        app:showAsAction="never" />
+
     <item
         android:id="@+id/menu_clear_cache"
         android:title="@string/clear_cache"

+ 2 - 0
app/src/main/res/values-es-rES/strings.xml

@@ -332,6 +332,8 @@
     <string name="tip_margin_change">Ajuste de margen</string>
     <string name="allow_update">Activar actualizaciones</string>
     <string name="disable_update">Desactivar actualizaciones</string>
+    <string name="limit_content_length">限制正文长度</string>
+    <string name="need_more_time_load_content">正文长度过长时,加载正文可能会花费更多时间</string>
     <string name="revert_selection">Invertir</string>
     <string name="search_book_key">Buscar libro por nombre o autor</string>
     <string name="debug_hint">Nombre del libro, autor, URL</string>

+ 2 - 0
app/src/main/res/values-ja-rJP/strings.xml

@@ -336,6 +336,8 @@
     <string name="tip_margin_change">Margin adjustment</string>
     <string name="allow_update">Enable update</string>
     <string name="disable_update">Disable update</string>
+    <string name="limit_content_length">限制正文长度</string>
+    <string name="need_more_time_load_content">正文长度过长时,加载正文可能会花费更多时间</string>
     <string name="revert_selection">Inverse</string>
     <string name="search_book_key">Search book name/author</string>
     <string name="debug_hint">Book name,Author,URL</string>

+ 2 - 0
app/src/main/res/values-zh-rHK/strings.xml

@@ -333,6 +333,8 @@
     <string name="tip_margin_change">Tip 邊距跟隨邊距調整</string>
     <string name="allow_update">允許更新</string>
     <string name="disable_update">禁止更新</string>
+    <string name="limit_content_length">限制正文长度</string>
+    <string name="need_more_time_load_content">正文长度过长时,加载正文可能会花费更多时间</string>
     <string name="revert_selection">反選</string>
     <string name="search_book_key">搜索書名、作者</string>
     <string name="debug_hint">書名、作者、URL</string>

+ 2 - 0
app/src/main/res/values-zh-rTW/strings.xml

@@ -335,6 +335,8 @@
     <string name="tip_margin_change">Tip邊距跟隨邊距調整</string>
     <string name="allow_update">允許更新</string>
     <string name="disable_update">禁止更新</string>
+    <string name="limit_content_length">限制正文长度</string>
+    <string name="need_more_time_load_content">正文长度过长时,加载正文可能会花费更多时间</string>
     <string name="revert_selection">反選</string>
     <string name="search_book_key">搜尋書名、作者</string>
     <string name="debug_hint">書名、作者、URL</string>

+ 2 - 0
app/src/main/res/values-zh/strings.xml

@@ -335,6 +335,8 @@
     <string name="tip_margin_change">Tip边距跟随边距调整</string>
     <string name="allow_update">允许更新</string>
     <string name="disable_update">禁止更新</string>
+    <string name="limit_content_length">限制正文长度</string>
+    <string name="need_more_time_load_content">正文长度过长时,加载正文可能会花费更多时间</string>
     <string name="revert_selection">反选</string>
     <string name="search_book_key">搜索书名、作者</string>
     <string name="debug_hint">书名、作者、URL</string>

+ 2 - 0
app/src/main/res/values/strings.xml

@@ -336,6 +336,8 @@
     <string name="tip_margin_change">Margin adjustment</string>
     <string name="allow_update">Enable update</string>
     <string name="disable_update">Disable update</string>
+    <string name="limit_content_length">限制正文长度</string>
+    <string name="need_more_time_load_content">正文长度过长时,加载正文可能会花费更多时间</string>
     <string name="revert_selection">Inverse</string>
     <string name="search_book_key">Search book name/author</string>
     <string name="debug_hint">Book name,Author,URL</string>