This commit is contained in:
Horis
2025-05-24 11:53:29 +08:00
parent d46249be0d
commit 3baaf6ecfd
5 changed files with 18 additions and 9 deletions
@@ -245,6 +245,8 @@ object LocalBook {
} else { } else {
deleteBook(book, false) deleteBook(book, false)
upBookInfo(book) upBookInfo(book)
// 触发 isLocalModified
book.latestChapterTime = 0
//已有书籍说明是更新,删除原有目录 //已有书籍说明是更新,删除原有目录
appDb.bookChapterDao.delByBook(bookUrl) appDb.bookChapterDao.delByBook(bookUrl)
} }
@@ -76,16 +76,17 @@ class TextFile(private var book: Book) {
*/ */
@Throws(FileNotFoundException::class, SecurityException::class, EmptyFileException::class) @Throws(FileNotFoundException::class, SecurityException::class, EmptyFileException::class)
fun getChapterList(): ArrayList<BookChapter> { fun getChapterList(): ArrayList<BookChapter> {
if (book.charset == null || book.tocUrl.isBlank()) { val modified = book.isLocalModified()
if (book.charset == null || book.tocUrl.isBlank() || modified) {
LocalBook.getBookInputStream(book).use { bis -> LocalBook.getBookInputStream(book).use { bis ->
val buffer = ByteArray(bufferSize) val buffer = ByteArray(bufferSize)
val length = bis.read(buffer) val length = bis.read(buffer)
if (length == -1) throw EmptyFileException("Unexpected Empty Txt File") if (length == -1) throw EmptyFileException("Unexpected Empty Txt File")
if (book.charset.isNullOrBlank()) { if (book.charset.isNullOrBlank() || modified) {
book.charset = EncodingDetect.getEncode(buffer.copyOf(length)) book.charset = EncodingDetect.getEncode(buffer.copyOf(length))
} }
charset = book.fileCharset() charset = book.fileCharset()
if (book.tocUrl.isBlank()) { if (book.tocUrl.isBlank() || modified) {
val blockContent = String(buffer, 0, length, charset) val blockContent = String(buffer, 0, length, charset)
book.tocUrl = getTocRule(blockContent)?.pattern() ?: "" book.tocUrl = getTocRule(blockContent)?.pattern() ?: ""
} }
@@ -223,7 +224,7 @@ class TextFile(private var book: Book) {
*/ */
if (toc.isEmpty()) { //如果当前没有章节,那么就是序章 if (toc.isEmpty()) { //如果当前没有章节,那么就是序章
//加入简介 //加入简介
if (StringUtils.trim(chapterContent).isNotEmpty()) { if (chapterContent.isNotBlank()) {
val qyChapter = BookChapter() val qyChapter = BookChapter()
qyChapter.title = "前言" qyChapter.title = "前言"
qyChapter.start = curOffset qyChapter.start = curOffset
@@ -195,9 +195,11 @@ class TocActivity : VMBaseActivity<ActivityChapterListBinding, TocViewModel>(),
waitDialog.show() waitDialog.show()
viewModel.upBookTocRule(book) { viewModel.upBookTocRule(book) {
waitDialog.dismiss() waitDialog.dismiss()
ReadBook.book?.let { readBook -> if (ReadBook.book == book) {
if (readBook == book) { if (it == null) {
ReadBook.upMsg(null) ReadBook.upMsg(null)
} else {
ReadBook.upMsg("LoadTocError:${it.localizedMessage}")
} }
} }
} }
@@ -35,7 +35,7 @@ class TocViewModel(application: Application) : BaseViewModel(application) {
} }
} }
fun upBookTocRule(book: Book, finally: () -> Unit) { fun upBookTocRule(book: Book, complete: (Throwable?) -> Unit) {
execute { execute {
appDb.bookDao.update(book) appDb.bookDao.update(book)
LocalBook.getChapterList(book).let { LocalBook.getChapterList(book).let {
@@ -45,8 +45,10 @@ class TocViewModel(application: Application) : BaseViewModel(application) {
ReadBook.onChapterListUpdated(book) ReadBook.onChapterListUpdated(book)
bookData.postValue(book) bookData.postValue(book)
} }
}.onFinally { }.onSuccess {
finally.invoke() complete.invoke(null)
}.onError {
complete.invoke(it)
} }
} }
@@ -55,6 +55,7 @@ object RhinoClassShutter : ClassShutter {
"java.lang.ProcessImpl", "java.lang.ProcessImpl",
"java.lang.UNIXProcess", "java.lang.UNIXProcess",
"java.io.File", "java.io.File",
"java.io.FileDescriptor",
"java.io.FileInputStream", "java.io.FileInputStream",
"java.io.FileOutputStream", "java.io.FileOutputStream",
"java.io.PrintStream", "java.io.PrintStream",
@@ -113,6 +114,7 @@ object RhinoClassShutter : ClassShutter {
"com.script", "com.script",
"org.mozilla", "org.mozilla",
"sun", "sun",
"libcore",
).let { ClassNameMatcher(it) } ).let { ClassNameMatcher(it) }
} }