feat: 优化代码

pull/111/head
kunfei 5 years ago
parent f0ce910944
commit 228825dd08
  1. 6
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 65
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 4
      app/src/main/java/io/legado/app/ui/replacerule/edit/ReplaceEditDialog.kt

@ -389,10 +389,8 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
override fun onMenuItemSelected(item: MenuItemImpl): Boolean {
when (item.itemId) {
R.id.menu_replace -> ReplaceEditDialog.show(
supportFragmentManager,
pattern = selectedText
)
R.id.menu_replace -> ReplaceEditDialog
.show(supportFragmentManager, pattern = selectedText)
}
return false
}

@ -24,10 +24,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
}
private var activityCallBack: CallBack
var selectAble = context.getPrefBoolean(PreferKey.textSelectAble)
var selectStartLine = 0
var selectStartChar = 0
var selectEndLine = 0
var selectEndChar = 0
private var selectLineStart = 0
private var selectCharStart = 0
private var selectLineEnd = 0
private var selectCharEnd = 0
private var textPage: TextPage? = null
init {
@ -91,10 +91,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) {
textChar.selected = true
invalidate()
selectStartLine = lineIndex
selectStartChar = charIndex
selectEndLine = lineIndex
selectEndChar = charIndex
selectLineStart = lineIndex
selectCharStart = charIndex
selectLineEnd = lineIndex
selectCharEnd = charIndex
upSelectedStart(
textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y.toFloat()
@ -119,9 +119,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (y > textLine.lineTop && y < textLine.lineBottom) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) {
if (selectStartLine != lineIndex || selectStartChar != charIndex) {
selectStartLine = lineIndex
selectStartChar = charIndex
if (selectLineStart != lineIndex || selectCharStart != charIndex) {
selectLineStart = lineIndex
selectCharStart = charIndex
upSelectedStart(
textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y.toFloat()
@ -143,9 +143,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (y > textLine.lineTop && y < textLine.lineBottom) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) {
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) {
if (selectEndLine != lineIndex || selectEndChar != charIndex) {
selectEndLine = lineIndex
selectEndChar = charIndex
if (selectLineEnd != lineIndex || selectCharEnd != charIndex) {
selectLineEnd = lineIndex
selectCharEnd = charIndex
upSelectedEnd(
textChar.rightTopPosition.x,
textChar.leftBottomPosition.y.toFloat()
@ -165,14 +165,14 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
for ((lineIndex, textLine) in textPage.textLines.withIndex()) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) {
textChar.selected =
if (lineIndex == selectStartLine && lineIndex == selectEndLine) {
charIndex in selectStartChar..selectEndChar
} else if (lineIndex == selectStartLine) {
charIndex >= selectStartChar
} else if (lineIndex == selectEndLine) {
charIndex <= selectEndChar
if (lineIndex == selectLineStart && lineIndex == selectLineEnd) {
charIndex in selectCharStart..selectCharEnd
} else if (lineIndex == selectLineStart) {
charIndex >= selectCharStart
} else if (lineIndex == selectLineEnd) {
charIndex <= selectCharEnd
} else {
lineIndex in (selectStartLine + 1) until selectEndLine
lineIndex in (selectLineStart + 1) until selectLineEnd
}
}
}
@ -201,7 +201,28 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
val selectedText: String
get() {
return ""
val stringBuilder = StringBuilder()
textPage?.let {
for (lineIndex in selectLineStart..selectLineEnd) {
if (lineIndex == selectLineStart && lineIndex == selectLineEnd) {
stringBuilder.append(
it.textLines[lineIndex].text.substring(
selectCharStart + 1,
selectCharEnd + 2
)
)
} else if (lineIndex == selectLineStart) {
stringBuilder.append(it.textLines[lineIndex].text.substring(selectCharStart + 1))
} else if (lineIndex == selectLineEnd) {
stringBuilder.append(
it.textLines[lineIndex].text.substring(0, selectCharEnd + 1)
)
} else {
stringBuilder.append(it.textLines[lineIndex].text)
}
}
}
return stringBuilder.toString()
}
interface CallBack {

@ -23,14 +23,12 @@ class ReplaceEditDialog : DialogFragment(),
companion object {
fun show(fragmentManager: FragmentManager, id: Long? = null, pattern: String? = null) {
fun show(fragmentManager: FragmentManager, id: Long = -1, pattern: String? = null) {
val dialog = ReplaceEditDialog()
id?.let {
val bundle = Bundle()
bundle.putLong("id", id)
bundle.putString("pattern", pattern)
dialog.arguments = bundle
}
dialog.show(fragmentManager, "editReplace")
}
}

Loading…
Cancel
Save