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 package io.legado.app.data.dao
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.paging.DataSource
import androidx.room.* import androidx.room.*
import io.legado.app.data.entities.ReplaceRule import io.legado.app.data.entities.ReplaceRule
@ -10,10 +9,7 @@ import io.legado.app.data.entities.ReplaceRule
interface ReplaceRuleDao { interface ReplaceRuleDao {
@Query("SELECT * FROM replace_rules ORDER BY sortOrder ASC") @Query("SELECT * FROM replace_rules ORDER BY sortOrder ASC")
fun observeAll(): DataSource.Factory<Int, ReplaceRule> fun liveDataAll(): LiveData<List<ReplaceRule>>
@Query("SELECT id FROM replace_rules ORDER BY sortOrder ASC")
fun observeAllIds(): LiveData<List<Long>>
@get:Query("SELECT MIN(sortOrder) FROM replace_rules") @get:Query("SELECT MIN(sortOrder) FROM replace_rules")
val minOrder: Int 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 android.view.SubMenu
import androidx.appcompat.widget.SearchView import androidx.appcompat.widget.SearchView
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.paging.LivePagedListBuilder import androidx.recyclerview.widget.DiffUtil
import androidx.paging.PagedList
import androidx.recyclerview.widget.DividerItemDecoration import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.ItemTouchHelper import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -34,7 +32,6 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
get() = getViewModel(ReplaceRuleViewModel::class.java) get() = getViewModel(ReplaceRuleViewModel::class.java)
private lateinit var adapter: ReplaceRuleAdapter private lateinit var adapter: ReplaceRuleAdapter
private var rulesLiveData: LiveData<PagedList<ReplaceRule>>? = null
private var allEnabled = false private var allEnabled = false
private var groups = hashSetOf<String>() private var groups = hashSetOf<String>()
private var groupMenu: SubMenu? = null private var groupMenu: SubMenu? = null
@ -92,9 +89,11 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
} }
private fun initDataObservers() { private fun initDataObservers() {
rulesLiveData?.removeObservers(this) App.db.replaceRuleDao().liveDataAll().observe(this, Observer {
rulesLiveData = LivePagedListBuilder(App.db.replaceRuleDao().observeAll(), 30).build() val diffResult = DiffUtil.calculateDiff(DiffCallBack(adapter.getItems(), it))
rulesLiveData?.observe(this, Observer<PagedList<ReplaceRule>> { adapter.submitList(it) }) adapter.setItemsNoNotify(it)
diffResult.dispatchUpdatesTo(adapter)
})
App.db.replaceRuleDao().liveGroup().observe(this, Observer { App.db.replaceRuleDao().liveGroup().observe(this, Observer {
groups.clear() groups.clear()
@ -128,8 +127,8 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
return false return false
} }
override fun update(rule: ReplaceRule) { override fun update(vararg rule: ReplaceRule) {
viewModel.update(rule) viewModel.update(*rule)
} }
override fun delete(rule: ReplaceRule) { override fun delete(rule: ReplaceRule) {

@ -1,15 +1,11 @@
package io.legado.app.ui.replacerule package io.legado.app.ui.replacerule
import android.content.Context import android.content.Context
import android.view.LayoutInflater
import android.view.Menu import android.view.Menu
import android.view.View
import android.view.ViewGroup
import android.widget.PopupMenu 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.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.data.entities.ReplaceRule
import io.legado.app.help.ItemTouchCallback import io.legado.app.help.ItemTouchCallback
import io.legado.app.lib.theme.backgroundColor 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) : class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
PagedListAdapter<ReplaceRule, ReplaceRuleAdapter.MyViewHolder>(DIFF_CALLBACK), SimpleRecyclerAdapter<ReplaceRule>(context, R.layout.item_replace_rule),
ItemTouchCallback.OnItemTouchCallbackListener { ItemTouchCallback.OnItemTouchCallbackListener {
companion object { override fun convert(holder: ItemViewHolder, item: ReplaceRule, payloads: MutableList<Any>) {
with(holder.itemView) {
@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) {
this.setBackgroundColor(context.backgroundColor) this.setBackgroundColor(context.backgroundColor)
cb_name.text = rule.name cb_name.text = item.name
swt_enabled.isChecked = rule.isEnabled swt_enabled.isChecked = item.isEnabled
swt_enabled.onClick { swt_enabled.onClick {
rule.isEnabled = swt_enabled.isChecked item.isEnabled = swt_enabled.isChecked
callBack.update(rule) callBack.update(item)
} }
iv_edit.onClick { iv_edit.onClick {
callBack.edit(rule) callBack.edit(item)
} }
iv_menu_more.onClick { iv_menu_more.onClick {
val popupMenu = PopupMenu(context, it) 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_top, Menu.NONE, R.string.to_top)
popupMenu.menu.add(Menu.NONE, R.id.menu_del, Menu.NONE, R.string.delete) popupMenu.menu.add(Menu.NONE, R.id.menu_del, Menu.NONE, R.string.delete)
popupMenu.setOnMenuItemClickListener { item -> popupMenu.setOnMenuItemClickListener { menuItem ->
when (item.itemId) { when (menuItem.itemId) {
R.id.menu_top -> callBack.toTop(rule) R.id.menu_top -> callBack.toTop(item)
R.id.menu_del -> callBack.delete(rule) R.id.menu_del -> callBack.delete(item)
} }
true 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 { interface CallBack {
fun update(rule: ReplaceRule) fun update(vararg rule: ReplaceRule)
fun delete(rule: ReplaceRule) fun delete(rule: ReplaceRule)
fun edit(rule: ReplaceRule) fun edit(rule: ReplaceRule)
fun toTop(rule: ReplaceRule) fun toTop(rule: ReplaceRule)

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

Loading…
Cancel
Save