feat: 优化代码

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

@ -225,6 +225,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
R.id.menu_copy_text -> R.id.menu_copy_text ->
TextDialog.show(supportFragmentManager, ReadBook.curTextChapter?.getContent()) TextDialog.show(supportFragmentManager, ReadBook.curTextChapter?.getContent())
R.id.menu_update_toc -> ReadBook.book?.let { R.id.menu_update_toc -> ReadBook.book?.let {
ReadBook.upMsg(getString(R.string.toc_updateing))
viewModel.loadChapterList(it) viewModel.loadChapterList(it)
} }
R.id.menu_enable_replace -> ReadBook.book?.let { 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.bookChapterDao().insert(*it.toTypedArray())
App.db.bookDao().update(book) App.db.bookDao().update(book)
ReadBook.chapterSize = it.size ReadBook.chapterSize = it.size
ReadBook.upMsg(null)
ReadBook.loadContent(resetPageOffset = true) ReadBook.loadContent(resetPageOffset = true)
} }
} else { } else {

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

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

Loading…
Cancel
Save