feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 469f893ef1
commit 943e5fb76c
  1. 33
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  2. 8
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt

@ -19,8 +19,8 @@ class PageView(context: Context, attrs: AttributeSet) :
ContentView.CallBack, ContentView.CallBack,
DataSource { DataSource {
var callBack: CallBack? = null var callBack: CallBack
var pageFactory: TextPageFactory? = null var pageFactory: TextPageFactory
var pageDelegate: PageDelegate? = null var pageDelegate: PageDelegate? = null
var prevPage: ContentView var prevPage: ContentView
@ -28,7 +28,7 @@ class PageView(context: Context, attrs: AttributeSet) :
var nextPage: ContentView var nextPage: ContentView
init { init {
callBack = activity as? CallBack callBack = activity as CallBack
prevPage = ContentView(context) prevPage = ContentView(context)
addView(prevPage) addView(prevPage)
nextPage = ContentView(context) nextPage = ContentView(context)
@ -66,7 +66,7 @@ class PageView(context: Context, attrs: AttributeSet) :
@SuppressLint("ClickableViewAccessibility") @SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent): Boolean { override fun onTouchEvent(event: MotionEvent): Boolean {
callBack?.screenOffTimerStart() callBack.screenOffTimerStart()
return pageDelegate?.onTouch(event) ?: super.onTouchEvent(event) return pageDelegate?.onTouch(event) ?: super.onTouchEvent(event)
} }
@ -78,11 +78,11 @@ 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.moveToPrevious()
upContent() upContent()
} }
PageDelegate.Direction.NEXT -> { PageDelegate.Direction.NEXT -> {
pageFactory?.moveToNext() pageFactory.moveToNext()
upContent() upContent()
} }
else -> Unit else -> Unit
@ -103,7 +103,7 @@ class PageView(context: Context, attrs: AttributeSet) :
} }
fun upContent(position: Int = 0) { fun upContent(position: Int = 0) {
pageFactory?.let { pageFactory.let {
when (position) { when (position) {
-1 -> prevPage.setContent(it.previousPage()) -1 -> prevPage.setContent(it.previousPage())
1 -> nextPage.setContent(it.nextPage()) 1 -> nextPage.setContent(it.nextPage())
@ -114,7 +114,7 @@ class PageView(context: Context, attrs: AttributeSet) :
} }
} }
} }
callBack?.screenOffTimerStart() callBack.screenOffTimerStart()
} }
fun moveToPrevPage(noAnim: Boolean = true) { fun moveToPrevPage(noAnim: Boolean = true) {
@ -172,7 +172,7 @@ class PageView(context: Context, attrs: AttributeSet) :
get() = ReadBook.durChapterPos() get() = ReadBook.durChapterPos()
override fun setPageIndex(pageIndex: Int) { override fun setPageIndex(pageIndex: Int) {
callBack?.setPageIndex(pageIndex) callBack.setPageIndex(pageIndex)
} }
override fun getChapterPosition(): Int { override fun getChapterPosition(): Int {
@ -180,15 +180,15 @@ class PageView(context: Context, attrs: AttributeSet) :
} }
override fun getCurrentChapter(): TextChapter? { override fun getCurrentChapter(): TextChapter? {
return if (callBack?.isInitFinish == true) ReadBook.textChapter(0) else null return if (callBack.isInitFinish) ReadBook.textChapter(0) else null
} }
override fun getNextChapter(): TextChapter? { override fun getNextChapter(): TextChapter? {
return if (callBack?.isInitFinish == true) ReadBook.textChapter(1) else null return if (callBack.isInitFinish) ReadBook.textChapter(1) else null
} }
override fun getPreviousChapter(): TextChapter? { override fun getPreviousChapter(): TextChapter? {
return if (callBack?.isInitFinish == true) ReadBook.textChapter(-1) else null return if (callBack.isInitFinish) ReadBook.textChapter(-1) else null
} }
override fun hasNextChapter(): Boolean { override fun hasNextChapter(): Boolean {
@ -196,10 +196,7 @@ class PageView(context: Context, attrs: AttributeSet) :
} }
override fun hasPrevChapter(): Boolean { override fun hasPrevChapter(): Boolean {
callBack?.let { return ReadBook.durChapterIndex > 0
return ReadBook.durChapterIndex > 0
}
return false
} }
override fun scrollToLine(line: Int) { override fun scrollToLine(line: Int) {
@ -207,7 +204,7 @@ class PageView(context: Context, attrs: AttributeSet) :
ReadBook.textChapter()?.let { ReadBook.textChapter()?.let {
val pageIndex = it.getPageIndex(line) val pageIndex = it.getPageIndex(line)
curPage.setPageIndex(pageIndex) curPage.setPageIndex(pageIndex)
callBack?.setPageIndex(pageIndex) callBack.setPageIndex(pageIndex)
} }
} }
} }
@ -215,7 +212,7 @@ class PageView(context: Context, attrs: AttributeSet) :
override fun scrollToLast() { override fun scrollToLast() {
if (isScrollDelegate) { if (isScrollDelegate) {
ReadBook.textChapter()?.let { ReadBook.textChapter()?.let {
callBack?.setPageIndex(it.lastIndex()) callBack.setPageIndex(it.lastIndex())
curPage.setPageIndex(it.lastIndex()) curPage.setPageIndex(it.lastIndex())
} }
} }

@ -130,7 +130,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
invalidate() invalidate()
} }
protected fun stopScroll() { private fun stopScroll() {
isRunning = false isRunning = false
isStarted = false isStarted = false
invalidate() invalidate()
@ -272,7 +272,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
val x = e.x val x = e.x
val y = e.y val y = e.y
if (centerRectF.contains(x, y)) { if (centerRectF.contains(x, y)) {
pageView.callBack?.clickCenter() pageView.callBack.clickCenter()
setTouchPoint(x, y) setTouchPoint(x, y)
} else { } else {
if (x > viewWidth / 2 || if (x > viewWidth / 2 ||
@ -308,7 +308,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
* 判断是否有上一页 * 判断是否有上一页
*/ */
fun hasPrev(): Boolean { fun hasPrev(): Boolean {
val hasPrev = pageView.pageFactory?.hasPrev() == true val hasPrev = pageView.pageFactory.hasPrev()
if (!hasPrev) { if (!hasPrev) {
if (!snackBar.isShown) { if (!snackBar.isShown) {
snackBar.setText("没有上一页") snackBar.setText("没有上一页")
@ -322,7 +322,7 @@ abstract class PageDelegate(protected val pageView: PageView) :
* 判断是否有下一页 * 判断是否有下一页
*/ */
fun hasNext(): Boolean { fun hasNext(): Boolean {
val hasNext = pageView.pageFactory?.hasNext() == true val hasNext = pageView.pageFactory.hasNext()
if (!hasNext) { if (!hasNext) {
if (!snackBar.isShown) { if (!snackBar.isShown) {
snackBar.setText("没有下一页") snackBar.setText("没有下一页")

Loading…
Cancel
Save