pull/32/head
kunfei 5 years ago
parent f6e28c6036
commit ab83e12c0f
  1. 6
      app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt
  2. 32
      app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt
  3. 17
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt
  4. 90
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt
  5. 4
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt

@ -1,7 +1,6 @@
package io.legado.app.data.dao
import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import androidx.room.*
import io.legado.app.data.entities.ReplaceRule
@ -10,10 +9,7 @@ import io.legado.app.data.entities.ReplaceRule
interface ReplaceRuleDao {
@Query("SELECT * FROM replace_rules ORDER BY sortOrder ASC")
fun observeAll(): DataSource.Factory<Int, ReplaceRule>
@Query("SELECT id FROM replace_rules ORDER BY sortOrder ASC")
fun observeAllIds(): LiveData<List<Long>>
fun liveDataAll(): LiveData<List<ReplaceRule>>
@get:Query("SELECT MIN(sortOrder) FROM replace_rules")
val minOrder: Int

@ -0,0 +1,32 @@
package io.legado.app.ui.replacerule
import androidx.recyclerview.widget.DiffUtil
import io.legado.app.data.entities.ReplaceRule
class DiffCallBack(
private val oldItems: List<ReplaceRule>,
private val newItems: List<ReplaceRule>
) : DiffUtil.Callback() {
override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return oldItem.id == newItem.id
}
override fun getOldListSize(): Int {
return oldItems.size
}
override fun getNewListSize(): Int {
return newItems.size
}
override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return oldItem.name == newItem.name
&& oldItem.isEnabled == newItem.isEnabled
}
}

@ -6,10 +6,8 @@ import android.view.MenuItem
import android.view.SubMenu
import androidx.appcompat.widget.SearchView
import androidx.core.content.ContextCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.paging.LivePagedListBuilder
import androidx.paging.PagedList
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
@ -34,7 +32,6 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
get() = getViewModel(ReplaceRuleViewModel::class.java)
private lateinit var adapter: ReplaceRuleAdapter
private var rulesLiveData: LiveData<PagedList<ReplaceRule>>? = null
private var allEnabled = false
private var groups = hashSetOf<String>()
private var groupMenu: SubMenu? = null
@ -92,9 +89,11 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
}
private fun initDataObservers() {
rulesLiveData?.removeObservers(this)
rulesLiveData = LivePagedListBuilder(App.db.replaceRuleDao().observeAll(), 30).build()
rulesLiveData?.observe(this, Observer<PagedList<ReplaceRule>> { adapter.submitList(it) })
App.db.replaceRuleDao().liveDataAll().observe(this, Observer {
val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), it))
adapter.setItemsNoNotify(it)
diffResult.dispatchUpdatesTo(adapter)
})
App.db.replaceRuleDao().liveGroup().observe(this, Observer {
groups.clear()
@ -128,8 +127,8 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
return false
}
override fun update(rule: ReplaceRule) {
viewModel.update(rule)
override fun update(vararg rule: ReplaceRule) {
viewModel.update(*rule)
}
override fun delete(rule: ReplaceRule) {

@ -1,15 +1,11 @@
package io.legado.app.ui.replacerule
import android.content.Context
import android.view.LayoutInflater
import android.view.Menu
import android.view.View
import android.view.ViewGroup
import android.widget.PopupMenu
import androidx.paging.PagedListAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import io.legado.app.R
import io.legado.app.base.adapter.ItemViewHolder
import io.legado.app.base.adapter.SimpleRecyclerAdapter
import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.ItemTouchCallback
import io.legado.app.lib.theme.backgroundColor
@ -18,69 +14,29 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
PagedListAdapter<ReplaceRule, ReplaceRuleAdapter.MyViewHolder>(DIFF_CALLBACK),
SimpleRecyclerAdapter<ReplaceRule>(context, R.layout.item_replace_rule),
ItemTouchCallback.OnItemTouchCallbackListener {
companion object {
@JvmField
val DIFF_CALLBACK = object : DiffUtil.ItemCallback<ReplaceRule>() {
override fun areItemsTheSame(oldItem: ReplaceRule, newItem: ReplaceRule): Boolean =
oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: ReplaceRule, newItem: ReplaceRule): Boolean =
oldItem.id == newItem.id
&& oldItem.pattern == newItem.pattern
&& oldItem.replacement == newItem.replacement
&& oldItem.isRegex == newItem.isRegex
&& oldItem.isEnabled == newItem.isEnabled
&& oldItem.scope == newItem.scope
}
}
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
return true
}
override fun onSwiped(adapterPosition: Int) {
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
return MyViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.item_replace_rule,
parent,
false
)
)
}
override fun onBindViewHolder(holder: MyViewHolder, pos: Int) {
getItem(pos)?.let { holder.bind(it, callBack) }
}
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
fun bind(rule: ReplaceRule, callBack: CallBack) = with(itemView) {
override fun convert(holder: ItemViewHolder, item: ReplaceRule, payloads: MutableList<Any>) {
with(holder.itemView) {
this.setBackgroundColor(context.backgroundColor)
cb_name.text = rule.name
swt_enabled.isChecked = rule.isEnabled
cb_name.text = item.name
swt_enabled.isChecked = item.isEnabled
swt_enabled.onClick {
rule.isEnabled = swt_enabled.isChecked
callBack.update(rule)
item.isEnabled = swt_enabled.isChecked
callBack.update(item)
}
iv_edit.onClick {
callBack.edit(rule)
callBack.edit(item)
}
iv_menu_more.onClick {
val popupMenu = PopupMenu(context, it)
popupMenu.menu.add(Menu.NONE, R.id.menu_top, Menu.NONE, R.string.to_top)
popupMenu.menu.add(Menu.NONE, R.id.menu_del, Menu.NONE, R.string.delete)
popupMenu.setOnMenuItemClickListener { item ->
when (item.itemId) {
R.id.menu_top -> callBack.toTop(rule)
R.id.menu_del -> callBack.delete(rule)
popupMenu.setOnMenuItemClickListener { menuItem ->
when (menuItem.itemId) {
R.id.menu_top -> callBack.toTop(item)
R.id.menu_del -> callBack.delete(item)
}
true
}
@ -89,8 +45,24 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
}
}
override fun onMove(srcPosition: Int, targetPosition: Int): Boolean {
val srcItem = getItem(srcPosition)
val targetItem = getItem(targetPosition)
if (srcItem != null && targetItem != null) {
val srcOrder = srcItem.order
srcItem.order = targetItem.order
targetItem.order = srcOrder
callBack.update(srcItem, targetItem)
}
return true
}
override fun onSwiped(adapterPosition: Int) {
}
interface CallBack {
fun update(rule: ReplaceRule)
fun update(vararg rule: ReplaceRule)
fun delete(rule: ReplaceRule)
fun edit(rule: ReplaceRule)
fun toTop(rule: ReplaceRule)

@ -10,9 +10,9 @@ import io.legado.app.utils.splitNotBlank
class ReplaceRuleViewModel(application: Application) : BaseViewModel(application) {
fun update(rule: ReplaceRule) {
fun update(vararg rule: ReplaceRule) {
execute {
App.db.replaceRuleDao().update(rule)
App.db.replaceRuleDao().update(*rule)
}
}

Loading…
Cancel
Save