feat: 优化代码

pull/115/head
kunfei 5 years ago
parent 1a5c9bfacf
commit 80a587f8c4
  1. 3
      app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt
  2. 2
      app/src/main/java/io/legado/app/help/ItemTouchCallback.kt
  3. 72
      app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt
  4. 11
      app/src/main/res/menu/txt_toc_regex.xml
  5. 1
      app/src/main/res/values/strings.xml

@ -19,6 +19,9 @@ interface TxtTocRuleDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(vararg rule: TxtTocRule) fun insert(vararg rule: TxtTocRule)
@Update(onConflict = OnConflictStrategy.REPLACE)
fun update(vararg rule: TxtTocRule)
@Delete @Delete
fun delete(vararg rule: TxtTocRule) fun delete(vararg rule: TxtTocRule)
} }

@ -128,7 +128,7 @@ class ItemTouchCallback : ItemTouchHelper.Callback() {
* @return 开发者处理了操作应该返回true开发者没有处理就返回false * @return 开发者处理了操作应该返回true开发者没有处理就返回false
*/ */
fun onMove(srcPosition: Int, targetPosition: Int): Boolean { fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
return false return true
} }
fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {

@ -12,6 +12,7 @@ import androidx.fragment.app.FragmentManager
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.App import io.legado.app.App
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.BaseDialogFragment import io.legado.app.base.BaseDialogFragment
@ -19,16 +20,22 @@ import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter 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.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.item_toc_regex.view.* import kotlinx.android.synthetic.main.item_toc_regex.view.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch
import java.util.*
class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
private lateinit var adapter: TocRegexAdapter private lateinit var adapter: TocRegexAdapter
private var tocRegexLiveData: LiveData<List<TxtTocRule>>? = null private var tocRegexLiveData: LiveData<List<TxtTocRule>>? = null
var selectedName: String? = null
var durRegex: String? = null
override fun onStart() { override fun onStart() {
super.onStart() super.onStart()
@ -47,6 +54,7 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
durRegex = arguments?.getString("tocRegex")
tool_bar.setTitle(R.string.txt_toc_regex) tool_bar.setTitle(R.string.txt_toc_regex)
tool_bar.inflateMenu(R.menu.txt_toc_regex) tool_bar.inflateMenu(R.menu.txt_toc_regex)
tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) tool_bar.menu.applyTint(requireContext(), Theme.getTheme())
@ -60,33 +68,89 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener {
recycler_view.layoutManager = LinearLayoutManager(requireContext()) recycler_view.layoutManager = LinearLayoutManager(requireContext())
recycler_view.addItemDecoration(recycler_view.getVerticalDivider()) recycler_view.addItemDecoration(recycler_view.getVerticalDivider())
recycler_view.adapter = adapter recycler_view.adapter = adapter
val itemTouchCallback = ItemTouchCallback()
itemTouchCallback.onItemTouchCallbackListener = adapter
itemTouchCallback.isCanDrag = true
} }
private fun initData() { private fun initData() {
tocRegexLiveData?.removeObservers(viewLifecycleOwner) tocRegexLiveData?.removeObservers(viewLifecycleOwner)
tocRegexLiveData = App.db.txtTocRule().observeAll() tocRegexLiveData = App.db.txtTocRule().observeAll()
tocRegexLiveData?.observe(viewLifecycleOwner, Observer { tocRegexLiveData?.observe(viewLifecycleOwner, Observer { tocRules ->
adapter.setItems(it) initSelectedName(tocRules)
adapter.setItems(tocRules)
}) })
} }
override fun onMenuItemClick(item: MenuItem?): Boolean { private fun initSelectedName(tocRules: List<TxtTocRule>) {
if (selectedName == null && durRegex != null) {
tocRules.forEach {
if (durRegex == it.rule) {
selectedName = it.name
return@forEach
}
}
if (selectedName == null) {
selectedName = ""
}
}
}
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_add -> {
}
R.id.menu_default -> {
}
}
return false return false
} }
inner class TocRegexAdapter(context: Context) : inner class TocRegexAdapter(context: Context) :
SimpleRecyclerAdapter<TxtTocRule>(context, R.layout.item_toc_regex) { SimpleRecyclerAdapter<TxtTocRule>(context, R.layout.item_toc_regex),
ItemTouchCallback.OnItemTouchCallbackListener {
override fun convert(holder: ItemViewHolder, item: TxtTocRule, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: TxtTocRule, payloads: MutableList<Any>) {
holder.itemView.apply { holder.itemView.apply {
if (payloads.isEmpty()) {
rb_regex_name.text = item.name rb_regex_name.text = item.name
rb_regex_name.isChecked = item.name == selectedName
} else {
rb_regex_name.isChecked = item.name == selectedName
}
} }
} }
override fun registerListener(holder: ItemViewHolder) { override fun registerListener(holder: ItemViewHolder) {
holder.itemView.rb_regex_name.setOnCheckedChangeListener { buttonView, isChecked ->
if (buttonView.isPressed && isChecked) {
selectedName = getItem(holder.layoutPosition)?.name
updateItems(0, itemCount - 1, true)
}
}
}
private var isMoved = false
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
Collections.swap(getItems(), srcPosition, targetPosition)
notifyItemMoved(srcPosition, targetPosition)
isMoved = true
return super.onMove(srcPosition, targetPosition)
}
override fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) {
super.clearView(recyclerView, viewHolder)
if (isMoved) {
for ((index, item) in getItems().withIndex()) {
item.serialNumber = index + 1
}
launch(IO) {
App.db.txtTocRule().update(*getItems().toTypedArray())
}
}
isMoved = false
} }
} }

@ -1,11 +1,18 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" <menu xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item <item
android:id="@+id/menu_add" android:id="@+id/menu_add"
android:title="@string/add" android:title="@string/add"
android:icon="@drawable/ic_add" android:icon="@drawable/ic_add"
app:showAsAction="always" /> app:showAsAction="always"
tools:ignore="AlwaysShowAction" />
<item
android:id="@+id/menu_default"
android:title="@string/import_default_rule"
app:showAsAction="never" />
</menu> </menu>

@ -620,4 +620,5 @@
<string name="share_layout">共用布局</string> <string name="share_layout">共用布局</string>
<string name="title_center">标题居中</string> <string name="title_center">标题居中</string>
<string name="browser">浏览器</string> <string name="browser">浏览器</string>
<string name="import_default_rule">导入默认规则</string>
</resources> </resources>

Loading…
Cancel
Save