From ab83e12c0ff417b5e1269ef928475bd9b43ffe86 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 7 Sep 2019 22:58:11 +0800 Subject: [PATCH] up --- .../io/legado/app/data/dao/ReplaceRuleDao.kt | 6 +- .../legado/app/ui/replacerule/DiffCallBack.kt | 32 +++++++ .../app/ui/replacerule/ReplaceRuleActivity.kt | 17 ++-- .../app/ui/replacerule/ReplaceRuleAdapter.kt | 90 +++++++------------ .../ui/replacerule/ReplaceRuleViewModel.kt | 4 +- 5 files changed, 74 insertions(+), 75 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt diff --git a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt index 907b96d71..30e2522e9 100644 --- a/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.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 - - @Query("SELECT id FROM replace_rules ORDER BY sortOrder ASC") - fun observeAllIds(): LiveData> + fun liveDataAll(): LiveData> @get:Query("SELECT MIN(sortOrder) FROM replace_rules") val minOrder: Int diff --git a/app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt b/app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt new file mode 100644 index 000000000..fc1f33b11 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt @@ -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, + private val newItems: List +) : 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 + } + + +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index c839da8bf..8515aa180 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -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(R.layout.activi get() = getViewModel(ReplaceRuleViewModel::class.java) private lateinit var adapter: ReplaceRuleAdapter - private var rulesLiveData: LiveData>? = null private var allEnabled = false private var groups = hashSetOf() private var groupMenu: SubMenu? = null @@ -92,9 +89,11 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi } private fun initDataObservers() { - rulesLiveData?.removeObservers(this) - rulesLiveData = LivePagedListBuilder(App.db.replaceRuleDao().observeAll(), 30).build() - rulesLiveData?.observe(this, Observer> { 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(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) { diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt index 55b9d6c3c..92483c7d8 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt @@ -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(DIFF_CALLBACK), + SimpleRecyclerAdapter(context, R.layout.item_replace_rule), ItemTouchCallback.OnItemTouchCallbackListener { - companion object { - - @JvmField - val DIFF_CALLBACK = object : DiffUtil.ItemCallback() { - 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) { + 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) diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index 85f7cf430..4674ab415 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -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) } }