feat: 优化代码

pull/141/head
kunfei 5 years ago
parent c99355e504
commit 39ed24d4dc
  1. 4
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 83
      app/src/main/java/io/legado/app/ui/book/read/config/ReadStyleDialog.kt
  3. 91
      app/src/main/java/io/legado/app/ui/book/read/config/ReadTypeDialog.kt
  4. 12
      app/src/main/res/drawable/ic_type_default_1.xml
  5. 15
      app/src/main/res/drawable/ic_type_default_2.xml
  6. 18
      app/src/main/res/drawable/ic_type_default_3.xml
  7. 117
      app/src/main/res/layout/dialog_read_book_style.xml
  8. 45
      app/src/main/res/layout/dialog_read_type.xml
  9. 51
      app/src/main/res/layout/dialog_title_config.xml
  10. 6
      app/src/main/res/values/arrays.xml
  11. 5
      app/src/main/res/values/strings.xml

@ -152,10 +152,6 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
PaddingConfigDialog().show(supportFragmentManager, "paddingConfig")
}
fun showTypeConfig() {
ReadTypeDialog().show(supportFragmentManager, "readTypeDialog")
}
fun showBgTextConfig() {
BgTextConfigDialog().show(supportFragmentManager, "bgTextConfig")
}

@ -1,5 +1,6 @@
package io.legado.app.ui.book.read.config
import android.annotation.SuppressLint
import android.content.DialogInterface
import android.os.Bundle
import android.util.DisplayMetrics
@ -13,6 +14,7 @@ import io.legado.app.R
import io.legado.app.constant.EventBus
import io.legado.app.constant.PreferKey
import io.legado.app.help.ReadBookConfig
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.selector
import io.legado.app.lib.theme.accentColor
import io.legado.app.lib.theme.bottomBackground
@ -20,12 +22,10 @@ import io.legado.app.lib.theme.primaryColor
import io.legado.app.ui.book.read.Help
import io.legado.app.ui.book.read.ReadBookActivity
import io.legado.app.ui.widget.font.FontSelectDialog
import io.legado.app.utils.getIndexById
import io.legado.app.utils.getPrefString
import io.legado.app.utils.postEvent
import io.legado.app.utils.putPrefString
import io.legado.app.utils.*
import kotlinx.android.synthetic.main.activity_book_read.*
import kotlinx.android.synthetic.main.dialog_read_book_style.*
import kotlinx.android.synthetic.main.dialog_title_config.view.*
import org.jetbrains.anko.sdk27.listeners.onCheckedChange
import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.sdk27.listeners.onLongClick
@ -33,7 +33,6 @@ import org.jetbrains.anko.sdk27.listeners.onLongClick
class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
val callBack get() = activity as? ReadBookActivity
lateinit var titleModes: Array<String>
override fun onStart() {
super.onStart()
@ -63,7 +62,6 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
titleModes = requireContext().resources.getStringArray(R.array.title_mode)
initView()
initData()
initViewEvent()
@ -76,6 +74,14 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
private fun initView() {
root_view.setBackgroundColor(requireContext().bottomBackground)
dsb_text_size.valueFormat = {
(it + 5).toString()
}
dsb_text_letter_spacing.valueFormat = {
((it - 50) / 100f).toString()
}
dsb_line_size.valueFormat = { ((it - 10) / 10f).toString() }
dsb_paragraph_spacing.valueFormat = { (it / 10f).toString() }
}
private fun initData() {
@ -95,11 +101,7 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
postEvent(EventBus.UP_CONFIG, true)
}
tv_title_mode.onClick {
requireContext().selector("标题模式", titleModes.toList()) { _, index ->
ReadBookConfig.titleMode = index
tv_title_mode.text = titleModes[index]
postEvent(EventBus.UP_CONFIG, true)
}
showTitleConfig()
}
tv_text_bold.onClick {
ReadBookConfig.apply {
@ -124,16 +126,10 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
dismiss()
callBack?.showPaddingConfig()
}
tv_type.onClick {
dismiss()
callBack?.showTypeConfig()
}
rg_page_anim.onCheckedChange { _, checkedId ->
rg_page_anim.getIndexById(checkedId).let {
ReadBookConfig.pageAnim = it
ReadBookConfig.pageAnim = rg_page_anim.getIndexById(checkedId)
callBack?.page_view?.upPageAnim()
}
}
cb_share_layout.onCheckedChangeListener = { checkBox, isChecked ->
if (checkBox.isPressed) {
ReadBookConfig.shareLayout = isChecked
@ -141,19 +137,20 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
postEvent(EventBus.UP_CONFIG, true)
}
}
iv_default1.onClick {
ReadBookConfig.lineSpacingExtra = 16
ReadBookConfig.paragraphSpacing = 6
dsb_text_size.onChanged = {
ReadBookConfig.textSize = it + 5
postEvent(EventBus.UP_CONFIG, true)
}
iv_default2.onClick {
ReadBookConfig.lineSpacingExtra = 13
ReadBookConfig.paragraphSpacing = 3
dsb_text_letter_spacing.onChanged = {
ReadBookConfig.letterSpacing = (it - 50) / 100f
postEvent(EventBus.UP_CONFIG, true)
}
iv_default3.onClick {
ReadBookConfig.lineSpacingExtra = 10
ReadBookConfig.paragraphSpacing = 0
dsb_line_size.onChanged = {
ReadBookConfig.lineSpacingExtra = it
postEvent(EventBus.UP_CONFIG, true)
}
dsb_paragraph_spacing.onChanged = {
ReadBookConfig.paragraphSpacing = it
postEvent(EventBus.UP_CONFIG, true)
}
bg0.onClick { changeBg(0) }
@ -168,6 +165,33 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
bg4.onLongClick { showBgTextConfig(4) }
}
@SuppressLint("InflateParams")
private fun showTitleConfig() {
requireContext().alert(R.string.title) {
val rootView = LayoutInflater.from(requireContext())
.inflate(R.layout.dialog_title_config, null).apply {
ReadBookConfig.apply {
rg_title_mode.checkByIndex(titleMode)
dsb_title_top.progress = titleTopSpacing
dsb_title_bottom.progress = titleBottomSpacing
rg_title_mode.onCheckedChange { _, checkedId ->
titleMode = rg_title_mode.getIndexById(checkedId)
postEvent(EventBus.UP_CONFIG, true)
}
dsb_title_top.onChanged = {
titleTopSpacing = it
postEvent(EventBus.UP_CONFIG, true)
}
dsb_title_bottom.onChanged = {
titleBottomSpacing = it
postEvent(EventBus.UP_CONFIG, true)
}
}
}
customView = rootView
}.show()
}
private fun changeBg(index: Int) {
if (ReadBookConfig.styleSelect != index) {
ReadBookConfig.styleSelect = index
@ -187,8 +211,11 @@ class ReadStyleDialog : DialogFragment(), FontSelectDialog.CallBack {
private fun upStyle() {
ReadBookConfig.let {
tv_title_mode.text = titleModes.getOrElse(it.titleMode) { titleModes[0] }
tv_text_bold.isSelected = it.textBold
dsb_text_size.progress = it.textSize - 5
dsb_text_letter_spacing.progress = (it.letterSpacing * 100).toInt() + 50
dsb_line_size.progress = it.lineSpacingExtra
dsb_paragraph_spacing.progress = it.paragraphSpacing
}
}

@ -1,91 +0,0 @@
package io.legado.app.ui.book.read.config
import android.content.DialogInterface
import android.os.Bundle
import android.util.DisplayMetrics
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import io.legado.app.R
import io.legado.app.base.BaseDialogFragment
import io.legado.app.constant.EventBus
import io.legado.app.help.ReadBookConfig
import io.legado.app.ui.book.read.Help
import io.legado.app.utils.postEvent
import kotlinx.android.synthetic.main.dialog_read_type.*
class ReadTypeDialog : BaseDialogFragment() {
override fun onStart() {
super.onStart()
val dm = DisplayMetrics()
activity?.let {
Help.upSystemUiVisibility(it)
it.windowManager?.defaultDisplay?.getMetrics(dm)
}
dialog?.window?.let {
val attr = it.attributes
attr.dimAmount = 0.0f
it.attributes = attr
it.setLayout((dm.widthPixels * 0.9).toInt(), ViewGroup.LayoutParams.WRAP_CONTENT)
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.dialog_read_type, container)
}
override fun onDismiss(dialog: DialogInterface) {
super.onDismiss(dialog)
ReadBookConfig.save()
}
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) {
initView()
initViewEvent()
upStyle()
}
private fun initView() {
dsb_text_size.valueFormat = {
(it + 5).toString()
}
dsb_text_letter_spacing.valueFormat = {
((it - 50) / 100f).toString()
}
dsb_line_size.valueFormat = { ((it - 10) / 10f).toString() }
dsb_paragraph_spacing.valueFormat = { (it / 10f).toString() }
}
private fun initViewEvent() {
dsb_text_size.onChanged = {
ReadBookConfig.textSize = it + 5
postEvent(EventBus.UP_CONFIG, true)
}
dsb_text_letter_spacing.onChanged = {
ReadBookConfig.letterSpacing = (it - 50) / 100f
postEvent(EventBus.UP_CONFIG, true)
}
dsb_line_size.onChanged = {
ReadBookConfig.lineSpacingExtra = it
postEvent(EventBus.UP_CONFIG, true)
}
dsb_paragraph_spacing.onChanged = {
ReadBookConfig.paragraphSpacing = it
postEvent(EventBus.UP_CONFIG, true)
}
}
private fun upStyle() {
ReadBookConfig.let {
dsb_text_size.progress = it.textSize - 5
dsb_text_letter_spacing.progress = (it.letterSpacing * 100).toInt() + 50
dsb_line_size.progress = it.lineSpacingExtra
dsb_paragraph_spacing.progress = it.paragraphSpacing
}
}
}

@ -1,12 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:viewportHeight="16"
android:viewportWidth="16"
android:width="24dp">
<path
android:fillColor="#3C3F43"
android:pathData="M0.5,2h15C15.8,2 16,2.2 16,2.5l0,0C16,2.8 15.8,3 15.5,3h-15C0.2,3 0,2.8 0,2.5l0,0C0,2.2 0.2,2 0.5,2z" />
<path
android:fillColor="#3C3F43"
android:pathData="M0.5,13h15c0.3,0 0.5,0.2 0.5,0.5l0,0c0,0.3 -0.2,0.5 -0.5,0.5h-15C0.2,14 0,13.8 0,13.5l0,0C0,13.2 0.2,13 0.5,13z" />
</vector>

@ -1,15 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:viewportHeight="16"
android:viewportWidth="16"
android:width="24dp">
<path
android:fillColor="#3C3F43"
android:pathData="M0.5,2h15C15.8,2 16,2.2 16,2.5l0,0C16,2.8 15.8,3 15.5,3h-15C0.2,3 0,2.8 0,2.5l0,0C0,2.2 0.2,2 0.5,2z" />
<path
android:fillColor="#3C3F43"
android:pathData="M0.5,7.4h15c0.3,0 0.5,0.2 0.5,0.5l0,0c0,0.3 -0.2,0.5 -0.5,0.5h-15C0.2,8.4 0,8.2 0,7.9l0,0C0,7.7 0.2,7.4 0.5,7.4z" />
<path
android:fillColor="#3C3F43"
android:pathData="M0.5,13h15c0.3,0 0.5,0.2 0.5,0.5l0,0c0,0.3 -0.2,0.5 -0.5,0.5h-15C0.2,14 0,13.8 0,13.5l0,0C0,13.2 0.2,13 0.5,13z" />
</vector>

@ -1,18 +0,0 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="24dp"
android:viewportHeight="16"
android:viewportWidth="16"
android:width="24dp">
<path
android:fillColor="#3C3F43"
android:pathData="M0.5,2h15C15.8,2 16,2.2 16,2.5l0,0C16,2.8 15.8,3 15.5,3h-15C0.2,3 0,2.8 0,2.5l0,0C0,2.2 0.2,2 0.5,2z" />
<path
android:fillColor="#3C3F43"
android:pathData="M0.5,5.7h15c0.3,0 0.5,0.2 0.5,0.5l0,0c0,0.3 -0.2,0.5 -0.5,0.5h-15C0.2,6.7 0,6.5 0,6.2l0,0C0,5.9 0.2,5.7 0.5,5.7z" />
<path
android:fillColor="#3C3F43"
android:pathData="M0.5,9.3h15c0.3,0 0.5,0.2 0.5,0.5l0,0c0,0.3 -0.2,0.5 -0.5,0.5h-15C0.2,10.3 0,10.1 0,9.8l0,0C0,9.5 0.2,9.3 0.5,9.3z" />
<path
android:fillColor="#3C3F43"
android:pathData="M0.5,13h15c0.3,0 0.5,0.2 0.5,0.5l0,0c0,0.3 -0.2,0.5 -0.5,0.5h-15C0.2,14 0,13.8 0,13.5l0,0C0,13.2 0.2,13 0.5,13z" />
</vector>

@ -27,7 +27,7 @@
android:paddingRight="6dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:text="@string/title_center"
android:text="@string/title"
android:gravity="center"
android:textSize="14sp" />
@ -98,82 +98,6 @@
android:gravity="center"
android:textSize="14sp" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:gravity="center_vertical">
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5" />
<ImageView
android:id="@+id/iv_default1"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="?attr/selectableItemBackground"
android:tint="@color/tv_text_default"
android:padding="6dp"
android:contentDescription="@string/default1"
android:src="@drawable/ic_type_default_1" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView
android:id="@+id/iv_default2"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="?attr/selectableItemBackground"
android:tint="@color/tv_text_default"
android:padding="6dp"
android:contentDescription="@string/default2"
android:src="@drawable/ic_type_default_2" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ImageView
android:id="@+id/iv_default3"
android:layout_width="30dp"
android:layout_height="30dp"
android:background="?attr/selectableItemBackground"
android:tint="@color/tv_text_default"
android:padding="6dp"
android:contentDescription="@string/default3"
android:src="@drawable/ic_type_default_3" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<io.legado.app.ui.widget.text.StrokeTextView
android:id="@+id/tv_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
android:text="@string/compose_type"
android:gravity="center"
android:textSize="14sp" />
<Space
android:layout_width="0dp"
android:layout_height="wrap_content"
@ -198,6 +122,43 @@
</LinearLayout>
<io.legado.app.ui.widget.DetailSeekBar
android:id="@+id/dsb_text_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:max="45"
app:title="@string/text_size" />
<io.legado.app.ui.widget.DetailSeekBar
android:id="@+id/dsb_text_letter_spacing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:max="100"
app:title="@string/text_letter_spacing" />
<io.legado.app.ui.widget.DetailSeekBar
android:id="@+id/dsb_line_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:max="20"
app:title="@string/line_size" />
<io.legado.app.ui.widget.DetailSeekBar
android:id="@+id/dsb_paragraph_spacing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:max="20"
app:title="@string/paragraph_size" />
<View
android:id="@+id/vw_bg_fg"
android:layout_width="match_parent"
@ -208,6 +169,8 @@
android:id="@+id/tv_page_anim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="3dp"
android:paddingBottom="3dp"
android:paddingStart="6dp"
android:paddingEnd="6dp"
android:text="@string/page_anim" />

@ -1,45 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<io.legado.app.ui.widget.DetailSeekBar
android:id="@+id/dsb_text_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:max="45"
app:title="@string/text_size" />
<io.legado.app.ui.widget.DetailSeekBar
android:id="@+id/dsb_text_letter_spacing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:max="100"
app:title="@string/text_letter_spacing" />
<io.legado.app.ui.widget.DetailSeekBar
android:id="@+id/dsb_line_size"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:max="20"
app:title="@string/line_size" />
<io.legado.app.ui.widget.DetailSeekBar
android:id="@+id/dsb_paragraph_spacing"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
app:max="20"
app:title="@string/paragraph_size" />
</LinearLayout>

@ -0,0 +1,51 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<RadioGroup
android:id="@+id/rg_title_mode"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_title_mode1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="@string/title_left" />
<RadioButton
android:id="@+id/rb_title_mode2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="@string/title_center" />
<RadioButton
android:id="@+id/rb_title_mode3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:text="@string/title_hide" />
</RadioGroup>
<io.legado.app.ui.widget.DetailSeekBar
android:id="@+id/dsb_title_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
app:title="上间距" />
<io.legado.app.ui.widget.DetailSeekBar
android:id="@+id/dsb_title_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
app:title="下间距" />
</LinearLayout>

@ -93,10 +93,4 @@
<item>系统等宽字体</item>
</string-array>
<string-array name="title_mode">
<item>标题靠左</item>
<item>标题居中</item>
<item>标题隐藏</item>
</string-array>
</resources>

@ -620,7 +620,6 @@
<string name="select_end">文本选择结束位置</string>
<string name="select_start">文本选择开始位置</string>
<string name="share_layout">共用布局</string>
<string name="title_center">标题居中</string>
<string name="browser">浏览器</string>
<string name="import_default_rule">导入默认规则</string>
<string name="name">名称</string>
@ -634,4 +633,8 @@
<string name="default1">预设一</string>
<string name="default2">预设二</string>
<string name="default3">预设三</string>
<string name="title">标题</string>
<string name="title_left">靠左</string>
<string name="title_center">居中</string>
<string name="title_hide">隐藏</string>
</resources>

Loading…
Cancel
Save