feat: 优化

pull/201/head
kunfei 5 years ago
parent bf27baccd1
commit 7a4fb24055
  1. 16
      app/src/main/java/io/legado/app/help/BookHelp.kt
  2. 11
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  3. 3
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt

@ -196,7 +196,7 @@ object BookHelp {
origin: String?, origin: String?,
content: String, content: String,
enableReplace: Boolean enableReplace: Boolean
): String { ): List<String> {
var c = content var c = content
if (enableReplace) { if (enableReplace) {
synchronized(this) { synchronized(this) {
@ -228,9 +228,6 @@ object BookHelp {
} }
} }
} }
if (!c.substringBefore("\n").contains(title)) {
c = "$title\n$c"
}
try { try {
when (AppConfig.chineseConverterType) { when (AppConfig.chineseConverterType) {
1 -> c = HanLP.convertToSimplifiedChinese(c) 1 -> c = HanLP.convertToSimplifiedChinese(c)
@ -241,8 +238,13 @@ object BookHelp {
App.INSTANCE.toast("简繁转换出错") App.INSTANCE.toast("简繁转换出错")
} }
} }
return c val contents = arrayListOf<String>()
.replace("\\s*\\n+\\s*".toRegex(), "\n${ReadBookConfig.bodyIndent}") c.split("\n").forEach {
.replace("[\\n\\s]+$".toRegex(), "") //移除尾部空行 val str = it.replace("^\\s+".toRegex(), "")
if (str.isNotEmpty()) {
contents.add("${ReadBookConfig.bodyIndent}str")
}
}
return contents
} }
} }

@ -293,7 +293,7 @@ object ReadBook {
) { ) {
Coroutine.async { Coroutine.async {
if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) { if (chapter.index in durChapterIndex - 1..durChapterIndex + 1) {
val c = BookHelp.disposeContent( val contents = BookHelp.disposeContent(
chapter.title, chapter.title,
book!!.name, book!!.name,
webBook?.bookSource?.bookSourceUrl, webBook?.bookSource?.bookSourceUrl,
@ -302,18 +302,21 @@ object ReadBook {
) )
when (chapter.index) { when (chapter.index) {
durChapterIndex -> { durChapterIndex -> {
curTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize) curTextChapter =
ChapterProvider.getTextChapter(chapter, contents, chapterSize)
if (upContent) callBack?.upContent(resetPageOffset = resetPageOffset) if (upContent) callBack?.upContent(resetPageOffset = resetPageOffset)
callBack?.upView() callBack?.upView()
curPageChanged() curPageChanged()
callBack?.contentLoadFinish() callBack?.contentLoadFinish()
} }
durChapterIndex - 1 -> { durChapterIndex - 1 -> {
prevTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize) prevTextChapter =
ChapterProvider.getTextChapter(chapter, contents, chapterSize)
if (upContent) callBack?.upContent(-1, resetPageOffset) if (upContent) callBack?.upContent(-1, resetPageOffset)
} }
durChapterIndex + 1 -> { durChapterIndex + 1 -> {
nextTextChapter = ChapterProvider.getTextChapter(chapter, c, chapterSize) nextTextChapter =
ChapterProvider.getTextChapter(chapter, contents, chapterSize)
if (upContent) callBack?.upContent(1, resetPageOffset) if (upContent) callBack?.upContent(1, resetPageOffset)
} }
} }

@ -44,14 +44,13 @@ object ChapterProvider {
*/ */
fun getTextChapter( fun getTextChapter(
bookChapter: BookChapter, bookChapter: BookChapter,
content: String, contents: List<String>,
chapterSize: Int chapterSize: Int
): TextChapter { ): TextChapter {
val textPages = arrayListOf<TextPage>() val textPages = arrayListOf<TextPage>()
val pageLines = arrayListOf<Int>() val pageLines = arrayListOf<Int>()
val pageLengths = arrayListOf<Int>() val pageLengths = arrayListOf<Int>()
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
val contents = content.split("\n")
var durY = 0f var durY = 0f
textPages.add(TextPage()) textPages.add(TextPage())
for ((index, text) in contents.withIndex()) { for ((index, text) in contents.withIndex()) {

Loading…
Cancel
Save