feat: 优化代码

pull/117/head
kunfei 5 years ago
parent 8132afde6c
commit 01f886b7d1
  1. 208
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt

@ -28,12 +28,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
private var callBack: CallBack private var callBack: CallBack
private val visibleRect = RectF() private val visibleRect = RectF()
private var selectPageStart = 0 private var selectStart = arrayOf(0, 0, 0)
private var selectLineStart = 0 private var selectEnd = arrayOf(0, 0, 0)
private var selectCharStart = 0
private var selectPageEnd = 0
private var selectLineEnd = 0
private var selectCharEnd = 0
private var textPage: TextPage = TextPage() private var textPage: TextPage = TextPage()
//滚动参数 //滚动参数
private val pageFactory: TextPageFactory get() = callBack.pageFactory private val pageFactory: TextPageFactory get() = callBack.pageFactory
@ -197,12 +193,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (x > textChar.start && x < textChar.end) { if (x > textChar.start && x < textChar.end) {
textChar.selected = true textChar.selected = true
invalidate() invalidate()
selectPageStart = 0 selectStart[0] = 0
selectLineStart = lineIndex selectStart[1] = lineIndex
selectCharStart = charIndex selectStart[2] = charIndex
selectPageEnd = 0 selectEnd[0] = 0
selectLineEnd = lineIndex selectEnd[1] = lineIndex
selectCharEnd = charIndex selectEnd[2] = charIndex
upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset) upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset)
upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset) upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset)
select(0, lineIndex, charIndex) select(0, lineIndex, charIndex)
@ -223,12 +219,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (x > textChar.start && x < textChar.end) { if (x > textChar.start && x < textChar.end) {
textChar.selected = true textChar.selected = true
invalidate() invalidate()
selectPageStart = 1 selectStart[0] = 1
selectLineStart = lineIndex selectStart[1] = lineIndex
selectCharStart = charIndex selectStart[2] = charIndex
selectPageEnd = 1 selectEnd[0] = 1
selectLineEnd = lineIndex selectEnd[1] = lineIndex
selectCharEnd = charIndex selectEnd[2] = charIndex
upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset) upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset)
upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset) upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset)
select(1, lineIndex, charIndex) select(1, lineIndex, charIndex)
@ -246,12 +242,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (x > textChar.start && x < textChar.end) { if (x > textChar.start && x < textChar.end) {
textChar.selected = true textChar.selected = true
invalidate() invalidate()
selectPageStart = 2 selectStart[0] = 2
selectLineStart = lineIndex selectStart[1] = lineIndex
selectCharStart = charIndex selectStart[2] = charIndex
selectPageEnd = 2 selectEnd[0] = 2
selectLineEnd = lineIndex selectEnd[1] = lineIndex
selectCharEnd = charIndex selectEnd[2] = charIndex
upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset) upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset)
upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset) upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset)
select(2, lineIndex, charIndex) select(2, lineIndex, charIndex)
@ -273,12 +269,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) { if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.start && x < textChar.end) { if (x > textChar.start && x < textChar.end) {
if (selectLineStart != lineIndex || selectCharStart != charIndex) { if (selectStart[1] != lineIndex || selectStart[2] != charIndex) {
selectPageStart = 0 selectStart[0] = 0
selectLineStart = lineIndex selectStart[1] = lineIndex
selectCharStart = charIndex selectStart[2] = charIndex
upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset) upSelect()
upSelectChars()
} }
return return
} }
@ -294,12 +289,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) { if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.start && x < textChar.end) { if (x > textChar.start && x < textChar.end) {
if (selectLineStart != lineIndex || selectCharStart != charIndex) { if (selectStart[1] != lineIndex || selectStart[2] != charIndex) {
selectPageStart = 1 selectStart[0] = 1
selectLineStart = lineIndex selectStart[1] = lineIndex
selectCharStart = charIndex selectStart[2] = charIndex
upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset) upSelect()
upSelectChars()
} }
return return
} }
@ -313,12 +307,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) { if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.start && x < textChar.end) { if (x > textChar.start && x < textChar.end) {
if (selectLineStart != lineIndex || selectCharStart != charIndex) { if (selectStart[1] != lineIndex || selectStart[2] != charIndex) {
selectPageStart = 1 selectStart[0] = 2
selectLineStart = lineIndex selectStart[1] = lineIndex
selectCharStart = charIndex selectStart[2] = charIndex
upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset) upSelect()
upSelectChars()
} }
return return
} }
@ -338,11 +331,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) { if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.start && x < textChar.end) { if (x > textChar.start && x < textChar.end) {
if (selectLineEnd != lineIndex || selectCharEnd != charIndex) { if (selectEnd[1] != lineIndex || selectEnd[2] != charIndex) {
selectLineEnd = lineIndex selectEnd[0] = 0
selectCharEnd = charIndex selectEnd[1] = lineIndex
upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset) selectEnd[2] = charIndex
upSelectChars() upSelect()
} }
return return
} }
@ -358,11 +351,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) { if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.start && x < textChar.end) { if (x > textChar.start && x < textChar.end) {
if (selectLineEnd != lineIndex || selectCharEnd != charIndex) { if (selectEnd[1] != lineIndex || selectEnd[2] != charIndex) {
selectLineEnd = lineIndex selectEnd[0] = 1
selectCharEnd = charIndex selectEnd[1] = lineIndex
upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset) selectEnd[2] = charIndex
upSelectChars() upSelect()
} }
return return
} }
@ -376,11 +369,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) { if (y > textLine.lineTop + relativeOffset && y < textLine.lineBottom + relativeOffset) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.start && x < textChar.end) { if (x > textChar.start && x < textChar.end) {
if (selectLineEnd != lineIndex || selectCharEnd != charIndex) { if (selectEnd[1] != lineIndex || selectEnd[2] != charIndex) {
selectLineEnd = lineIndex selectEnd[0] = 2
selectCharEnd = charIndex selectEnd[1] = lineIndex
upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset) selectEnd[2] = charIndex
upSelectChars() upSelect()
} }
return return
} }
@ -394,9 +387,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 selectStart[0] = relativePage
selectLineStart = lineIndex selectStart[1] = lineIndex
selectCharStart = charIndex selectStart[2] = charIndex
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))
@ -407,39 +400,56 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
* 选择结束文字 * 选择结束文字
*/ */
fun selectEndMoveIndex(relativePage: Int, lineIndex: Int, charIndex: Int) { fun selectEndMoveIndex(relativePage: Int, lineIndex: Int, charIndex: Int) {
selectPageEnd = relativePage selectEnd[0] = relativePage
selectLineEnd = lineIndex selectEnd[1] = lineIndex
selectCharEnd = charIndex selectEnd[2] = charIndex
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() upSelectChars()
} }
private fun upSelect() {
val start = selectStart[0] * 1000000 + selectStart[1] * 100000 + selectStart[2]
val end = selectEnd[0] * 1000000 + selectEnd[1] * 100000 + selectEnd[2]
if (start > end) {
val value = selectStart
selectStart = selectEnd
selectEnd = value
}
var textLine = relativePage(selectStart[0]).textLines[selectStart[1]]
var textChar = textLine.textChars[selectStart[2]]
upSelectedStart(textChar.start, textLine.lineBottom + relativeOffset(selectStart[0]))
textLine = relativePage(selectEnd[0]).textLines[selectEnd[0]]
textChar = textLine.textChars[selectEnd[2]]
upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset(selectEnd[0]))
upSelectChars()
}
private fun upSelectChars() { private fun upSelectChars() {
val last = if (ReadBookConfig.isScroll) 2 else 0 val last = if (ReadBookConfig.isScroll) 2 else 0
for (relativePos in 0..last) { for (relativePos in 0..last) {
for ((lineIndex, textLine) in relativePage(relativePos).textLines.withIndex()) { for ((lineIndex, textLine) in relativePage(relativePos).textLines.withIndex()) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
textChar.selected = textChar.selected =
if (relativePos == selectPageStart if (relativePos == selectStart[0]
&& relativePos == selectPageEnd && relativePos == selectEnd[0]
&& lineIndex == selectLineStart && lineIndex == selectStart[1]
&& lineIndex == selectLineEnd && lineIndex == selectEnd[1]
) { ) {
charIndex in selectCharStart..selectCharEnd charIndex in selectStart[2]..selectEnd[2]
} else if (relativePos == selectPageStart && lineIndex == selectLineStart) { } else if (relativePos == selectStart[0] && lineIndex == selectStart[1]) {
charIndex >= selectCharStart charIndex >= selectStart[2]
} else if (relativePos == selectPageEnd && lineIndex == selectLineEnd) { } else if (relativePos == selectEnd[0] && lineIndex == selectEnd[1]) {
charIndex <= selectCharEnd charIndex <= selectEnd[2]
} else if (relativePos == selectPageStart && relativePos == selectPageEnd) { } else if (relativePos == selectStart[0] && relativePos == selectEnd[0]) {
lineIndex in (selectLineStart + 1) until selectLineEnd lineIndex in (selectStart[1] + 1) until selectEnd[1]
} else if (relativePos == selectPageStart) { } else if (relativePos == selectStart[0]) {
lineIndex > selectLineStart lineIndex > selectStart[1]
} else if (relativePos == selectPageEnd) { } else if (relativePos == selectEnd[0]) {
lineIndex < selectLineEnd lineIndex < selectEnd[1]
} else { } else {
relativePos in selectPageStart + 1 until selectPageEnd relativePos in selectStart[0] + 1 until selectEnd[0]
} }
} }
} }
@ -471,55 +481,55 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
val selectedText: String val selectedText: String
get() { get() {
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
for (relativePos in selectPageStart..selectPageEnd) { for (relativePos in selectStart[0]..selectEnd[0]) {
val textPage = relativePage(relativePos) val textPage = relativePage(relativePos)
if (relativePos == selectPageStart && relativePos == selectPageEnd) { if (relativePos == selectStart[0] && relativePos == selectEnd[0]) {
for (lineIndex in selectLineStart..selectLineEnd) { for (lineIndex in selectStart[1]..selectEnd[1]) {
if (lineIndex == selectLineStart && lineIndex == selectLineEnd) { if (lineIndex == selectStart[1] && lineIndex == selectEnd[1]) {
stringBuilder.append( stringBuilder.append(
textPage.textLines[lineIndex].text.substring( textPage.textLines[lineIndex].text.substring(
selectCharStart, selectStart[2],
selectCharEnd + 1 selectEnd[2] + 1
) )
) )
} else if (lineIndex == selectLineStart) { } else if (lineIndex == selectStart[1]) {
stringBuilder.append( stringBuilder.append(
textPage.textLines[lineIndex].text.substring( textPage.textLines[lineIndex].text.substring(
selectCharStart selectStart[2]
) )
) )
} else if (lineIndex == selectLineEnd) { } else if (lineIndex == selectEnd[1]) {
stringBuilder.append( stringBuilder.append(
textPage.textLines[lineIndex].text.substring(0, selectCharEnd + 1) textPage.textLines[lineIndex].text.substring(0, selectEnd[2] + 1)
) )
} else { } else {
stringBuilder.append(textPage.textLines[lineIndex].text) stringBuilder.append(textPage.textLines[lineIndex].text)
} }
} }
} else if (relativePos == selectPageStart) { } else if (relativePos == selectStart[0]) {
for (lineIndex in selectLineStart until relativePage(relativePos).textLines.size) { for (lineIndex in selectStart[1] until relativePage(relativePos).textLines.size) {
if (lineIndex == selectLineStart) { if (lineIndex == selectStart[1]) {
stringBuilder.append( stringBuilder.append(
textPage.textLines[lineIndex].text.substring( textPage.textLines[lineIndex].text.substring(
selectCharStart selectStart[2]
) )
) )
} else { } else {
stringBuilder.append(textPage.textLines[lineIndex].text) stringBuilder.append(textPage.textLines[lineIndex].text)
} }
} }
} else if (relativePos == selectPageEnd) { } else if (relativePos == selectEnd[0]) {
for (lineIndex in 0..selectLineEnd) { for (lineIndex in 0..selectEnd[1]) {
if (lineIndex == selectLineEnd) { if (lineIndex == selectEnd[1]) {
stringBuilder.append( stringBuilder.append(
textPage.textLines[lineIndex].text.substring(0, selectCharEnd + 1) textPage.textLines[lineIndex].text.substring(0, selectEnd[2] + 1)
) )
} else { } else {
stringBuilder.append(textPage.textLines[lineIndex].text) stringBuilder.append(textPage.textLines[lineIndex].text)
} }
} }
} else if (relativePos in selectPageStart + 1 until selectPageEnd) { } else if (relativePos in selectStart[0] + 1 until selectEnd[0]) {
for (lineIndex in selectLineStart..selectLineEnd) { for (lineIndex in selectStart[1]..selectEnd[1]) {
stringBuilder.append(textPage.textLines[lineIndex].text) stringBuilder.append(textPage.textLines[lineIndex].text)
} }
} }

Loading…
Cancel
Save