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. 12
      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 { override fun onMenuItemSelected(item: MenuItemImpl): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.menu_replace -> ReplaceEditDialog.show( R.id.menu_replace -> ReplaceEditDialog
supportFragmentManager, .show(supportFragmentManager, pattern = selectedText)
pattern = selectedText
)
} }
return false return false
} }

@ -24,10 +24,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
private var activityCallBack: CallBack private var activityCallBack: CallBack
var selectAble = context.getPrefBoolean(PreferKey.textSelectAble) var selectAble = context.getPrefBoolean(PreferKey.textSelectAble)
var selectStartLine = 0 private var selectLineStart = 0
var selectStartChar = 0 private var selectCharStart = 0
var selectEndLine = 0 private var selectLineEnd = 0
var selectEndChar = 0 private var selectCharEnd = 0
private var textPage: TextPage? = null private var textPage: TextPage? = null
init { init {
@ -91,10 +91,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) { if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) {
textChar.selected = true textChar.selected = true
invalidate() invalidate()
selectStartLine = lineIndex selectLineStart = lineIndex
selectStartChar = charIndex selectCharStart = charIndex
selectEndLine = lineIndex selectLineEnd = lineIndex
selectEndChar = charIndex selectCharEnd = charIndex
upSelectedStart( upSelectedStart(
textChar.leftBottomPosition.x, textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y.toFloat() textChar.leftBottomPosition.y.toFloat()
@ -119,9 +119,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
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()) {
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) { if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) {
if (selectStartLine != lineIndex || selectStartChar != charIndex) { if (selectLineStart != lineIndex || selectCharStart != charIndex) {
selectStartLine = lineIndex selectLineStart = lineIndex
selectStartChar = charIndex selectCharStart = charIndex
upSelectedStart( upSelectedStart(
textChar.leftBottomPosition.x, textChar.leftBottomPosition.x,
textChar.leftBottomPosition.y.toFloat() textChar.leftBottomPosition.y.toFloat()
@ -143,9 +143,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
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()) {
if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) { if (x > textChar.leftBottomPosition.x && x < textChar.rightTopPosition.x) {
if (selectEndLine != lineIndex || selectEndChar != charIndex) { if (selectLineEnd != lineIndex || selectCharEnd != charIndex) {
selectEndLine = lineIndex selectLineEnd = lineIndex
selectEndChar = charIndex selectCharEnd = charIndex
upSelectedEnd( upSelectedEnd(
textChar.rightTopPosition.x, textChar.rightTopPosition.x,
textChar.leftBottomPosition.y.toFloat() 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 ((lineIndex, textLine) in textPage.textLines.withIndex()) {
for ((charIndex, textChar) in textLine.textChars.withIndex()) { for ((charIndex, textChar) in textLine.textChars.withIndex()) {
textChar.selected = textChar.selected =
if (lineIndex == selectStartLine && lineIndex == selectEndLine) { if (lineIndex == selectLineStart && lineIndex == selectLineEnd) {
charIndex in selectStartChar..selectEndChar charIndex in selectCharStart..selectCharEnd
} else if (lineIndex == selectStartLine) { } else if (lineIndex == selectLineStart) {
charIndex >= selectStartChar charIndex >= selectCharStart
} else if (lineIndex == selectEndLine) { } else if (lineIndex == selectLineEnd) {
charIndex <= selectEndChar charIndex <= selectCharEnd
} else { } 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 val selectedText: String
get() { 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 { interface CallBack {

@ -23,14 +23,12 @@ class ReplaceEditDialog : DialogFragment(),
companion object { 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() val dialog = ReplaceEditDialog()
id?.let { val bundle = Bundle()
val bundle = Bundle() bundle.putLong("id", id)
bundle.putLong("id", id) bundle.putString("pattern", pattern)
bundle.putString("pattern", pattern) dialog.arguments = bundle
dialog.arguments = bundle
}
dialog.show(fragmentManager, "editReplace") dialog.show(fragmentManager, "editReplace")
} }
} }

Loading…
Cancel
Save