feat: 优化代码

pull/117/head
kunfei 5 years ago
parent 4337c773ff
commit edb0335919
  1. 61
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt

@ -278,7 +278,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectLineStart = lineIndex selectLineStart = lineIndex
selectCharStart = charIndex selectCharStart = charIndex
upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset) upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset)
upSelectChars(textPage) upSelectChars()
} }
return return
} }
@ -299,7 +299,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectLineStart = lineIndex selectLineStart = lineIndex
selectCharStart = charIndex selectCharStart = charIndex
upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset) upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset)
upSelectChars(textPage) upSelectChars()
} }
return return
} }
@ -318,7 +318,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectLineStart = lineIndex selectLineStart = lineIndex
selectCharStart = charIndex selectCharStart = charIndex
upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset) upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset)
upSelectChars(textPage) upSelectChars()
} }
return return
} }
@ -342,7 +342,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectLineEnd = lineIndex selectLineEnd = lineIndex
selectCharEnd = charIndex selectCharEnd = charIndex
upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset) upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset)
upSelectChars(textPage) upSelectChars()
} }
return return
} }
@ -362,7 +362,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectLineEnd = lineIndex selectLineEnd = lineIndex
selectCharEnd = charIndex selectCharEnd = charIndex
upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset) upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset)
upSelectChars(textPage) upSelectChars()
} }
return return
} }
@ -380,7 +380,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
selectLineEnd = lineIndex selectLineEnd = lineIndex
selectCharEnd = charIndex selectCharEnd = charIndex
upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset) upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset)
upSelectChars(textPage) upSelectChars()
} }
return return
} }
@ -390,6 +390,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
/**
* 选择开始文字
*/
fun selectStartMoveIndex(relativePage: Int, lineIndex: Int, charIndex: Int) { fun selectStartMoveIndex(relativePage: Int, lineIndex: Int, charIndex: Int) {
selectPageStart = relativePage selectPageStart = relativePage
selectLineStart = lineIndex selectLineStart = lineIndex
@ -397,9 +400,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
val textLine = relativePage(relativePage).textLines[lineIndex] val textLine = relativePage(relativePage).textLines[lineIndex]
val textChar = textLine.textChars[charIndex] val textChar = textLine.textChars[charIndex]
upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset(relativePage)) upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset(relativePage))
upSelectChars(textPage) upSelectChars()
} }
/**
* 选择结束文字
*/
fun selectEndMoveIndex(relativePage: Int, lineIndex: Int, charIndex: Int) { fun selectEndMoveIndex(relativePage: Int, lineIndex: Int, charIndex: Int) {
selectPageEnd = relativePage selectPageEnd = relativePage
selectLineEnd = lineIndex selectLineEnd = lineIndex
@ -407,22 +413,35 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
val textLine = relativePage(relativePage).textLines[lineIndex] val textLine = relativePage(relativePage).textLines[lineIndex]
val textChar = textLine.textChars[charIndex] val textChar = textLine.textChars[charIndex]
upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset(relativePage)) upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset(relativePage))
upSelectChars(textPage) upSelectChars()
} }
private fun upSelectChars(textPage: TextPage) { private fun upSelectChars() {
for ((lineIndex, textLine) in textPage.textLines.withIndex()) { val last = if (ReadBookConfig.isScroll) 2 else 0
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for (relativePos in 0..last) {
textChar.selected = for ((lineIndex, textLine) in relativePage(relativePos).textLines.withIndex()) {
if (lineIndex == selectLineStart && lineIndex == selectLineEnd) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
charIndex in selectCharStart..selectCharEnd textChar.selected =
} else if (lineIndex == selectLineStart) { if (relativePos == selectPageStart
charIndex >= selectCharStart && relativePos == selectPageEnd
} else if (lineIndex == selectLineEnd) { && lineIndex == selectLineStart
charIndex <= selectCharEnd && lineIndex == selectLineEnd
} else { ) {
lineIndex in (selectLineStart + 1) until selectLineEnd charIndex in selectCharStart..selectCharEnd
} } else if (relativePos == selectPageStart && lineIndex == selectLineStart) {
charIndex >= selectCharStart
} else if (relativePos == selectPageEnd && lineIndex == selectLineEnd) {
charIndex <= selectCharEnd
} else if (relativePos == selectPageStart && relativePos == selectPageEnd) {
lineIndex in (selectLineStart + 1) until selectLineEnd
} else if (relativePos == selectPageStart) {
lineIndex > selectLineStart
} else if (relativePos == selectPageEnd) {
lineIndex < selectLineEnd
} else {
relativePos in selectPageStart + 1 until selectPageEnd
}
}
} }
} }
invalidate() invalidate()

Loading…
Cancel
Save