feat: 优化代码

pull/111/head
kunfei 5 years ago
parent 8662c66e11
commit e29c9d8ad8
  1. 15
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 27
      app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt
  3. 39
      app/src/main/java/io/legado/app/ui/book/read/page/ContentSelectActionCallback.kt
  4. 5
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  5. 2
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  6. 2
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
  7. 2
      app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt
  8. 8
      app/src/main/res/layout/popup_action_menu.xml

@ -55,6 +55,7 @@ import org.jetbrains.anko.toast
class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_book_read),
View.OnTouchListener,
PageView.CallBack,
TextActionMenu.CallBack,
ContentTextView.CallBack,
ReadMenu.CallBack,
ReadAloudDialog.CallBack,
@ -65,6 +66,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
private val requestCodeEditSource = 111
private val requestCodeReplace = 312
private var menu: Menu? = null
private var textActionMenu: TextActionMenu? = null
override val viewModel: ReadBookViewModel
get() = getViewModel(ReadBookViewModel::class.java)
@ -340,6 +342,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
cursor_left.x = x - cursor_left.width
cursor_left.y = y
cursor_left.visible(true)
showTextActionMenu()
}
/**
@ -349,6 +352,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
cursor_right.x = x
cursor_right.y = y
cursor_right.visible(true)
showTextActionMenu()
}
/**
@ -359,8 +363,14 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
cursor_right.invisible()
}
private fun showTextContextMenu(x: Float, y: Float) {
/**
* 显示文本操作菜单
*/
private fun showTextActionMenu() {
textActionMenu ?: let {
textActionMenu = TextActionMenu(this)
}
page_view.selectedText
}
/**
@ -556,6 +566,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
override fun onDestroy() {
super.onDestroy()
mHandler.removeCallbacks(keepScreenRunnable)
textActionMenu?.dismiss()
}
override fun observeLiveBus() {

@ -0,0 +1,27 @@
package io.legado.app.ui.book.read
import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.PopupWindow
import io.legado.app.R
class TextActionMenu(context: Context, callBack: CallBack) :
PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) {
init {
@SuppressLint("InflateParams")
contentView = LayoutInflater.from(context).inflate(R.layout.popup_action_menu, null)
isTouchable = true
isOutsideTouchable = false
isFocusable = false
}
interface CallBack {
}
}

@ -1,39 +0,0 @@
package io.legado.app.ui.book.read.page
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.widget.TextView
import io.legado.app.R
import io.legado.app.constant.EventBus
import io.legado.app.utils.postEvent
class ContentSelectActionCallback(private val textView: TextView) : ActionMode.Callback {
override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_replace -> {
val text = textView.text.substring(textView.selectionStart, textView.selectionEnd)
postEvent(EventBus.REPLACE, text)
mode?.finish()
return true
}
}
return false
}
override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean {
mode?.menuInflater?.inflate(R.menu.content_select_action, menu)
return true
}
override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean {
return false
}
override fun onDestroyActionMode(mode: ActionMode?) {
}
}

@ -200,6 +200,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
activityCallBack?.onCancelSelect()
}
val selectedText: String
get() {
return ""
}
interface CallBack {
fun upSelectedStart(x: Float, y: Float)
fun upSelectedEnd(x: Float, y: Float)

@ -121,6 +121,8 @@ class ContentView(context: Context) : FrameLayout(context) {
content_text_view.cancelSelect()
}
val selectedText: String get() = content_text_view.selectedText
fun scrollTo(pos: Int?) {
if (pos != null) {
content_text_view.post {

@ -191,6 +191,8 @@ class PageView(context: Context, attrs: AttributeSet) :
curPage.selectEndMove(x, y)
}
val selectedText: String get() = curPage.selectedText
override val isScrollDelegate: Boolean
get() = pageDelegate is ScrollPageDelegate

@ -23,7 +23,7 @@ class KeyboardToolPop(
init {
@SuppressLint("InflateParams")
this.contentView = LayoutInflater.from(context).inflate(R.layout.popup_keyboard_tool, null)
contentView = LayoutInflater.from(context).inflate(R.layout.popup_keyboard_tool, null)
isTouchable = true
isOutsideTouchable = false

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@color/background_card"
android:padding="5dp" />
Loading…
Cancel
Save