diff --git a/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt b/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt index 679ba6f89..a87ffb58a 100644 --- a/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt +++ b/app/src/main/java/io/legado/app/base/adapter/CommonRecyclerAdapter.kt @@ -5,6 +5,7 @@ import android.util.SparseArray import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.GridLayoutManager import androidx.recyclerview.widget.RecyclerView import java.util.* @@ -122,7 +123,7 @@ abstract class CommonRecyclerAdapter(protected val context: Context) : Rec } } - fun setItems(items: List?, notify: Boolean = true) { + fun setItems(items: List?) { synchronized(lock) { if (this.items.isNotEmpty()) { this.items.clear() @@ -130,9 +131,19 @@ abstract class CommonRecyclerAdapter(protected val context: Context) : Rec if (items != null) { this.items.addAll(items) } - if (notify) { - notifyDataSetChanged() + notifyDataSetChanged() + } + } + + fun setItems(items: List?, diffResult: DiffUtil.DiffResult) { + synchronized(lock) { + if (this.items.isNotEmpty()) { + this.items.clear() + } + if (items != null) { + this.items.addAll(items) } + diffResult.dispatchUpdatesTo(this) } } diff --git a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt index 2de19d314..33ad0aa46 100644 --- a/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt +++ b/app/src/main/java/io/legado/app/ui/book/group/GroupManageDialog.kt @@ -82,8 +82,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener { App.db.bookGroupDao().liveDataAll().observe(viewLifecycleOwner, Observer { val diffResult = DiffUtil.calculateDiff(GroupDiffCallBack(ArrayList(adapter.getItems()), it)) - adapter.setItems(it, false) - diffResult.dispatchUpdatesTo(adapter) + adapter.setItems(it, diffResult) }) val itemTouchCallback = ItemTouchCallback() itemTouchCallback.onItemTouchCallbackListener = adapter diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt index c94674afd..1227f4232 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt @@ -285,8 +285,7 @@ class SearchActivity : VMBaseActivity(R.layout.activity_book_se } refreshTime = System.currentTimeMillis() val diffResult = DiffUtil.calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), items)) - adapter.setItems(items, false) - diffResult.dispatchUpdatesTo(adapter) + adapter.setItems(items, diffResult) } /** diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index 128e4d9ca..5b375b4cf 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -136,8 +136,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity bookSourceLiveDate?.observe(this, Observer { val diffResult = DiffUtil .calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), it)) - adapter.setItems(it, false) - diffResult.dispatchUpdatesTo(adapter) + adapter.setItems(it, diffResult) upCountView() }) } diff --git a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt index 7288b2207..1a462a294 100644 --- a/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/bookshelf/books/BooksFragment.kt @@ -106,8 +106,7 @@ class BooksFragment : BaseFragment(R.layout.fragment_books), bookshelfLiveData?.observe(this, Observer { val diffResult = DiffUtil .calculateDiff(BooksDiffCallBack(ArrayList(booksAdapter.getItems()), it)) - booksAdapter.setItems(it, false) - diffResult.dispatchUpdatesTo(booksAdapter) + booksAdapter.setItems(it, diffResult) }) } 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 f4bf1774d..4bcaa7945 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 @@ -134,8 +134,7 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi } val diffResult = DiffUtil.calculateDiff(DiffCallBack(ArrayList(adapter.getItems()), it)) - adapter.setItems(it, false) - diffResult.dispatchUpdatesTo(adapter) + adapter.setItems(it, diffResult) dataInit = true upCountView() }) diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index 381cbb199..e83a15c19 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -186,8 +186,7 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r sourceLiveData?.observe(this, Observer { val diffResult = DiffUtil .calculateDiff(DiffCallBack(adapter.getItems(), it)) - adapter.setItems(it, false) - diffResult.dispatchUpdatesTo(adapter) + adapter.setItems(it, diffResult) upCountView() }) }