diff --git a/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt index c63133164..fea5818fa 100644 --- a/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/TxtTocRuleDao.kt @@ -19,6 +19,9 @@ interface TxtTocRuleDao { @Insert(onConflict = OnConflictStrategy.REPLACE) fun insert(vararg rule: TxtTocRule) + @Update(onConflict = OnConflictStrategy.REPLACE) + fun update(vararg rule: TxtTocRule) + @Delete fun delete(vararg rule: TxtTocRule) } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt b/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt index 0e9a29351..99ba74ef1 100644 --- a/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt +++ b/app/src/main/java/io/legado/app/help/ItemTouchCallback.kt @@ -128,7 +128,7 @@ class ItemTouchCallback : ItemTouchHelper.Callback() { * @return 开发者处理了操作应该返回true,开发者没有处理就返回false */ fun onMove(srcPosition: Int, targetPosition: Int): Boolean { - return false + return true } fun clearView(recyclerView: RecyclerView, viewHolder: RecyclerView.ViewHolder) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt index 4359b15d5..04185bcdb 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/config/TocRegexDialog.kt @@ -12,6 +12,7 @@ import androidx.fragment.app.FragmentManager import androidx.lifecycle.LiveData import androidx.lifecycle.Observer import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView import io.legado.app.App import io.legado.app.R 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.constant.Theme import io.legado.app.data.entities.TxtTocRule +import io.legado.app.help.ItemTouchCallback import io.legado.app.utils.applyTint import io.legado.app.utils.getVerticalDivider import kotlinx.android.synthetic.main.dialog_toc_regex.* 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 { private lateinit var adapter: TocRegexAdapter private var tocRegexLiveData: LiveData>? = null + var selectedName: String? = null + var durRegex: String? = null override fun onStart() { super.onStart() @@ -47,6 +54,7 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + durRegex = arguments?.getString("tocRegex") tool_bar.setTitle(R.string.txt_toc_regex) tool_bar.inflateMenu(R.menu.txt_toc_regex) tool_bar.menu.applyTint(requireContext(), Theme.getTheme()) @@ -60,33 +68,89 @@ class TocRegexDialog : BaseDialogFragment(), Toolbar.OnMenuItemClickListener { recycler_view.layoutManager = LinearLayoutManager(requireContext()) recycler_view.addItemDecoration(recycler_view.getVerticalDivider()) recycler_view.adapter = adapter + val itemTouchCallback = ItemTouchCallback() + itemTouchCallback.onItemTouchCallbackListener = adapter + itemTouchCallback.isCanDrag = true } private fun initData() { tocRegexLiveData?.removeObservers(viewLifecycleOwner) tocRegexLiveData = App.db.txtTocRule().observeAll() - tocRegexLiveData?.observe(viewLifecycleOwner, Observer { - adapter.setItems(it) + tocRegexLiveData?.observe(viewLifecycleOwner, Observer { tocRules -> + initSelectedName(tocRules) + adapter.setItems(tocRules) }) } - override fun onMenuItemClick(item: MenuItem?): Boolean { + private fun initSelectedName(tocRules: List) { + 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 } inner class TocRegexAdapter(context: Context) : - SimpleRecyclerAdapter(context, R.layout.item_toc_regex) { + SimpleRecyclerAdapter(context, R.layout.item_toc_regex), + ItemTouchCallback.OnItemTouchCallbackListener { override fun convert(holder: ItemViewHolder, item: TxtTocRule, payloads: MutableList) { holder.itemView.apply { - rb_regex_name.text = item.name + if (payloads.isEmpty()) { + 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) { + 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 } } diff --git a/app/src/main/res/menu/txt_toc_regex.xml b/app/src/main/res/menu/txt_toc_regex.xml index d2797eff5..38a50e8cc 100644 --- a/app/src/main/res/menu/txt_toc_regex.xml +++ b/app/src/main/res/menu/txt_toc_regex.xml @@ -1,11 +1,18 @@ - + app:showAsAction="always" + tools:ignore="AlwaysShowAction" /> + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a19395ca8..0efb23246 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -620,4 +620,5 @@ 共用布局 标题居中 浏览器 + 导入默认规则