feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 284fe6ce56
commit 9c15e9401c
  1. 2
      app/src/main/java/io/legado/app/service/help/ReadBook.kt
  2. 4
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  3. 8
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  4. 11
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  5. 1
      app/src/main/java/io/legado/app/ui/book/read/page/DataSource.kt
  6. 4
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt

@ -312,7 +312,7 @@ object ReadBook {
} }
interface CallBack { interface CallBack {
fun upContent(relativePosition: Int = 0, resetScrollOffset: Boolean = true) fun upContent(relativePosition: Int = 0)
fun upView() fun upView()
fun upPageProgress() fun upPageProgress()
fun contentLoadFinish() fun contentLoadFinish()

@ -456,9 +456,9 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
/** /**
* 更新内容 * 更新内容
*/ */
override fun upContent(relativePosition: Int, resetScrollOffset: Boolean) { override fun upContent(relativePosition: Int) {
launch { launch {
page_view.upContent(relativePosition, resetScrollOffset) page_view.upContent(relativePosition)
} }
} }

@ -17,6 +17,8 @@ import io.legado.app.utils.getPrefBoolean
class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, attrs) { class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
var selectAble = context.getPrefBoolean(PreferKey.textSelectAble)
var upView: ((TextPage) -> Unit)? = null
private val selectedPaint by lazy { private val selectedPaint by lazy {
Paint().apply { Paint().apply {
color = context.getCompatColor(R.color.btn_bg_press_2) color = context.getCompatColor(R.color.btn_bg_press_2)
@ -24,7 +26,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
private var callBack: CallBack private var callBack: CallBack
var selectAble = context.getPrefBoolean(PreferKey.textSelectAble)
private var selectLineStart = 0 private var selectLineStart = 0
private var selectCharStart = 0 private var selectCharStart = 0
private var selectLineEnd = 0 private var selectLineEnd = 0
@ -159,12 +160,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
pageFactory.moveToPrev() pageFactory.moveToPrev()
textPage = pageFactory.currentPage ?: TextPage().format() textPage = pageFactory.currentPage ?: TextPage().format()
pageOffset -= textPage.height pageOffset -= textPage.height
callBack.upContent(0, false) upView?.invoke(textPage)
} else if (pageOffset < -textPage.height) { } else if (pageOffset < -textPage.height) {
pageOffset += textPage.height pageOffset += textPage.height
pageFactory.moveToNext() pageFactory.moveToNext()
textPage = pageFactory.currentPage ?: TextPage().format() textPage = pageFactory.currentPage ?: TextPage().format()
callBack.upContent(0, false) upView?.invoke(textPage)
} }
invalidate() invalidate()
} }
@ -302,7 +303,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
interface CallBack { interface CallBack {
fun upContent(relativePosition: Int, resetScrollOffset: Boolean)
fun upSelectedStart(x: Float, y: Float) fun upSelectedStart(x: Float, y: Float)
fun upSelectedEnd(x: Float, y: Float) fun upSelectedEnd(x: Float, y: Float)
fun onCancelSelect() fun onCancelSelect()

@ -25,6 +25,11 @@ class ContentView(context: Context) : FrameLayout(context) {
upStyle() upStyle()
upTime() upTime()
content_text_view.upView = {
tv_bottom_left.text = it.title
pageSize = it.pageSize
setPageIndex(it.index)
}
} }
fun upStyle() { fun upStyle() {
@ -91,17 +96,13 @@ class ContentView(context: Context) : FrameLayout(context) {
tv_top_right.text = context.getString(R.string.battery_show, battery) tv_top_right.text = context.getString(R.string.battery_show, battery)
} }
fun setContent(textPage: TextPage?, resetScrollOffset: Boolean = true) { fun setContent(textPage: TextPage?) {
if (textPage != null) { if (textPage != null) {
tv_bottom_left.text = textPage.title tv_bottom_left.text = textPage.title
pageSize = textPage.pageSize pageSize = textPage.pageSize
setPageIndex(textPage.index) setPageIndex(textPage.index)
if (resetScrollOffset) {
content_text_view.resetPageOffset() content_text_view.resetPageOffset()
content_text_view.setContent(textPage) content_text_view.setContent(textPage)
} else {
content_text_view.invalidate()
}
} }
} }

@ -16,5 +16,4 @@ interface DataSource {
fun hasNextChapter(): Boolean fun hasNextChapter(): Boolean
fun hasPrevChapter(): Boolean fun hasPrevChapter(): Boolean
} }

@ -98,9 +98,9 @@ class PageView(context: Context, attrs: AttributeSet) :
upContent() upContent()
} }
fun upContent(relativePosition: Int = 0, resetScrollOffset: Boolean = true) { fun upContent(relativePosition: Int = 0) {
if (ReadBookConfig.isScroll) { if (ReadBookConfig.isScroll) {
curPage.setContent(pageFactory.currentPage, resetScrollOffset) curPage.setContent(pageFactory.currentPage)
} else { } else {
when (relativePosition) { when (relativePosition) {
-1 -> prevPage.setContent(pageFactory.prevPage) -1 -> prevPage.setContent(pageFactory.prevPage)

Loading…
Cancel
Save