|
|
|
@ -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<List<TxtTocRule>>? = 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<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 |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>) { |
|
|
|
|
holder.itemView.apply { |
|
|
|
|
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 |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|