feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 3c8def787d
commit 2c314a0745
  1. 7
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/read/page/PageFactory.kt
  3. 2
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  4. 27
      app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt

@ -156,6 +156,13 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
pageOffset += offset pageOffset += offset
if (pageOffset > 0) {
pageFactory.moveToPrev()
pageOffset -= textPage.height
} else if (pageOffset < -textPage.height) {
pageOffset += textPage.height
pageFactory.moveToNext()
}
invalidate() invalidate()
} }

@ -8,7 +8,7 @@ abstract class PageFactory<DATA>(protected val dataSource: DataSource) {
abstract fun moveToNext():Boolean abstract fun moveToNext():Boolean
abstract fun moveToPrevious(): Boolean abstract fun moveToPrev(): Boolean
abstract val nextPage: DATA? abstract val nextPage: DATA?

@ -74,7 +74,7 @@ class PageView(context: Context, attrs: AttributeSet) :
fun fillPage(direction: PageDelegate.Direction) { fun fillPage(direction: PageDelegate.Direction) {
when (direction) { when (direction) {
PageDelegate.Direction.PREV -> { PageDelegate.Direction.PREV -> {
pageFactory.moveToPrevious() pageFactory.moveToPrev()
upContent() upContent()
} }
PageDelegate.Direction.NEXT -> { PageDelegate.Direction.NEXT -> {

@ -29,8 +29,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
override fun moveToNext(): Boolean = with(dataSource) { override fun moveToNext(): Boolean = with(dataSource) {
return if (hasNext()) { return if (hasNext()) {
if (getCurrentChapter()?.isLastIndex(pageIndex) == true if (getCurrentChapter()?.isLastIndex(pageIndex) == true) {
) {
ReadBook.moveToNextChapter(false) ReadBook.moveToNextChapter(false)
} else { } else {
setPageIndex(pageIndex.plus(1)) setPageIndex(pageIndex.plus(1))
@ -40,7 +39,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
false false
} }
override fun moveToPrevious(): Boolean = with(dataSource) { override fun moveToPrev(): Boolean = with(dataSource) {
return if (hasPrev()) { return if (hasPrev()) {
if (pageIndex <= 0) { if (pageIndex <= 0) {
ReadBook.moveToPrevChapter(false) ReadBook.moveToPrevChapter(false)
@ -54,26 +53,26 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
override val currentPage: TextPage? override val currentPage: TextPage?
get() = with(dataSource) { get() = with(dataSource) {
return getCurrentChapter()?.page(pageIndex) return getCurrentChapter()?.page(pageIndex)
} }
override val nextPage: TextPage? override val nextPage: TextPage?
get() = with(dataSource) { get() = with(dataSource) {
getCurrentChapter()?.let { getCurrentChapter()?.let {
if (pageIndex < it.pageSize() - 1) { if (pageIndex < it.pageSize() - 1) {
return getCurrentChapter()?.page(pageIndex + 1)?.removePageAloudSpan() return getCurrentChapter()?.page(pageIndex + 1)?.removePageAloudSpan()
}
} }
return getNextChapter()?.page(0)?.removePageAloudSpan()
} }
return getNextChapter()?.page(0)?.removePageAloudSpan()
}
override val prevPage: TextPage? override val prevPage: TextPage?
get() = with(dataSource) { get() = with(dataSource) {
if (pageIndex > 0) { if (pageIndex > 0) {
return getCurrentChapter()?.page(pageIndex - 1)?.removePageAloudSpan() return getCurrentChapter()?.page(pageIndex - 1)?.removePageAloudSpan()
}
return getPreviousChapter()?.lastPage()?.removePageAloudSpan()
} }
return getPreviousChapter()?.lastPage()?.removePageAloudSpan()
}
} }

Loading…
Cancel
Save