From e16cb8df0a054f26f0cae04df3f930edcf353028 Mon Sep 17 00:00:00 2001 From: kunfei Date: Mon, 23 Sep 2019 09:41:40 +0800 Subject: [PATCH] up --- .../app/ui/book/read/ReadBookActivity.kt | 20 +++++--- .../legado/app/ui/widget/page/DataSource.kt | 4 +- .../io/legado/app/ui/widget/page/PageView.kt | 36 +++++++++----- .../app/ui/widget/page/TextPageFactory.kt | 48 +++++++++---------- .../ui/widget/page/delegate/PageDelegate.kt | 4 +- 5 files changed, 65 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index b1d735117..dbcc05184 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -582,16 +582,24 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_rea observeEvent(Bus.TTS_TURN_PAGE) { when (it) { 1 -> { - viewModel.durPageIndex = viewModel.durPageIndex + 1 - page_view.upContent() - viewModel.saveRead() + if (page_view.isScrollDelegate) { + page_view.moveToNextPage() + } else { + viewModel.durPageIndex = viewModel.durPageIndex + 1 + page_view.upContent() + viewModel.saveRead() + } } 2 -> if (!moveToNextChapter(true)) ReadAloud.stop(this) -1 -> { if (viewModel.durPageIndex > 0) { - viewModel.durPageIndex = viewModel.durPageIndex - 1 - page_view.upContent() - viewModel.saveRead() + if (page_view.isScrollDelegate) { + page_view.moveToPrevPage() + } else { + viewModel.durPageIndex = viewModel.durPageIndex - 1 + page_view.upContent() + viewModel.saveRead() + } } else { moveToPrevChapter(true) } 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 fabb55a06..01c84da91 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 @@ -1,9 +1,9 @@ package io.legado.app.ui.widget.page interface DataSource { - fun isScrollDelegate(): Boolean + val isScrollDelegate: Boolean - fun pageIndex(): Int + val pageIndex: Int fun setPageIndex(pageIndex: 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 12b90dae2..50d6ff59c 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,14 +70,14 @@ class PageView(context: Context, attrs: AttributeSet) : PageDelegate.Direction.PREV -> { pageFactory?.moveToPrevious() upContent() - if (isScrollDelegate()) { + if (isScrollDelegate) { curPage?.scrollToBottom() } } PageDelegate.Direction.NEXT -> { pageFactory?.moveToNext() upContent() - if (isScrollDelegate()) { + if (isScrollDelegate) { curPage?.scrollTo(0) } } @@ -106,7 +106,7 @@ class PageView(context: Context, attrs: AttributeSet) : nextPage?.setContent(it.nextPage()) prevPage?.setContent(it.previousPage()) callback?.let { callback -> - if (isScrollDelegate()) { + if (isScrollDelegate) { curPage?.scrollTo(callback.textChapter()?.getStartLine(callback.durChapterPos())) } } @@ -117,13 +117,25 @@ class PageView(context: Context, attrs: AttributeSet) : fun moveToPrevPage(noAnim: Boolean = true) { if (noAnim) { - fillPage(PageDelegate.Direction.PREV) + if (isScrollDelegate) { + callback?.textChapter()?.let { + curPage?.scrollTo(it.getStartLine(pageIndex - 1)) + } + } else { + fillPage(PageDelegate.Direction.PREV) + } } } fun moveToNextPage(noAnim: Boolean = true) { if (noAnim) { - fillPage(PageDelegate.Direction.NEXT) + if (isScrollDelegate) { + callback?.textChapter()?.let { + curPage?.scrollTo(it.getStartLine(pageIndex + 1)) + } + } else { + fillPage(PageDelegate.Direction.NEXT) + } } } @@ -166,13 +178,11 @@ class PageView(context: Context, attrs: AttributeSet) : callback?.clickCenter() } - override fun isScrollDelegate(): Boolean { - return pageDelegate is ScrollPageDelegate - } + override val isScrollDelegate: Boolean + get() = pageDelegate is ScrollPageDelegate - override fun pageIndex(): Int { - return callback?.durChapterPos() ?: 0 - } + override val pageIndex: Int + get() = callback?.durChapterPos() ?: 0 override fun setPageIndex(pageIndex: Int) { callback?.setPageIndex(pageIndex) @@ -221,7 +231,7 @@ class PageView(context: Context, attrs: AttributeSet) : } override fun scrollToLine(line: Int) { - if (isScrollDelegate()) { + if (isScrollDelegate) { callback?.textChapter()?.let { val pageIndex = it.getPageIndex(line) curPage?.setPageIndex(pageIndex) @@ -231,7 +241,7 @@ class PageView(context: Context, attrs: AttributeSet) : } override fun scrollToLast() { - if (isScrollDelegate()) { + if (isScrollDelegate) { callback?.textChapter()?.let { callback?.setPageIndex(it.lastIndex()) } 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 cfadbf7aa..544049bdc 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 @@ -3,19 +3,19 @@ package io.legado.app.ui.widget.page class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource) { override fun hasPrev(): Boolean = with(dataSource) { - return if (isScrollDelegate()) { + return if (isScrollDelegate) { hasPrevChapter() } else { - hasPrevChapter() || pageIndex() > 0 + hasPrevChapter() || pageIndex > 0 } } override fun hasNext(): Boolean = with(dataSource) { - return if (isScrollDelegate()) { + return if (isScrollDelegate) { hasNextChapter() } else { hasNextChapter() - || getCurrentChapter()?.isLastIndex(pageIndex()) != true + || getCurrentChapter()?.isLastIndex(pageIndex) != true } } @@ -40,12 +40,12 @@ class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource override fun moveToNext(): Boolean = with(dataSource) { return if (hasNext()) { - if (getCurrentChapter()?.isLastIndex(pageIndex()) == true - || isScrollDelegate() + if (getCurrentChapter()?.isLastIndex(pageIndex) == true + || isScrollDelegate ) { moveToNextChapter() } else { - setPageIndex(pageIndex().plus(1)) + setPageIndex(pageIndex.plus(1)) } true } else @@ -54,10 +54,10 @@ class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource override fun moveToPrevious(): Boolean = with(dataSource) { return if (hasPrev()) { - if (pageIndex() <= 0 || isScrollDelegate()) { + if (pageIndex <= 0 || isScrollDelegate) { moveToPrevChapter() } else { - setPageIndex(pageIndex().minus(1)) + setPageIndex(pageIndex.minus(1)) } true } else @@ -65,39 +65,39 @@ class TextPageFactory(dataSource: DataSource) : PageFactory(dataSource } override fun currentPage(): TextPage? = with(dataSource) { - return if (isScrollDelegate()) { + return if (isScrollDelegate) { getCurrentChapter()?.scrollPage() } else { - getCurrentChapter()?.page(pageIndex()) - } ?: TextPage(index = pageIndex(), title = "index:${pageIndex()}") + getCurrentChapter()?.page(pageIndex) + } ?: TextPage(index = pageIndex, title = "index:${pageIndex}") } override fun nextPage(): TextPage? = with(dataSource) { - if (isScrollDelegate()) { + if (isScrollDelegate) { return getNextChapter()?.scrollPage() - ?: TextPage(index = pageIndex() + 1, title = "index:${pageIndex() + 1}") + ?: TextPage(index = pageIndex + 1, title = "index:${pageIndex + 1}") } getCurrentChapter()?.let { - if (pageIndex() < it.pageSize() - 1) { - return getCurrentChapter()?.page(pageIndex() + 1)?.removePageAloudSpan() - ?: TextPage(index = pageIndex() + 1, title = "index:${pageIndex() + 1}") + if (pageIndex < it.pageSize() - 1) { + return getCurrentChapter()?.page(pageIndex + 1)?.removePageAloudSpan() + ?: TextPage(index = pageIndex + 1, title = "index:${pageIndex + 1}") } } return getNextChapter()?.page(0)?.removePageAloudSpan() - ?: TextPage(index = pageIndex() + 1, title = "index:${pageIndex() + 1}") + ?: TextPage(index = pageIndex + 1, title = "index:${pageIndex + 1}") } override fun previousPage(): TextPage? = with(dataSource) { - if (isScrollDelegate()) { + if (isScrollDelegate) { return getPreviousChapter()?.scrollPage() - ?: TextPage(index = pageIndex() + 1, title = "index:${pageIndex() + 1}") + ?: TextPage(index = pageIndex + 1, title = "index:${pageIndex + 1}") } - if (pageIndex() > 0) { - return getCurrentChapter()?.page(pageIndex() - 1)?.removePageAloudSpan() - ?: TextPage(index = pageIndex() - 1, title = "index:${pageIndex() - 1}") + if (pageIndex > 0) { + return getCurrentChapter()?.page(pageIndex - 1)?.removePageAloudSpan() + ?: TextPage(index = pageIndex - 1, title = "index:${pageIndex - 1}") } return getPreviousChapter()?.lastPage()?.removePageAloudSpan() - ?: TextPage(index = pageIndex() - 1, title = "index:${pageIndex() - 1}") + ?: TextPage(index = pageIndex - 1, title = "index:${pageIndex - 1}") } diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt index 9ce140145..deae2f920 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt @@ -270,7 +270,7 @@ abstract class PageDelegate(protected val pageView: PageView) { distanceX: Float, distanceY: Float ): Boolean { - if (pageView.isScrollDelegate()) { + if (pageView.isScrollDelegate) { if (!isMoved && abs(distanceX) < abs(distanceY)) { if (distanceY < 0) { if (atTop) { @@ -339,7 +339,7 @@ abstract class PageDelegate(protected val pageView: PageView) { isMoved = true } if (isMoved) { - isCancel = if (pageView.isScrollDelegate()) { + isCancel = if (pageView.isScrollDelegate) { if (direction == Direction.NEXT) distanceY < 0 else distanceY > 0 } else { if (direction == Direction.NEXT) distanceX < 0 else distanceX > 0