From 7d516587b3d282552dccba636accb1c96cb3634e Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 24 Aug 2019 13:45:15 +0800 Subject: [PATCH] up --- .../app/ui/readbook/ReadBookActivity.kt | 8 +++++ .../app/ui/readbook/ReadBookViewModel.kt | 18 ++++++++++++ .../legado/app/ui/widget/page/DataSource.kt | 4 +++ .../io/legado/app/ui/widget/page/PageView.kt | 10 +++++++ .../legado/app/ui/widget/page/TextChapter.kt | 8 +++++ .../app/ui/widget/page/TextPageFactory.kt | 29 ++++++++++++++++--- 6 files changed, 73 insertions(+), 4 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 941b6ec3f..1a1b5c885 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 @@ -256,6 +256,14 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea } } + override fun moveToNextChapter() { + viewModel.moveToNextChapter() + } + + override fun moveToPrevChapter() { + viewModel.moveToPrevChapter() + } + private fun onClickReadAloud() { if (!ReadAloudService.isRun) { readAloudStatus = Status.STOP diff --git a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt index b69f3c3d8..dedb9b702 100644 --- a/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/readbook/ReadBookViewModel.kt @@ -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() { super.onCleared() ReadAloudService.stop(context) 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 2101ed952..ce3974b01 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 @@ -20,6 +20,10 @@ interface DataSource { fun hasPrevChapter(): Boolean + fun moveToNextChapter() + + fun moveToPrevChapter() + interface CallBack { fun onLoadFinish(bookChapter: BookChapter, content: String) } 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 2850b32e3..2a78d0cdd 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 @@ -70,6 +70,14 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att } 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 textChapter(chapterOnDur: Int = 0): TextChapter? fun loadChapter(index: Int) + fun moveToNextChapter() + fun moveToPrevChapter() } } diff --git a/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt b/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt index 8f913d92c..54295f149 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/TextChapter.kt @@ -20,6 +20,14 @@ data class TextChapter( return null } + fun lastIndex(): Int { + return pages.size - 1 + } + + fun isLastIndex(index: Int): Boolean { + return index == pages.size - 1 + } + fun pageSize(): Int { return pages.size } 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 4d66ed307..e19cd2487 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 @@ -39,7 +39,12 @@ class TextPageFactory private constructor(dataSource: DataSource) : override fun moveToNext(): Boolean { return if (hasNext()) { - index = index.plus(1) + index = if (dataSource.getCurrentChapter()?.isLastIndex(index) == true) { + dataSource.moveToNextChapter() + 0 + } else { + index.plus(1) + } true } else false @@ -47,7 +52,12 @@ class TextPageFactory private constructor(dataSource: DataSource) : override fun moveToPrevious(): Boolean { return if (hasPrev()) { - index = index.minus(1) + index = if (index > 0) { + index.minus(1) + } else { + dataSource.moveToPrevChapter() + dataSource.getPreviousChapter()?.lastIndex() ?: 0 + } true } else false @@ -59,12 +69,23 @@ class TextPageFactory private constructor(dataSource: DataSource) : } override fun nextPage(): TextPage? { - return dataSource.getCurrentChapter()?.page(index + 1) + dataSource.getCurrentChapter()?.let { + if (index < it.pageSize() - 1) { + return dataSource.getCurrentChapter()?.page(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? { - return dataSource.getCurrentChapter()?.page(index - 1) + if (index > 0) { + return dataSource.getCurrentChapter()?.page(index - 1) + ?: TextPage(index - 1, "index:${index - 1}", "index:${index - 1}") + } + + return dataSource.getPreviousChapter()?.lastPage() ?: TextPage(index - 1, "index:${index - 1}", "index:${index - 1}") }