feat: 优化

pull/201/head
kunfei 5 years ago
parent f9ae176688
commit 0b4dcdcee8
  1. 2
      app/src/main/java/io/legado/app/service/BaseReadAloudService.kt
  2. 6
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  3. 2
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  4. 14
      app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt
  5. 21
      app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt

@ -109,7 +109,7 @@ abstract class BaseReadAloudService : BaseService(),
readAloudNumber = textChapter.getReadLength(pageIndex) readAloudNumber = textChapter.getReadLength(pageIndex)
contentList.clear() contentList.clear()
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 {
contentList.addAll(it) contentList.addAll(it)
} }

@ -107,7 +107,7 @@ object ReadBook {
fun moveToPrevChapter(upContent: Boolean, toLast: Boolean = true): Boolean { fun moveToPrevChapter(upContent: Boolean, toLast: Boolean = true): Boolean {
if (durChapterIndex > 0) { if (durChapterIndex > 0) {
durPageIndex = if (toLast) prevTextChapter?.lastIndex() ?: 0 else 0 durPageIndex = if (toLast) prevTextChapter?.lastIndex ?: 0 else 0
durChapterIndex-- durChapterIndex--
nextTextChapter = curTextChapter nextTextChapter = curTextChapter
curTextChapter = prevTextChapter curTextChapter = prevTextChapter
@ -176,10 +176,10 @@ object ReadBook {
fun durChapterPos(): Int { fun durChapterPos(): Int {
curTextChapter?.let { curTextChapter?.let {
if (durPageIndex < it.pageSize()) { if (durPageIndex < it.pageSize) {
return durPageIndex return durPageIndex
} }
return it.pageSize() - 1 return it.pageSize - 1
} }
return durPageIndex return durPageIndex
} }

@ -507,7 +507,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} else { } else {
tv_chapter_url.gone() tv_chapter_url.gone()
} }
seek_read_page.max = it.pageSize().minus(1) seek_read_page.max = it.pageSize.minus(1)
seek_read_page.progress = ReadBook.durPageIndex seek_read_page.progress = ReadBook.durPageIndex
tv_pre.isEnabled = ReadBook.durChapterIndex != 0 tv_pre.isEnabled = ReadBook.durChapterIndex != 0
tv_next.isEnabled = ReadBook.durChapterIndex != ReadBook.chapterSize - 1 tv_next.isEnabled = ReadBook.durChapterIndex != ReadBook.chapterSize - 1

@ -14,7 +14,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
} }
override fun hasNextPlus(): Boolean = with(dataSource) { override fun hasNextPlus(): Boolean = with(dataSource) {
return hasNextChapter() || pageIndex < (currentChapter?.pageSize() ?: 1) - 2 return hasNextChapter() || pageIndex < (currentChapter?.pageSize ?: 1) - 2
} }
override fun moveToFirst() { override fun moveToFirst() {
@ -23,10 +23,10 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
override fun moveToLast() = with(dataSource) { override fun moveToLast() = with(dataSource) {
currentChapter?.let { currentChapter?.let {
if (it.pageSize() == 0) { if (it.pageSize == 0) {
ReadBook.setPageIndex(0) ReadBook.setPageIndex(0)
} else { } else {
ReadBook.setPageIndex(it.pageSize().minus(1)) ReadBook.setPageIndex(it.pageSize.minus(1))
} }
} ?: ReadBook.setPageIndex(0) } ?: ReadBook.setPageIndex(0)
} }
@ -75,7 +75,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
return@with TextPage(text = it).format() return@with TextPage(text = it).format()
} }
currentChapter?.let { currentChapter?.let {
if (pageIndex < it.pageSize() - 1) { if (pageIndex < it.pageSize - 1) {
return@with it.page(pageIndex + 1)?.removePageAloudSpan() return@with it.page(pageIndex + 1)?.removePageAloudSpan()
?: TextPage(title = it.title).format() ?: TextPage(title = it.title).format()
} }
@ -102,7 +102,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
} }
} }
prevChapter?.let { prevChapter?.let {
return@with it.lastPage()?.removePageAloudSpan() return@with it.lastPage?.removePageAloudSpan()
?: TextPage(title = it.title).format() ?: TextPage(title = it.title).format()
} }
return TextPage().format() return TextPage().format()
@ -111,12 +111,12 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
override val nextPagePlus: TextPage override val nextPagePlus: TextPage
get() = with(dataSource) { get() = with(dataSource) {
currentChapter?.let { currentChapter?.let {
if (pageIndex < it.pageSize() - 2) { if (pageIndex < it.pageSize - 2) {
return@with it.page(pageIndex + 2)?.removePageAloudSpan() return@with it.page(pageIndex + 2)?.removePageAloudSpan()
?: TextPage(title = it.title).format() ?: TextPage(title = it.title).format()
} }
nextChapter?.let { nc -> nextChapter?.let { nc ->
if (pageIndex < it.pageSize() - 1) { if (pageIndex < it.pageSize - 1) {
return@with nc.page(0)?.removePageAloudSpan() return@with nc.page(0)?.removePageAloudSpan()
?: TextPage(title = nc.title).format() ?: TextPage(title = nc.title).format()
} }

@ -15,25 +15,16 @@ data class TextChapter(
return pages.getOrNull(index) return pages.getOrNull(index)
} }
fun lastPage(): TextPage? { val lastPage: TextPage? get() = pages.lastOrNull()
if (pages.isNotEmpty()) {
return pages[pages.lastIndex]
}
return null
}
fun lastIndex(): Int { val lastIndex: Int get() = pages.lastIndex
return pages.size - 1
} val pageSize: Int get() = pages.size
fun isLastIndex(index: Int): Boolean { fun isLastIndex(index: Int): Boolean {
return index >= pages.size - 1 return index >= pages.size - 1
} }
fun pageSize(): Int {
return pages.size
}
fun getReadLength(pageIndex: Int): Int { fun getReadLength(pageIndex: Int): Int {
var length = 0 var length = 0
val maxIndex = min(pageIndex, pages.size) val maxIndex = min(pageIndex, pages.size)
@ -45,8 +36,8 @@ data class TextChapter(
fun getUnRead(pageIndex: Int): String { fun getUnRead(pageIndex: Int): String {
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
if (pageIndex < pages.size && pages.isNotEmpty()) { if (pages.isNotEmpty()) {
for (index in pageIndex..lastIndex()) { for (index in pageIndex..pages.lastIndex) {
stringBuilder.append(pages[index].text) stringBuilder.append(pages[index].text)
} }
} }

Loading…
Cancel
Save