feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 80c242f4ad
commit 0df04c2e82
  1. 2
      app/src/main/java/io/legado/app/data/entities/TxtTocRule.kt
  2. 44
      app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt
  3. 4
      app/src/main/java/io/legado/app/ui/widget/DetailSeekBar.kt
  4. 31
      app/src/main/res/layout/dialog_toc_regex_edit.xml
  5. 1
      app/src/main/res/layout/item_book_source.xml
  6. 41
      app/src/main/res/layout/item_toc_regex.xml
  7. 6
      app/src/main/res/layout/view_detail_seek_bar.xml
  8. 4
      app/src/main/res/layout/view_select_action_bar.xml
  9. 5
      app/src/main/res/values/strings.xml

@ -9,6 +9,6 @@ data class TxtTocRule(
@PrimaryKey @PrimaryKey
var name: String = "", var name: String = "",
var rule: String = "", var rule: String = "",
var serialNumber: Int, var serialNumber: Int = -1,
var enable: Boolean = true var enable: Boolean = true
) )

@ -1,5 +1,6 @@
package io.legado.app.ui.book.read.config package io.legado.app.ui.book.read.config
import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.util.DisplayMetrics import android.util.DisplayMetrics
@ -21,10 +22,16 @@ import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.constant.Theme import io.legado.app.constant.Theme
import io.legado.app.data.entities.TxtTocRule import io.legado.app.data.entities.TxtTocRule
import io.legado.app.help.ItemTouchCallback import io.legado.app.help.ItemTouchCallback
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.cancelButton
import io.legado.app.lib.dialogs.customView
import io.legado.app.lib.dialogs.okButton
import io.legado.app.model.localBook.AnalyzeTxtFile import io.legado.app.model.localBook.AnalyzeTxtFile
import io.legado.app.utils.applyTint import io.legado.app.utils.applyTint
import io.legado.app.utils.getVerticalDivider import io.legado.app.utils.getVerticalDivider
import kotlinx.android.synthetic.main.dialog_toc_regex.* import kotlinx.android.synthetic.main.dialog_toc_regex.*
import kotlinx.android.synthetic.main.dialog_toc_regex_edit.*
import kotlinx.android.synthetic.main.dialog_toc_regex_edit.view.*
import kotlinx.android.synthetic.main.item_toc_regex.view.* import kotlinx.android.synthetic.main.item_toc_regex.view.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -114,8 +121,40 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
} }
} }
@SuppressLint("InflateParams")
private fun editRule(rule: TxtTocRule? = null) { private fun editRule(rule: TxtTocRule? = null) {
val tocRule = rule?.copy() ?: TxtTocRule()
requireContext().alert(titleResource = R.string.txt_toc_regex) {
var rootView: View? = null
customView {
LayoutInflater.from(requireContext())
.inflate(R.layout.dialog_toc_regex_edit, null).apply {
rootView = this
tv_rule_name.setText(tocRule.name)
tv_rule_regex.setText(tocRule.rule)
}
}
okButton {
rootView?.let {
tocRule.name = tv_rule_name.text.toString()
tocRule.rule = tv_rule_regex.text.toString()
saveRule(rule, tocRule)
}
}
cancelButton()
}.show().applyTint()
}
private fun saveRule(oldRule: TxtTocRule?, rule: TxtTocRule) {
launch(IO) {
if (rule.serialNumber < 0) {
rule.serialNumber = adapter.getItems().lastOrNull()?.serialNumber ?: 0 + 1
}
oldRule?.let {
App.db.txtTocRule().delete(oldRule)
}
App.db.txtTocRule().insert(rule)
}
} }
inner class TocRegexAdapter(context: Context) : inner class TocRegexAdapter(context: Context) :
@ -127,6 +166,7 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
if (payloads.isEmpty()) { if (payloads.isEmpty()) {
rb_regex_name.text = item.name rb_regex_name.text = item.name
rb_regex_name.isChecked = item.name == selectedName rb_regex_name.isChecked = item.name == selectedName
swt_enabled.isChecked = item.enable
} else { } else {
rb_regex_name.isChecked = item.name == selectedName rb_regex_name.isChecked = item.name == selectedName
} }
@ -141,10 +181,10 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
updateItems(0, itemCount - 1, true) updateItems(0, itemCount - 1, true)
} }
} }
tv_edit.onClick { iv_edit.onClick {
editRule(getItem(holder.layoutPosition)) editRule(getItem(holder.layoutPosition))
} }
tv_del.onClick { iv_delete.onClick {
getItem(holder.layoutPosition)?.let { item -> getItem(holder.layoutPosition)?.let { item ->
launch(IO) { launch(IO) {
App.db.txtTocRule().delete(item) App.db.txtTocRule().delete(item)

@ -34,11 +34,11 @@ class DetailSeekBar(context: Context, attrs: AttributeSet?) : FrameLayout(contex
seek_bar.max = typedArray.getInteger(R.styleable.DetailSeekBar_max, 0) seek_bar.max = typedArray.getInteger(R.styleable.DetailSeekBar_max, 0)
typedArray.recycle() typedArray.recycle()
iv_seek_add.onClick { iv_seek_plus.onClick {
seek_bar.progressAdd(1) seek_bar.progressAdd(1)
onChanged?.invoke(seek_bar.progress) onChanged?.invoke(seek_bar.progress)
} }
iv_seek_remove.onClick { iv_seek_reduce.onClick {
seek_bar.progressAdd(-1) seek_bar.progressAdd(-1)
onChanged?.invoke(seek_bar.progress) onChanged?.invoke(seek_bar.progress)
} }

@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<io.legado.app.ui.widget.text.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name">
<io.legado.app.ui.widget.text.EditText
android:id="@+id/tv_rule_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</io.legado.app.ui.widget.text.TextInputLayout>
<io.legado.app.ui.widget.text.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/regex">
<io.legado.app.ui.widget.text.EditText
android:id="@+id/tv_rule_regex"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</io.legado.app.ui.widget.text.TextInputLayout>
</LinearLayout>

@ -62,6 +62,7 @@
android:id="@+id/iv_explore" android:id="@+id/iv_explore"
android:layout_width="8dp" android:layout_width="8dp"
android:layout_height="8dp" android:layout_height="8dp"
android:contentDescription="@string/more_menu"
android:src="@color/md_green_600" android:src="@color/md_green_600"
app:layout_constraintRight_toRightOf="parent" app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:background="@color/background" android:background="@color/background"
@ -14,20 +15,34 @@
android:layout_weight="1" android:layout_weight="1"
android:maxLines="1" /> android:maxLines="1" />
<TextView <io.legado.app.lib.theme.view.ATESwitch
android:id="@+id/tv_edit" android:id="@+id/swt_enabled"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content" />
android:background="?attr/selectableItemBackground"
android:padding="8dp"
android:text="@string/edit" />
<TextView <androidx.appcompat.widget.AppCompatImageView
android:id="@+id/tv_del" android:id="@+id/iv_edit"
android:layout_width="wrap_content" android:layout_width="36dp"
android:layout_height="wrap_content" android:layout_height="36dp"
android:background="?attr/selectableItemBackground" android:layout_gravity="center"
android:padding="8dp" android:background="?android:attr/selectableItemBackgroundBorderless"
android:text="@string/delete" /> android:contentDescription="@string/edit"
android:padding="6dp"
android:tooltipText="@string/edit"
android:src="@drawable/ic_edit"
android:tint="@color/tv_text_default"
tools:ignore="UnusedAttribute" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_delete"
android:layout_width="36dp"
android:layout_height="36dp"
android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/more_menu"
android:tooltipText="@string/more_menu"
android:padding="6dp"
android:src="@drawable/ic_clear_all"
android:tint="@color/tv_text_default"
tools:ignore="UnusedAttribute" />
</LinearLayout> </LinearLayout>

@ -15,9 +15,10 @@
tools:ignore="HardcodedText" /> tools:ignore="HardcodedText" />
<androidx.appcompat.widget.AppCompatImageView <androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_seek_remove" android:id="@+id/iv_seek_reduce"
android:layout_width="24dp" android:layout_width="24dp"
android:layout_height="24dp" android:layout_height="24dp"
android:contentDescription="@string/reduce"
android:src="@drawable/ic_remove" android:src="@drawable/ic_remove"
android:background="?android:attr/selectableItemBackgroundBorderless" android:background="?android:attr/selectableItemBackgroundBorderless"
android:tint="@color/tv_text_default" /> android:tint="@color/tv_text_default" />
@ -31,9 +32,10 @@
android:max="10" /> android:max="10" />
<androidx.appcompat.widget.AppCompatImageView <androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_seek_add" android:id="@+id/iv_seek_plus"
android:layout_width="24dp" android:layout_width="24dp"
android:layout_height="24dp" android:layout_height="24dp"
android:contentDescription="@string/plus"
android:src="@drawable/ic_add" android:src="@drawable/ic_add"
android:background="?android:attr/selectableItemBackgroundBorderless" android:background="?android:attr/selectableItemBackgroundBorderless"
android:tint="@color/tv_text_default" /> android:tint="@color/tv_text_default" />

@ -46,10 +46,12 @@
android:layout_width="36dp" android:layout_width="36dp"
android:layout_height="36dp" android:layout_height="36dp"
android:background="?attr/selectableItemBackgroundBorderless" android:background="?attr/selectableItemBackgroundBorderless"
android:contentDescription="@string/more_menu"
android:tooltipText="@string/more_menu"
android:padding="6dp" android:padding="6dp"
android:src="@drawable/ic_more_vert" android:src="@drawable/ic_more_vert"
android:tint="@color/tv_text_default" android:tint="@color/tv_text_default"
android:visibility="gone" android:visibility="gone"
tools:ignore="RtlHardcoded" /> tools:ignore="RtlHardcoded,UnusedAttribute" />
</LinearLayout> </LinearLayout>

@ -621,4 +621,9 @@
<string name="title_center">标题居中</string> <string name="title_center">标题居中</string>
<string name="browser">浏览器</string> <string name="browser">浏览器</string>
<string name="import_default_rule">导入默认规则</string> <string name="import_default_rule">导入默认规则</string>
<string name="name">名称</string>
<string name="regex">正则</string>
<string name="more_menu">更多菜单</string>
<string name="reduce"></string>
<string name="plus"></string>
</resources> </resources>

Loading…
Cancel
Save