feat: 优化代码

pull/111/head
kunfei 5 years ago
parent f77fe68745
commit f06d77de89
  1. 29
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt

@ -29,7 +29,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private var selectCharStart = 0 private var selectCharStart = 0
private var selectLineEnd = 0 private var selectLineEnd = 0
private var selectCharEnd = 0 private var selectCharEnd = 0
private var textPage: TextPage? = null private var textPage: TextPage = TextPage()
//滚动参数 //滚动参数
private val maxScrollOffset = 100f private val maxScrollOffset = 100f
private var pageOffset = 0f private var pageOffset = 0f
@ -39,7 +39,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
activityCallBack = activity as CallBack activityCallBack = activity as CallBack
} }
fun setContent(textPage: TextPage?) { fun setContent(textPage: TextPage) {
this.textPage = textPage this.textPage = textPage
invalidate() invalidate()
} }
@ -55,7 +55,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
super.onDraw(canvas) super.onDraw(canvas)
textPage?.let { textPage ->
textPage.textLines.forEach { textLine -> textPage.textLines.forEach { textLine ->
val textPaint = if (textLine.isTitle) { val textPaint = if (textLine.isTitle) {
ChapterProvider.titlePaint ChapterProvider.titlePaint
@ -86,7 +85,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
} }
}
fun onScroll(mOffset: Float) { fun onScroll(mOffset: Float) {
var offset = mOffset var offset = mOffset
@ -105,10 +103,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
pageOffset = 0f pageOffset = 0f
} }
} }
fun selectText(x: Float, y: Float): Boolean { fun selectText(x: Float, y: Float): Boolean {
textPage?.let { textPage ->
for ((lineIndex, textLine) in textPage.textLines.withIndex()) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) {
if (y > textLine.lineTop && y < textLine.lineBottom) { if (y > textLine.lineTop && y < textLine.lineBottom) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
@ -133,12 +131,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
break break
} }
} }
}
return false return false
} }
fun selectStartMove(x: Float, y: Float) { fun selectStartMove(x: Float, y: Float) {
textPage?.let { textPage ->
for ((lineIndex, textLine) in textPage.textLines.withIndex()) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) {
if (y > textLine.lineTop && y < textLine.lineBottom) { if (y > textLine.lineTop && y < textLine.lineBottom) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
@ -159,10 +155,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
} }
}
fun selectEndMove(x: Float, y: Float) { fun selectEndMove(x: Float, y: Float) {
textPage?.let { textPage ->
for ((lineIndex, textLine) in textPage.textLines.withIndex()) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) {
if (y > textLine.lineTop && y < textLine.lineBottom) { if (y > textLine.lineTop && y < textLine.lineBottom) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
@ -183,7 +177,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
} }
} }
}
private fun upSelectChars(textPage: TextPage) { private fun upSelectChars(textPage: TextPage) {
for ((lineIndex, textLine) in textPage.textLines.withIndex()) { for ((lineIndex, textLine) in textPage.textLines.withIndex()) {
@ -212,38 +205,38 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
fun cancelSelect() { fun cancelSelect() {
textPage?.let { textPage ->
textPage.textLines.forEach { textLine -> textPage.textLines.forEach { textLine ->
textLine.textChars.forEach { textLine.textChars.forEach {
it.selected = false it.selected = false
} }
} }
invalidate() invalidate()
}
activityCallBack.onCancelSelect() activityCallBack.onCancelSelect()
} }
val selectedText: String val selectedText: String
get() { get() {
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
textPage?.let {
for (lineIndex in selectLineStart..selectLineEnd) { for (lineIndex in selectLineStart..selectLineEnd) {
if (lineIndex == selectLineStart && lineIndex == selectLineEnd) { if (lineIndex == selectLineStart && lineIndex == selectLineEnd) {
stringBuilder.append( stringBuilder.append(
it.textLines[lineIndex].text.substring( textPage.textLines[lineIndex].text.substring(
selectCharStart, selectCharStart,
selectCharEnd + 1 selectCharEnd + 1
) )
) )
} else if (lineIndex == selectLineStart) { } else if (lineIndex == selectLineStart) {
stringBuilder.append(it.textLines[lineIndex].text.substring(selectCharStart)) stringBuilder.append(
textPage.textLines[lineIndex].text.substring(
selectCharStart
)
)
} else if (lineIndex == selectLineEnd) { } else if (lineIndex == selectLineEnd) {
stringBuilder.append( stringBuilder.append(
it.textLines[lineIndex].text.substring(0, selectCharEnd + 1) textPage.textLines[lineIndex].text.substring(0, selectCharEnd + 1)
) )
} else { } else {
stringBuilder.append(it.textLines[lineIndex].text) stringBuilder.append(textPage.textLines[lineIndex].text)
}
} }
} }
return stringBuilder.toString() return stringBuilder.toString()

@ -93,8 +93,8 @@ class ContentView(context: Context) : FrameLayout(context) {
} }
fun setContent(textPage: TextPage?) { fun setContent(textPage: TextPage?) {
content_text_view.setContent(textPage)
if (textPage != null) { if (textPage != null) {
content_text_view.setContent(textPage)
tv_bottom_left.text = textPage.title tv_bottom_left.text = textPage.title
pageSize = textPage.pageSize pageSize = textPage.pageSize
setPageIndex(textPage.index) setPageIndex(textPage.index)

Loading…
Cancel
Save