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.ChapterProvider
import io.legado.app.ui.book.read.page.ContentTextView 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.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.read.page.delegate.PageDelegate
import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.book.source.edit.BookSourceEditActivity
import io.legado.app.ui.changesource.ChangeSourceDialog 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 screenTimeOut: Long = 0
private var timeElectricityReceiver: TimeElectricityReceiver? = null private var timeElectricityReceiver: TimeElectricityReceiver? = null
override val pageFactory: TextPageFactory get() = page_view.pageFactory
override val headerHeight: Int get() = page_view.curPage.headerHeight override val headerHeight: Int get() = page_view.curPage.headerHeight
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {

@ -11,7 +11,6 @@ import io.legado.app.R
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.help.ReadBookConfig import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.theme.accentColor 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.ui.book.read.page.entities.TextPage
import io.legado.app.utils.activity import io.legado.app.utils.activity
import io.legado.app.utils.getCompatColor import io.legado.app.utils.getCompatColor
@ -25,7 +24,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
style = Paint.Style.FILL style = Paint.Style.FILL
} }
} }
private var activityCallBack: CallBack private var callBack: CallBack
var selectAble = context.getPrefBoolean(PreferKey.textSelectAble) var selectAble = context.getPrefBoolean(PreferKey.textSelectAble)
private var selectLineStart = 0 private var selectLineStart = 0
private var selectCharStart = 0 private var selectCharStart = 0
@ -33,13 +32,14 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private var selectCharEnd = 0 private var selectCharEnd = 0
private var textPage: TextPage = TextPage() private var textPage: TextPage = TextPage()
//滚动参数 //滚动参数
private val pageFactory: TextPageFactory get() = callBack.pageFactory
private val maxScrollOffset = 100f private val maxScrollOffset = 100f
private var pageOffset = 0f private var pageOffset = 0f
private var linePos = 0 private var linePos = 0
private var isLastPage = false private var isLastPage = false
init { init {
activityCallBack = activity as CallBack callBack = activity as CallBack
contentDescription = textPage.text contentDescription = textPage.text
} }
@ -127,7 +127,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
isLastPage = false isLastPage = false
} }
// 首页 // 首页
if (pageOffset < 0 && ReadBook.durChapterIndex == 0 && ReadBook.durPageIndex == 0) { if (pageOffset < 0 && !pageFactory.hasPrev()) {
pageOffset = 0f pageOffset = 0f
} }
@ -245,11 +245,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
private fun upSelectedStart(x: Float, y: Float) { 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) { private fun upSelectedEnd(x: Float, y: Float) {
activityCallBack.upSelectedEnd(x, y + activityCallBack.headerHeight) callBack.upSelectedEnd(x, y + callBack.headerHeight)
} }
fun cancelSelect() { fun cancelSelect() {
@ -259,7 +259,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
invalidate() invalidate()
activityCallBack.onCancelSelect() callBack.onCancelSelect()
} }
val selectedText: String val selectedText: String
@ -295,5 +295,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
fun upSelectedEnd(x: Float, y: Float) fun upSelectedEnd(x: Float, y: Float)
fun onCancelSelect() fun onCancelSelect()
val headerHeight: Int 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) { class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource) {
override fun hasPrev(): Boolean = with(dataSource) { override fun hasPrev(): Boolean = with(dataSource) {
return if (isScrollDelegate) { return hasPrevChapter() || pageIndex > 0
hasPrevChapter()
} else {
hasPrevChapter() || pageIndex > 0
}
} }
override fun hasNext(): Boolean = with(dataSource) { override fun hasNext(): Boolean = with(dataSource) {
return if (isScrollDelegate) { return hasNextChapter() || getCurrentChapter()?.isLastIndex(pageIndex) != true
hasNextChapter()
} else {
hasNextChapter()
|| getCurrentChapter()?.isLastIndex(pageIndex) != true
}
} }
override fun moveToFirst() { override fun moveToFirst() {
@ -39,7 +30,6 @@ 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
|| isScrollDelegate
) { ) {
ReadBook.moveToNextChapter(false) ReadBook.moveToNextChapter(false)
} else { } else {
@ -52,7 +42,7 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
override fun moveToPrevious(): Boolean = with(dataSource) { override fun moveToPrevious(): Boolean = with(dataSource) {
return if (hasPrev()) { return if (hasPrev()) {
if (pageIndex <= 0 || isScrollDelegate) { if (pageIndex <= 0) {
ReadBook.moveToPrevChapter(false) ReadBook.moveToPrevChapter(false)
} else { } else {
setPageIndex(pageIndex.minus(1)) setPageIndex(pageIndex.minus(1))
@ -63,17 +53,10 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
} }
override fun currentPage(): TextPage? = with(dataSource) { override fun currentPage(): TextPage? = with(dataSource) {
return if (isScrollDelegate) { return getCurrentChapter()?.page(pageIndex)
getCurrentChapter()?.scrollPage()
} else {
getCurrentChapter()?.page(pageIndex)
}
} }
override fun nextPage(): TextPage? = with(dataSource) { override fun nextPage(): TextPage? = with(dataSource) {
if (isScrollDelegate) {
return getNextChapter()?.scrollPage()
}
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()
@ -83,9 +66,6 @@ class TextPageFactory(dataSource: DataSource) : PageFactory<TextPage>(dataSource
} }
override fun previousPage(): TextPage? = with(dataSource) { override fun previousPage(): TextPage? = with(dataSource) {
if (isScrollDelegate) {
return getPreviousChapter()?.scrollPage()
}
if (pageIndex > 0) { if (pageIndex > 0) {
return getCurrentChapter()?.page(pageIndex - 1)?.removePageAloudSpan() return getCurrentChapter()?.page(pageIndex - 1)?.removePageAloudSpan()
} }

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

Loading…
Cancel
Save