feat: 优化代码

pull/106/head
kunfei 5 years ago
parent 861f04c56a
commit 146f012afc
  1. 13
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 4
      app/src/main/java/io/legado/app/ui/book/read/page/ChapterProvider.kt
  3. 1
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt
  4. 7
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt

@ -46,9 +46,7 @@ import io.legado.app.utils.*
import kotlinx.android.synthetic.main.activity_book_read.* import kotlinx.android.synthetic.main.activity_book_read.*
import kotlinx.android.synthetic.main.view_read_menu.* import kotlinx.android.synthetic.main.view_read_menu.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Dispatchers.Main
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import org.jetbrains.anko.sdk27.listeners.onClick import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivity
import org.jetbrains.anko.startActivityForResult import org.jetbrains.anko.startActivityForResult
@ -543,12 +541,11 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
observeEventSticky<Int>(EventBus.TTS_START) { chapterStart -> observeEventSticky<Int>(EventBus.TTS_START) { chapterStart ->
launch(IO) { launch(IO) {
if (BaseReadAloudService.isPlay()) { if (BaseReadAloudService.isPlay()) {
ReadBook.curTextChapter?.let { ReadBook.curTextChapter?.let { textChapter ->
val pageStart = chapterStart - it.getReadLength(ReadBook.durPageIndex) val pageStart =
it.page(ReadBook.durPageIndex)?.upPageAloudSpan(pageStart) chapterStart - textChapter.getReadLength(ReadBook.durPageIndex)
withContext(Main) { textChapter.page(ReadBook.durPageIndex)?.upPageAloudSpan(pageStart)
page_view.upContent() upContent()
}
} }
} }
} }

@ -178,6 +178,7 @@ object ChapterProvider {
val words = val words =
title.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) title.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex))
stringBuilder.append(words) stringBuilder.append(words)
textLine.text = words
val desiredWidth = layout.getLineMax(lineIndex) val desiredWidth = layout.getLineMax(lineIndex)
if (lineIndex != layout.lineCount - 1) { if (lineIndex != layout.lineCount - 1) {
val gapCount: Int = words.length - 1 val gapCount: Int = words.length - 1
@ -201,6 +202,7 @@ object ChapterProvider {
} }
} else { } else {
//最后一行 //最后一行
textLine.text = "$words\n"
stringBuilder.append("\n") stringBuilder.append("\n")
var x = 0 var x = 0
for (i in words.indices) { for (i in words.indices) {
@ -262,6 +264,7 @@ object ChapterProvider {
var words = var words =
text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex)) text.substring(layout.getLineStart(lineIndex), layout.getLineEnd(lineIndex))
stringBuilder.append(words) stringBuilder.append(words)
textLine.text = words
val desiredWidth = layout.getLineMax(lineIndex) val desiredWidth = layout.getLineMax(lineIndex)
if (lineIndex == 0 && layout.lineCount > 1) { if (lineIndex == 0 && layout.lineCount > 1) {
//第一行 //第一行
@ -297,6 +300,7 @@ object ChapterProvider {
} else if (lineIndex == layout.lineCount - 1) { } else if (lineIndex == layout.lineCount - 1) {
//最后一行 //最后一行
stringBuilder.append("\n") stringBuilder.append("\n")
textLine.text = "$words\n"
var x = 0 var x = 0
for (i in words.indices) { for (i in words.indices) {
val char = words[i].toString() val char = words[i].toString()

@ -1,6 +1,7 @@
package io.legado.app.ui.book.read.page.entities package io.legado.app.ui.book.read.page.entities
data class TextLine( data class TextLine(
var text: String = "",
val textChars: ArrayList<TextChar> = arrayListOf(), val textChars: ArrayList<TextChar> = arrayListOf(),
var lineTop: Int = 0, var lineTop: Int = 0,
var lineBottom: Int = 0, var lineBottom: Int = 0,

@ -24,10 +24,9 @@ data class TextPage(
fun upPageAloudSpan(pageStart: Int) { fun upPageAloudSpan(pageStart: Int) {
if (text is SpannableStringBuilder) { if (text is SpannableStringBuilder) {
removePageAloudSpan() removePageAloudSpan()
var end = text.indexOf("\n", pageStart) for (textLine in textLines) {
if (end == -1) end = text.length
var start = text.lastIndexOf("\n", pageStart) }
if (start == -1) start = 0
} }
} }
} }

Loading…
Cancel
Save