feat: 优化代码

pull/178/head
kunfei 5 years ago
parent 9881d6e95b
commit a2082857fc
  1. 99
      app/src/main/java/io/legado/app/model/localBook/AnalyzeTxtFile.kt
  2. 1
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  3. 1
      app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt
  4. 9
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt
  5. 1
      app/src/main/res/values/strings.xml

@ -60,18 +60,18 @@ class AnalyzeTxtFile {
var allLength = 0
//获取文件中的数据到buffer,直到没有数据为止
while (bookStream.read(buffer, 0, buffer.size).also { length = it } > 0) {
while (bookStream.read(buffer).also { length = it } > 0) {
blockPos++
var blockContent = String(buffer, charset)
val lastN = blockContent.lastIndexOf("\n")
if (lastN > 0) {
blockContent = blockContent.substring(0, lastN)
length = blockContent.toByteArray(charset).size
allLength += length
bookStream.seek(allLength.toLong())
}
//如果存在Chapter
if (rulePattern != null) { //将数据转换成String
var blockContent = String(buffer, charset)
val lastN = blockContent.lastIndexOf("\n")
if (lastN > 0) {
blockContent = blockContent.substring(0, lastN)
length = blockContent.toByteArray(charset).size
allLength += length
bookStream.seek(allLength.toLong())
}
//当前Block下使过的String的指针
var seekPos = 0
//进行正则匹配
@ -81,47 +81,62 @@ class AnalyzeTxtFile {
val chapterStart = matcher.start()
//获取章节内容
val chapterContent = blockContent.substring(seekPos, chapterStart)
if (chapterContent.length > 30000 && pattern == null) {
val chapterLength = chapterContent.toByteArray(charset).size
if (chapterLength > 30000 && pattern == null) {
//移除不匹配的规则
tocRules.remove(tocRule)
return analyze(bookStream, book, null)
}
//如果 seekPos == 0 && nextChapterPos != 0 表示当前block处前面有一段内容
//第一种情况一定是序章 第二种情况可能是上一个章节的内容
//第一种情况一定是序章 第二种情况是上一个章节的内容
if (seekPos == 0 && chapterStart != 0) { //获取当前章节的内容
val contentSize = chapterContent.toByteArray(charset).size.toLong()
if (toc.isEmpty()) {
if (toc.isEmpty()) { //如果当前没有章节,那么就是序章
//加入简介
if (StringUtils.trim(chapterContent).isNotEmpty()) {
val chapter = BookChapter()
chapter.title = "前言"
chapter.start = 0
chapter.end = contentSize
toc.add(chapter)
}
} else {
toc.last().let {
it.end = it.end!! + contentSize
val qyChapter = BookChapter()
qyChapter.title = "前言"
qyChapter.start = 0
qyChapter.end = chapterLength.toLong()
toc.add(qyChapter)
}
//创建当前章节
val curChapter = BookChapter()
curChapter.title = matcher.group()
curChapter.start = chapterLength.toLong()
toc.add(curChapter)
} else { //否则就block分割之后,上一个章节的剩余内容
//获取上一章节
val lastChapter = toc.last()
//将当前段落添加上一章去
lastChapter.end =
lastChapter.end!! + chapterLength.toLong()
//创建当前章节
val curChapter = BookChapter()
curChapter.title = matcher.group()
curChapter.start = lastChapter.end
toc.add(curChapter)
}
//创建当前章节
val curChapter = BookChapter()
curChapter.title = matcher.group()
curChapter.start = (toc.lastOrNull()?.end ?: 0) + contentSize
toc.add(curChapter)
} else {
val lastChapter = toc.lastOrNull()
lastChapter?.let {
//上一章节结束等于这一章节开始
it.end = it.start!! + chapterContent.toByteArray(charset).size
if (toc.isNotEmpty()) { //获取章节内容
//获取上一章节
val lastChapter = toc.last()
lastChapter.end =
lastChapter.start!! + chapterContent.toByteArray(charset).size.toLong()
//创建当前章节
val curChapter = BookChapter()
curChapter.title = matcher.group()
curChapter.start = lastChapter.end
toc.add(curChapter)
} else { //如果章节不存在则创建章节
val curChapter = BookChapter()
curChapter.title = matcher.group()
curChapter.start = 0
curChapter.end = 0
toc.add(curChapter)
}
//创建当前章节
val curChapter = BookChapter()
curChapter.title = matcher.group()
curChapter.start = lastChapter?.end ?: 0
toc.add(curChapter)
}
//设置指针偏移
seekPos += chapterContent.length
seekPos += chapterLength
}
} else { //进行本地虚拟分章
//章节在buffer的偏移量
@ -165,7 +180,8 @@ class AnalyzeTxtFile {
//block的偏移点
curOffset += length.toLong()
if (rulePattern != null) { //设置上一章的结尾
if (rulePattern != null) {
//设置上一章的结尾
val lastChapter = toc.last()
lastChapter.end = curOffset
}
@ -231,10 +247,9 @@ class AnalyzeTxtFile {
val bookFile = getBookFile(App.INSTANCE, book)
//获取文件流
val bookStream = RandomAccessFile(bookFile, "r")
bookStream.seek(bookChapter.start ?: 0)
val extent = (bookChapter.end!! - bookChapter.start!!).toInt()
val content = ByteArray(extent)
bookStream.read(content, 0, extent)
val content = ByteArray((bookChapter.end!! - bookChapter.start!!).toInt())
bookStream.seek(bookChapter.start!!)
bookStream.read(content)
return String(content, book.fileCharset())
}

@ -225,6 +225,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
R.id.menu_copy_text ->
TextDialog.show(supportFragmentManager, ReadBook.curTextChapter?.getContent())
R.id.menu_update_toc -> ReadBook.book?.let {
ReadBook.upMsg(getString(R.string.toc_updateing))
viewModel.loadChapterList(it)
}
R.id.menu_enable_replace -> ReadBook.book?.let {

@ -107,6 +107,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
App.db.bookChapterDao().insert(*it.toTypedArray())
App.db.bookDao().update(book)
ReadBook.chapterSize = it.size
ReadBook.upMsg(null)
ReadBook.loadContent(resetPageOffset = true)
}
} else {

@ -44,12 +44,11 @@ data class TextPage(
if (y < 0) y = 0f
for (lineIndex in 0 until layout.lineCount) {
val textLine = TextLine()
textLine.lineTop = (ChapterProvider.paddingTop + y -
(layout.getLineBottom(lineIndex) - layout.getLineTop(lineIndex)))
textLine.lineBase = (ChapterProvider.paddingTop + y -
(layout.getLineBottom(lineIndex) - layout.getLineBaseline(lineIndex)))
textLine.lineTop = ChapterProvider.paddingTop + y + layout.getLineTop(lineIndex)
textLine.lineBase =
ChapterProvider.paddingTop + y + layout.getLineBaseline(lineIndex)
textLine.lineBottom =
textLine.lineBase + ChapterProvider.contentPaint.fontMetrics.descent
ChapterProvider.paddingTop + y + layout.getLineBottom(lineIndex)
var x = ChapterProvider.paddingLeft +
(ChapterProvider.visibleWidth - layout.getLineMax(lineIndex)) / 2
textLine.text =

@ -647,4 +647,5 @@
<string name="tip_local_perm_request_storage">阅读需要访问存储卡权限,请点击下方的"授予权限"按钮,或前往“设置”—“应用权限”—打开所需权限。如果授予权限后仍然不正常,请点击右上角的“选择文件夹”,使用系统文件夹选择器。</string>
<string name="alouding_disable">全文朗读中不能朗读选中文字</string>
<string name="read_body_to_lh">扩展到刘海</string>
<string name="toc_updateing">更新目录中</string>
</resources>

Loading…
Cancel
Save