feat: 优化代码

pull/111/head
kunfei 5 years ago
parent 2a87606be5
commit d291540513
  1. 12
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 12
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 24
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  4. 2
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt

@ -71,8 +71,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
override val viewModel: ReadBookViewModel override val viewModel: ReadBookViewModel
get() = getViewModel(ReadBookViewModel::class.java) get() = getViewModel(ReadBookViewModel::class.java)
override val isInitFinish: Boolean override val isInitFinish: Boolean get() = viewModel.isInitFinish
get() = viewModel.isInitFinish
private val mHandler = Handler() private val mHandler = Handler()
private val keepScreenRunnable: Runnable = Runnable { Help.keepScreenOn(window, false) } private val keepScreenRunnable: Runnable = Runnable { Help.keepScreenOn(window, false) }
@ -80,6 +79,8 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
private var screenTimeOut: Long = 0 private var screenTimeOut: Long = 0
private var timeElectricityReceiver: TimeElectricityReceiver? = null private var timeElectricityReceiver: TimeElectricityReceiver? = null
override val headerHeight: Int get() = page_view.headerHeight
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
Help.upLayoutInDisplayCutoutMode(window) Help.upLayoutInDisplayCutoutMode(window)
initView() initView()
@ -373,10 +374,11 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
} }
textActionMenu?.let { popup -> textActionMenu?.let { popup ->
if (!popup.isShowing) { if (!popup.isShowing) {
popup.showAsDropDown( popup.showAtLocation(
cursor_left, cursor_left,
cursor_left.width, Gravity.BOTTOM or Gravity.START,
-cursor_left.height - ReadBookConfig.durConfig.textSize.dp cursor_left.x.toInt() + cursor_left.width,
page_view.height - cursor_left.y.toInt() + ReadBookConfig.durConfig.textSize.dp + popup.height
) )
} }
} }

@ -22,8 +22,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
style = Paint.Style.FILL style = Paint.Style.FILL
} }
} }
private var activityCallBack: CallBack? = null private var activityCallBack: CallBack
var headerHeight = 0
var selectAble = context.getPrefBoolean(PreferKey.textSelectAble) var selectAble = context.getPrefBoolean(PreferKey.textSelectAble)
var selectStartLine = 0 var selectStartLine = 0
var selectStartChar = 0 var selectStartChar = 0
@ -32,7 +31,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private var textPage: TextPage? = null private var textPage: TextPage? = null
init { init {
activityCallBack = activity as? CallBack activityCallBack = activity as CallBack
} }
fun setContent(textPage: TextPage?) { fun setContent(textPage: TextPage?) {
@ -181,11 +180,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
private fun upSelectedStart(x: Float, y: Float) { private fun upSelectedStart(x: Float, y: Float) {
activityCallBack?.upSelectedStart(x, y + headerHeight) activityCallBack.upSelectedStart(x, y + activityCallBack.headerHeight)
} }
private fun upSelectedEnd(x: Float, y: Float) { private fun upSelectedEnd(x: Float, y: Float) {
activityCallBack?.upSelectedEnd(x, y + headerHeight) activityCallBack.upSelectedEnd(x, y + activityCallBack.headerHeight)
} }
fun cancelSelect() { fun cancelSelect() {
@ -197,7 +196,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
} }
invalidate() invalidate()
} }
activityCallBack?.onCancelSelect() activityCallBack.onCancelSelect()
} }
val selectedText: String val selectedText: String
@ -209,5 +208,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
fun upSelectedStart(x: Float, y: Float) fun upSelectedStart(x: Float, y: Float)
fun upSelectedEnd(x: Float, y: Float) fun upSelectedEnd(x: Float, y: Float)
fun onCancelSelect() fun onCancelSelect()
val headerHeight: Int
} }
} }

@ -23,6 +23,7 @@ class ContentView(context: Context) : FrameLayout(context) {
//设置背景防止切换背景时文字重叠 //设置背景防止切换背景时文字重叠
setBackgroundColor(context.getCompatColor(R.color.background)) setBackgroundColor(context.getCompatColor(R.color.background))
inflate(context, R.layout.view_book_page, this) inflate(context, R.layout.view_book_page, this)
upStyle() upStyle()
upTime() upTime()
} }
@ -33,9 +34,8 @@ class ContentView(context: Context) : FrameLayout(context) {
tv_top_right.typeface = ChapterProvider.typeface tv_top_right.typeface = ChapterProvider.typeface
tv_bottom_left.typeface = ChapterProvider.typeface tv_bottom_left.typeface = ChapterProvider.typeface
tv_bottom_right.typeface = ChapterProvider.typeface tv_bottom_right.typeface = ChapterProvider.typeface
if (context.getPrefBoolean(PreferKey.hideStatusBar, false)) {
//显示状态栏时隐藏header //显示状态栏时隐藏header
ll_header.visible() if (context.getPrefBoolean(PreferKey.hideStatusBar, false)) {
ll_header.layoutParams = ll_header.layoutParams =
ll_header.layoutParams.apply { height = context.getStatusBarHeight() } ll_header.layoutParams.apply { height = context.getStatusBarHeight() }
ll_header.setPadding( ll_header.setPadding(
@ -44,12 +44,11 @@ class ContentView(context: Context) : FrameLayout(context) {
headerPaddingRight.dp, headerPaddingRight.dp,
headerPaddingBottom.dp headerPaddingBottom.dp
) )
content_text_view.headerHeight = ll_header.height ll_header.visible()
page_panel.setPadding(0, 0, 0, 0) page_panel.setPadding(0, 0, 0, 0)
} else { } else {
ll_header.gone() ll_header.gone()
content_text_view.headerHeight = context.getStatusBarHeight() page_panel.setPadding(0, context.getStatusBarHeight(), 0, 0)
page_panel.setPadding(0, content_text_view.headerHeight, 0, 0)
} }
content_text_view.setPadding( content_text_view.setPadding(
paddingLeft.dp, paddingLeft.dp,
@ -72,6 +71,15 @@ class ContentView(context: Context) : FrameLayout(context) {
} }
} }
val headerHeight: Int
get() {
return if (context.getPrefBoolean(PreferKey.hideStatusBar, false)) {
ll_header.height
} else {
context.getStatusBarHeight()
}
}
fun setBg(bg: Drawable?) { fun setBg(bg: Drawable?) {
page_panel.background = bg page_panel.background = bg
} }
@ -105,16 +113,16 @@ class ContentView(context: Context) : FrameLayout(context) {
} }
fun selectText(e: MotionEvent): Boolean { fun selectText(e: MotionEvent): Boolean {
val y = e.y - content_text_view.headerHeight val y = e.y - headerHeight
return content_text_view.selectText(e.x, y) return content_text_view.selectText(e.x, y)
} }
fun selectStartMove(x: Float, y: Float) { fun selectStartMove(x: Float, y: Float) {
content_text_view.selectStartMove(x, y - content_text_view.headerHeight) content_text_view.selectStartMove(x, y - headerHeight)
} }
fun selectEndMove(x: Float, y: Float) { fun selectEndMove(x: Float, y: Float) {
content_text_view.selectEndMove(x, y - content_text_view.headerHeight) content_text_view.selectEndMove(x, y - headerHeight)
} }
fun cancelSelect() { fun cancelSelect() {

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

Loading…
Cancel
Save