From f06bdf0fe557e7d86aae9ca23c1f8d2747da84c3 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 24 Aug 2019 21:43:37 +0800 Subject: [PATCH] up --- .../app/ui/readbook/ReadBookActivity.kt | 16 ++++++--- .../legado/app/ui/widget/page/DataSource.kt | 4 +++ .../io/legado/app/ui/widget/page/PageView.kt | 25 +++++++++----- .../app/ui/widget/page/TextPageFactory.kt | 34 +++++++++---------- 4 files changed, 49 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt index dfbe83e85..75d622e8d 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookActivity.kt @@ -249,11 +249,19 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea return viewModel.durChapterIndex } - override fun durChapterPos(pageSize: Int): Int { - if (viewModel.durPageIndex < pageSize) { - return viewModel.durPageIndex + override fun durChapterPos(): Int { + viewModel.curTextChapter?.let { + 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? { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt b/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt index ce3974b01..8ba58b6ba 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt @@ -4,6 +4,10 @@ import io.legado.app.data.entities.BookChapter interface DataSource { + fun pageIndex(): Int + + fun setPageIndex(pageIndex: Int) + fun isPrepared(): Boolean fun getChapterPosition(): Int diff --git a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt index 2a78d0cdd..1744020d8 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt @@ -33,6 +33,14 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att pageDelegate = SlidePageDelegate(this) 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 { 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) { 0 -> { cb.textChapter()?.let { - curPage?.setContent(it.page(cb.durChapterPos(it.pageSize()))) - if (cb.durChapterPos(it.pageSize()) > 0) { - prevPage?.setContent(it.page(cb.durChapterPos(it.pageSize()) - 1)) + curPage?.setContent(it.page(cb.durChapterPos())) + if (cb.durChapterPos() > 0) { + prevPage?.setContent(it.page(cb.durChapterPos().minus(1))) } - if (cb.durChapterPos(it.pageSize()) < it.pageSize() - 1) { - nextPage?.setContent(it.page(cb.durChapterPos(it.pageSize()) + 1)) + if (cb.durChapterPos() < it.pageSize().minus(1)) { + nextPage?.setContent(it.page(cb.durChapterPos().plus(1))) } } } 1 -> { 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)) } } } -1 -> { cb.textChapter()?.let { - if (cb.durChapterPos(it.pageSize()) == 0) { + if (cb.durChapterPos() == 0) { prevPage?.setContent(cb.textChapter(-1)?.lastPage()) } } @@ -206,10 +214,11 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att interface CallBack { fun chapterSize(): Int fun durChapterIndex(): Int - fun durChapterPos(pageSize: Int): Int + fun durChapterPos(): Int fun textChapter(chapterOnDur: Int = 0): TextChapter? fun loadChapter(index: Int) fun moveToNextChapter() fun moveToPrevChapter() + fun setPageIndex(pageIndex: Int) } } diff --git a/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt b/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt index e19cd2487..0cc47789e 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt @@ -9,8 +9,6 @@ class TextPageFactory private constructor(dataSource: DataSource) : } } - var index: Int = 0 - override fun hasPrev(): Boolean { return true } @@ -24,51 +22,51 @@ class TextPageFactory private constructor(dataSource: DataSource) : } override fun moveToFirst() { - index = 0 + dataSource.setPageIndex(0) } override fun moveToLast() { - index = dataSource.getCurrentChapter()?.let { + dataSource.getCurrentChapter()?.let { if (it.pageSize() == 0) { - 0 + dataSource.setPageIndex(0) } 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()) { - index = if (dataSource.getCurrentChapter()?.isLastIndex(index) == true) { + if (dataSource.getCurrentChapter()?.isLastIndex(index) == true) { dataSource.moveToNextChapter() - 0 + dataSource.setPageIndex(0) } else { - index.plus(1) + dataSource.setPageIndex(index.plus(1)) } true } else false } - override fun moveToPrevious(): Boolean { + override fun moveToPrevious(): Boolean = dataSource.pageIndex().let { index -> return if (hasPrev()) { - index = if (index > 0) { - index.minus(1) + if (index > 0) { + dataSource.setPageIndex(index.minus(1)) } else { dataSource.moveToPrevChapter() - dataSource.getPreviousChapter()?.lastIndex() ?: 0 + dataSource.setPageIndex(dataSource.getPreviousChapter()?.lastIndex() ?: 0) } true } else false } - override fun currentPage(): TextPage? { + override fun currentPage(): TextPage? = dataSource.pageIndex().let { index -> return dataSource.getCurrentChapter()?.page(index) ?: TextPage(index, "index:$index", "index:$index") } - override fun nextPage(): TextPage? { + override fun nextPage(): TextPage? = dataSource.pageIndex().let { index -> dataSource.getCurrentChapter()?.let { if (index < it.pageSize() - 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}") } - override fun previousPage(): TextPage? { + override fun previousPage(): TextPage? = dataSource.pageIndex().let { index -> if (index > 0) { return dataSource.getCurrentChapter()?.page(index - 1) ?: TextPage(index - 1, "index:${index - 1}", "index:${index - 1}")