pull/34/head
kunfei 5 years ago
parent 4cd983f331
commit e16cb8df0a
  1. 20
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 4
      app/src/main/java/io/legado/app/ui/widget/page/DataSource.kt
  3. 36
      app/src/main/java/io/legado/app/ui/widget/page/PageView.kt
  4. 48
      app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt
  5. 4
      app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt

@ -582,16 +582,24 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_rea
observeEvent<Int>(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)
}

@ -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)

@ -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())
}

@ -3,19 +3,19 @@ package io.legado.app.ui.widget.page
class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(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<TextPage>(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<TextPage>(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<TextPage>(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}")
}

@ -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

Loading…
Cancel
Save