feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 56910ffbc3
commit b924cff588
  1. 3
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 15
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 28
      app/src/main/java/io/legado/app/ui/book/read/page/TextPageFactory.kt
  4. 7
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/PageDelegate.kt

@ -36,6 +36,7 @@ import io.legado.app.ui.book.read.config.BgTextConfigDialog.Companion.TEXT_COLOR
import io.legado.app.ui.book.read.page.ChapterProvider
import io.legado.app.ui.book.read.page.ContentTextView
import io.legado.app.ui.book.read.page.PageView
import io.legado.app.ui.book.read.page.TextPageFactory
import io.legado.app.ui.book.read.page.delegate.PageDelegate
import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.changesource.ChangeSourceDialog
@ -80,7 +81,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
private var screenTimeOut: Long = 0
private var timeElectricityReceiver: TimeElectricityReceiver? = null
override val pageFactory: TextPageFactory get() = page_view.pageFactory
override val headerHeight: Int get() = page_view.curPage.headerHeight
override fun onActivityCreated(savedInstanceState: Bundle?) {

@ -11,7 +11,6 @@ import io.legado.app.R
import io.legado.app.constant.PreferKey
import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.accentColor
import io.legado.app.service.help.ReadBook
import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.utils.activity
import io.legado.app.utils.getCompatColor
@ -25,7 +24,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
style = Paint.Style.FILL
}
}
private var activityCallBack: CallBack
private var callBack: CallBack
var selectAble = context.getPrefBoolean(PreferKey.textSelectAble)
private var selectLineStart = 0
private var selectCharStart = 0
@ -33,13 +32,14 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private var selectCharEnd = 0
private var textPage: TextPage = TextPage()
//滚动参数
private val pageFactory: TextPageFactory get() = callBack.pageFactory
private val maxScrollOffset = 100f
private var pageOffset = 0f
private var linePos = 0
private var isLastPage = false
init {
activityCallBack = activity as CallBack
callBack = activity as CallBack
contentDescription = textPage.text
}
@ -127,7 +127,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
isLastPage = false
}
// 首页
if (pageOffset < 0 && ReadBook.durChapterIndex == 0 && ReadBook.durPageIndex == 0) {
if (pageOffset < 0 && !pageFactory.hasPrev()) {
pageOffset = 0f
}
@ -245,11 +245,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
}
private fun upSelectedStart(x: Float, y: Float) {
activityCallBack.upSelectedStart(x, y + activityCallBack.headerHeight)
callBack.upSelectedStart(x, y + callBack.headerHeight)
}
private fun upSelectedEnd(x: Float, y: Float) {
activityCallBack.upSelectedEnd(x, y + activityCallBack.headerHeight)
callBack.upSelectedEnd(x, y + callBack.headerHeight)
}
fun cancelSelect() {
@ -259,7 +259,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
}
}
invalidate()
activityCallBack.onCancelSelect()
callBack.onCancelSelect()
}
val selectedText: String
@ -295,5 +295,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
fun upSelectedEnd(x: Float, y: Float)
fun onCancelSelect()
val headerHeight: Int
val pageFactory: TextPageFactory
}
}

@ -6,20 +6,11 @@ import io.legado.app.ui.book.read.page.entities.TextPage
class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource) {
override fun hasPrev(): Boolean = with(dataSource) {
return if (isScrollDelegate) {
hasPrevChapter()
} else {
hasPrevChapter() || pageIndex > 0
}
return hasPrevChapter() || pageIndex > 0
}
override fun hasNext(): Boolean = with(dataSource) {
return if (isScrollDelegate) {
hasNextChapter()
} else {
hasNextChapter()
|| getCurrentChapter()?.isLastIndex(pageIndex) != true
}
return hasNextChapter() || getCurrentChapter()?.isLastIndex(pageIndex) != true
}
override fun moveToFirst() {
@ -39,7 +30,6 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
override fun moveToNext(): Boolean = with(dataSource) {
return if (hasNext()) {
if (getCurrentChapter()?.isLastIndex(pageIndex) == true
|| isScrollDelegate
) {
ReadBook.moveToNextChapter(false)
} else {
@ -52,7 +42,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
override fun moveToPrevious(): Boolean = with(dataSource) {
return if (hasPrev()) {
if (pageIndex <= 0 || isScrollDelegate) {
if (pageIndex <= 0) {
ReadBook.moveToPrevChapter(false)
} else {
setPageIndex(pageIndex.minus(1))
@ -63,17 +53,10 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
}
override fun currentPage(): TextPage? = with(dataSource) {
return if (isScrollDelegate) {
getCurrentChapter()?.scrollPage()
} else {
getCurrentChapter()?.page(pageIndex)
}
return getCurrentChapter()?.page(pageIndex)
}
override fun nextPage(): TextPage? = with(dataSource) {
if (isScrollDelegate) {
return getNextChapter()?.scrollPage()
}
getCurrentChapter()?.let {
if (pageIndex < it.pageSize() - 1) {
return getCurrentChapter()?.page(pageIndex + 1)?.removePageAloudSpan()
@ -83,9 +66,6 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
}
override fun previousPage(): TextPage? = with(dataSource) {
if (isScrollDelegate) {
return getPreviousChapter()?.scrollPage()
}
if (pageIndex > 0) {
return getCurrentChapter()?.page(pageIndex - 1)?.removePageAloudSpan()
}

@ -134,15 +134,8 @@ abstract class PageDelegate(protected val pageView: PageView) :
isRunning = false
isStarted = false
invalidate()
if (pageView.isScrollDelegate) {
pageView.postDelayed({
bitmap?.recycle()
bitmap = null
}, 100)
} else {
bitmap?.recycle()
bitmap = null
}
}
fun setViewSize(width: Int, height: Int) {

Loading…
Cancel
Save