From 396d747c859d208aaf92cc96c09c6449957f643a Mon Sep 17 00:00:00 2001 From: kunfei Date: Fri, 14 Feb 2020 22:32:18 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/base/adapter/CommonRecyclerAdapter.kt | 17 ++++++++++++++--- .../app/ui/book/group/GroupManageDialog.kt | 3 +-- .../legado/app/ui/book/search/SearchActivity.kt | 3 +-- .../ui/book/source/manage/BookSourceActivity.kt | 3 +-- .../ui/main/bookshelf/books/BooksFragment.kt | 3 +-- .../app/ui/replacerule/ReplaceRuleActivity.kt | 3 +-- .../ui/rss/source/manage/RssSourceActivity.kt | 3 +-- 7 files changed, 20 insertions(+), 15 deletions(-) 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() }) }