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

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

Loading…
Cancel
Save