pull/32/head
kunfei 5 years ago
parent cff8b28a51
commit 7d516587b3
  1. 8
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt
  2. 18
      app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt
  3. 4
      app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt
  4. 10
      app/src/main/java/io/legado/app/ui/widget/page/PageView.kt
  5. 8
      app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt
  6. 25
      app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt

@ -256,6 +256,14 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
} }
} }
override fun moveToNextChapter() {
viewModel.moveToNextChapter()
}
override fun moveToPrevChapter() {
viewModel.moveToPrevChapter()
}
private fun onClickReadAloud() { private fun onClickReadAloud() {
if (!ReadAloudService.isRun) { if (!ReadAloudService.isRun) {
readAloudStatus = Status.STOP readAloudStatus = Status.STOP

@ -197,6 +197,24 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) {
} }
} }
fun moveToNextChapter() {
durChapterIndex++
prevTextChapter = curTextChapter
curTextChapter = nextTextChapter
bookData.value?.let {
loadContent(it, durChapterIndex.plus(1))
}
}
fun moveToPrevChapter() {
durChapterIndex--
nextTextChapter = curTextChapter
curTextChapter = prevTextChapter
bookData.value?.let {
loadContent(it, durChapterIndex.minus(1))
}
}
override fun onCleared() { override fun onCleared() {
super.onCleared() super.onCleared()
ReadAloudService.stop(context) ReadAloudService.stop(context)

@ -20,6 +20,10 @@ interface DataSource {
fun hasPrevChapter(): Boolean fun hasPrevChapter(): Boolean
fun moveToNextChapter()
fun moveToPrevChapter()
interface CallBack { interface CallBack {
fun onLoadFinish(bookChapter: BookChapter, content: String) fun onLoadFinish(bookChapter: BookChapter, content: String)
} }

@ -70,6 +70,14 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att
} }
return false return false
} }
override fun moveToNextChapter() {
callback?.moveToNextChapter()
}
override fun moveToPrevChapter() {
callback?.moveToPrevChapter()
}
})) }))
} }
@ -201,5 +209,7 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att
fun durChapterPos(pageSize: Int): Int fun durChapterPos(pageSize: Int): Int
fun textChapter(chapterOnDur: Int = 0): TextChapter? fun textChapter(chapterOnDur: Int = 0): TextChapter?
fun loadChapter(index: Int) fun loadChapter(index: Int)
fun moveToNextChapter()
fun moveToPrevChapter()
} }
} }

@ -20,6 +20,14 @@ data class TextChapter(
return null return null
} }
fun lastIndex(): Int {
return pages.size - 1
}
fun isLastIndex(index: Int): Boolean {
return index == pages.size - 1
}
fun pageSize(): Int { fun pageSize(): Int {
return pages.size return pages.size
} }

@ -39,7 +39,12 @@ class TextPageFactory private constructor(dataSource: DataSource) :
override fun moveToNext(): Boolean { override fun moveToNext(): Boolean {
return if (hasNext()) { return if (hasNext()) {
index = index.plus(1) index = if (dataSource.getCurrentChapter()?.isLastIndex(index) == true) {
dataSource.moveToNextChapter()
0
} else {
index.plus(1)
}
true true
} else } else
false false
@ -47,7 +52,12 @@ class TextPageFactory private constructor(dataSource: DataSource) :
override fun moveToPrevious(): Boolean { override fun moveToPrevious(): Boolean {
return if (hasPrev()) { return if (hasPrev()) {
index = index.minus(1) index = if (index > 0) {
index.minus(1)
} else {
dataSource.moveToPrevChapter()
dataSource.getPreviousChapter()?.lastIndex() ?: 0
}
true true
} else } else
false false
@ -59,14 +69,25 @@ class TextPageFactory private constructor(dataSource: DataSource) :
} }
override fun nextPage(): TextPage? { override fun nextPage(): TextPage? {
dataSource.getCurrentChapter()?.let {
if (index < it.pageSize() - 1) {
return dataSource.getCurrentChapter()?.page(index + 1) return dataSource.getCurrentChapter()?.page(index + 1)
?: TextPage(index + 1, "index:${index + 1}", "index:${index + 1}") ?: TextPage(index + 1, "index:${index + 1}", "index:${index + 1}")
} }
}
return dataSource.getNextChapter()?.page(0)
?: TextPage(index + 1, "index:${index + 1}", "index:${index + 1}")
}
override fun previousPage(): TextPage? { override fun previousPage(): TextPage? {
if (index > 0) {
return dataSource.getCurrentChapter()?.page(index - 1) return dataSource.getCurrentChapter()?.page(index - 1)
?: TextPage(index - 1, "index:${index - 1}", "index:${index - 1}") ?: TextPage(index - 1, "index:${index - 1}", "index:${index - 1}")
} }
return dataSource.getPreviousChapter()?.lastPage()
?: TextPage(index - 1, "index:${index - 1}", "index:${index - 1}")
}
} }

Loading…
Cancel
Save