pull/32/head
kunfei 5 years ago
parent dcaac551f4
commit f06bdf0fe5
  1. 16
      app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt
  2. 4
      app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt
  3. 25
      app/src/main/java/io/legado/app/ui/widget/page/PageView.kt
  4. 34
      app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt

@ -249,11 +249,19 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
return viewModel.durChapterIndex return viewModel.durChapterIndex
} }
override fun durChapterPos(pageSize: Int): Int { override fun durChapterPos(): Int {
if (viewModel.durPageIndex < pageSize) { viewModel.curTextChapter?.let {
return viewModel.durPageIndex if (viewModel.durPageIndex < it.pageSize()) {
return viewModel.durPageIndex
}
return it.pageSize() - 1
} }
return pageSize - 1 return viewModel.durPageIndex
}
override fun setPageIndex(pageIndex: Int) {
viewModel.durPageIndex = pageIndex
viewModel.saveRead()
} }
override fun textChapter(chapterOnDur: Int): TextChapter? { override fun textChapter(chapterOnDur: Int): TextChapter? {

@ -4,6 +4,10 @@ import io.legado.app.data.entities.BookChapter
interface DataSource { interface DataSource {
fun pageIndex(): Int
fun setPageIndex(pageIndex: Int)
fun isPrepared(): Boolean fun isPrepared(): Boolean
fun getChapterPosition(): Int fun getChapterPosition(): Int

@ -33,6 +33,14 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att
pageDelegate = SlidePageDelegate(this) pageDelegate = SlidePageDelegate(this)
setPageFactory(TextPageFactory.create(object : DataSource { setPageFactory(TextPageFactory.create(object : DataSource {
override fun pageIndex(): Int {
return callback?.durChapterPos() ?: 0
}
override fun setPageIndex(pageIndex: Int) {
callback?.setPageIndex(pageIndex)
}
override fun isPrepared(): Boolean { override fun isPrepared(): Boolean {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates. TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
} }
@ -107,25 +115,25 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att
when (chapterOnDur) { when (chapterOnDur) {
0 -> { 0 -> {
cb.textChapter()?.let { cb.textChapter()?.let {
curPage?.setContent(it.page(cb.durChapterPos(it.pageSize()))) curPage?.setContent(it.page(cb.durChapterPos()))
if (cb.durChapterPos(it.pageSize()) > 0) { if (cb.durChapterPos() > 0) {
prevPage?.setContent(it.page(cb.durChapterPos(it.pageSize()) - 1)) prevPage?.setContent(it.page(cb.durChapterPos().minus(1)))
} }
if (cb.durChapterPos(it.pageSize()) < it.pageSize() - 1) { if (cb.durChapterPos() < it.pageSize().minus(1)) {
nextPage?.setContent(it.page(cb.durChapterPos(it.pageSize()) + 1)) nextPage?.setContent(it.page(cb.durChapterPos().plus(1)))
} }
} }
} }
1 -> { 1 -> {
cb.textChapter()?.let { cb.textChapter()?.let {
if (cb.durChapterPos(it.pageSize()) == it.pageSize() - 1) { if (cb.durChapterPos() == it.pageSize().minus(1)) {
nextPage?.setContent(cb.textChapter(1)?.page(0)) nextPage?.setContent(cb.textChapter(1)?.page(0))
} }
} }
} }
-1 -> { -1 -> {
cb.textChapter()?.let { cb.textChapter()?.let {
if (cb.durChapterPos(it.pageSize()) == 0) { if (cb.durChapterPos() == 0) {
prevPage?.setContent(cb.textChapter(-1)?.lastPage()) prevPage?.setContent(cb.textChapter(-1)?.lastPage())
} }
} }
@ -206,10 +214,11 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att
interface CallBack { interface CallBack {
fun chapterSize(): Int fun chapterSize(): Int
fun durChapterIndex(): Int fun durChapterIndex(): Int
fun durChapterPos(pageSize: Int): Int fun durChapterPos(): Int
fun textChapter(chapterOnDur: Int = 0): TextChapter? fun textChapter(chapterOnDur: Int = 0): TextChapter?
fun loadChapter(index: Int) fun loadChapter(index: Int)
fun moveToNextChapter() fun moveToNextChapter()
fun moveToPrevChapter() fun moveToPrevChapter()
fun setPageIndex(pageIndex: Int)
} }
} }

@ -9,8 +9,6 @@ class TextPageFactory private constructor(dataSource: DataSource) :
} }
} }
var index: Int = 0
override fun hasPrev(): Boolean { override fun hasPrev(): Boolean {
return true return true
} }
@ -24,51 +22,51 @@ class TextPageFactory private constructor(dataSource: DataSource) :
} }
override fun moveToFirst() { override fun moveToFirst() {
index = 0 dataSource.setPageIndex(0)
} }
override fun moveToLast() { override fun moveToLast() {
index = dataSource.getCurrentChapter()?.let { dataSource.getCurrentChapter()?.let {
if (it.pageSize() == 0) { if (it.pageSize() == 0) {
0 dataSource.setPageIndex(0)
} else { } else {
it.pageSize() - 1 dataSource.setPageIndex(it.pageSize().minus(1))
} }
} ?: 0 } ?: dataSource.setPageIndex(0)
} }
override fun moveToNext(): Boolean { override fun moveToNext(): Boolean = dataSource.pageIndex().let { index ->
return if (hasNext()) { return if (hasNext()) {
index = if (dataSource.getCurrentChapter()?.isLastIndex(index) == true) { if (dataSource.getCurrentChapter()?.isLastIndex(index) == true) {
dataSource.moveToNextChapter() dataSource.moveToNextChapter()
0 dataSource.setPageIndex(0)
} else { } else {
index.plus(1) dataSource.setPageIndex(index.plus(1))
} }
true true
} else } else
false false
} }
override fun moveToPrevious(): Boolean { override fun moveToPrevious(): Boolean = dataSource.pageIndex().let { index ->
return if (hasPrev()) { return if (hasPrev()) {
index = if (index > 0) { if (index > 0) {
index.minus(1) dataSource.setPageIndex(index.minus(1))
} else { } else {
dataSource.moveToPrevChapter() dataSource.moveToPrevChapter()
dataSource.getPreviousChapter()?.lastIndex() ?: 0 dataSource.setPageIndex(dataSource.getPreviousChapter()?.lastIndex() ?: 0)
} }
true true
} else } else
false false
} }
override fun currentPage(): TextPage? { override fun currentPage(): TextPage? = dataSource.pageIndex().let { index ->
return dataSource.getCurrentChapter()?.page(index) return dataSource.getCurrentChapter()?.page(index)
?: TextPage(index, "index:$index", "index:$index") ?: TextPage(index, "index:$index", "index:$index")
} }
override fun nextPage(): TextPage? { override fun nextPage(): TextPage? = dataSource.pageIndex().let { index ->
dataSource.getCurrentChapter()?.let { dataSource.getCurrentChapter()?.let {
if (index < it.pageSize() - 1) { if (index < it.pageSize() - 1) {
return dataSource.getCurrentChapter()?.page(index + 1) return dataSource.getCurrentChapter()?.page(index + 1)
@ -79,7 +77,7 @@ class TextPageFactory private constructor(dataSource: DataSource) :
?: TextPage(index + 1, "index:${index + 1}", "index:${index + 1}") ?: TextPage(index + 1, "index:${index + 1}", "index:${index + 1}")
} }
override fun previousPage(): TextPage? { override fun previousPage(): TextPage? = dataSource.pageIndex().let { index ->
if (index > 0) { 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}")

Loading…
Cancel
Save