feat: 优化代码

pull/169/head^2
kunfei 5 years ago
parent 2ceb71ee1a
commit 7768773e65
  1. 2
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  2. 3
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  3. 41
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt

@ -111,9 +111,11 @@ abstract class BaseReadAloudService : BaseService(),
if (getPrefBoolean(PreferKey.readAloudByPage)) { if (getPrefBoolean(PreferKey.readAloudByPage)) {
for (index in pageIndex..textChapter.lastIndex()) { for (index in pageIndex..textChapter.lastIndex()) {
textChapter.page(index)?.text?.split("\n")?.let { textChapter.page(index)?.text?.split("\n")?.let {
if (it.isNotEmpty()) {
contentList.addAll(it) contentList.addAll(it)
} }
} }
}
} else { } else {
contentList.addAll(textChapter.getUnRead(pageIndex).split("\n")) contentList.addAll(textChapter.getUnRead(pageIndex).split("\n"))
} }

@ -609,9 +609,6 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
* 朗读按钮 * 朗读按钮
*/ */
override fun onClickReadAloud() { override fun onClickReadAloud() {
if (!BaseReadAloudService.isRun) {
SystemUtils.ignoreBatteryOptimization(this)
}
when { when {
!BaseReadAloudService.isRun -> ReadBook.readAloud() !BaseReadAloudService.isRun -> ReadBook.readAloud()
BaseReadAloudService.pause -> ReadAloud.resume(this) BaseReadAloudService.pause -> ReadAloud.resume(this)

@ -12,10 +12,7 @@ data class TextChapter(
val chaptersSize: Int val chaptersSize: Int
) { ) {
fun page(index: Int): TextPage? { fun page(index: Int): TextPage? {
if (index >= 0 && index < pages.size) { return pages.getOrNull(index)
return pages[index]
}
return null
} }
fun lastPage(): TextPage? { fun lastPage(): TextPage? {
@ -25,20 +22,6 @@ data class TextChapter(
return null return null
} }
fun scrollPage(): TextPage? {
if (pages.isNotEmpty()) {
val stringBuilder = StringBuilder()
pages.forEach {
stringBuilder.append(it.text)
}
return TextPage(
index = 0, text = stringBuilder.toString(), title = title,
pageSize = pages.size, chapterSize = chaptersSize, chapterIndex = position
)
}
return null
}
fun lastIndex(): Int { fun lastIndex(): Int {
return pages.size - 1 return pages.size - 1
} }
@ -70,28 +53,6 @@ data class TextChapter(
return stringBuilder.toString() return stringBuilder.toString()
} }
fun getStartLine(pageIndex: Int): Int {
if (pageLines.size > pageIndex) {
var lines = 0
for (index: Int in 0 until pageIndex) {
lines += pageLines[index] + 1
}
return lines
}
return 0
}
fun getPageIndex(line: Int): Int {
var lines = 0
for (pageIndex in pageLines.indices) {
lines += pageLines[pageIndex] + 1
if (line < lines) {
return pageIndex
}
}
return 0
}
fun getContent(): String { fun getContent(): String {
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
pages.forEach { pages.forEach {

Loading…
Cancel
Save