From f259380aaffbde6113fe90c3df75681c8d6d0978 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sun, 27 Dec 2020 15:19:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/base/adapter/DiffRecyclerAdapter.kt | 24 ++++-- .../app/base/adapter/RecyclerAdapter.kt | 81 ++++++++++++------- 2 files changed, 69 insertions(+), 36 deletions(-) diff --git a/app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt b/app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt index 258f93c87..9f9a69dc2 100644 --- a/app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt +++ b/app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt @@ -47,39 +47,46 @@ abstract class DiffRecyclerAdapter(protected val context recyclerView.adapter = this } + @Synchronized fun setItems(items: List?) { - synchronized(lock) { + kotlin.runCatching { asyncListDiffer.submitList(items) } } + @Synchronized fun setItem(position: Int, item: ITEM) { - synchronized(lock) { + kotlin.runCatching { val list = ArrayList(asyncListDiffer.currentList) list[position] = item asyncListDiffer.submitList(list) } } - fun updateItem(item: ITEM) = - synchronized(lock) { + @Synchronized + fun updateItem(item: ITEM) { + kotlin.runCatching { val index = asyncListDiffer.currentList.indexOf(item) if (index >= 0) { asyncListDiffer.currentList[index] = item notifyItemChanged(index) } } + } - fun updateItem(position: Int, payload: Any) = - synchronized(lock) { + @Synchronized + fun updateItem(position: Int, payload: Any) { + kotlin.runCatching { val size = itemCount if (position in 0 until size) { notifyItemChanged(position, payload) } } + } - fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) = - synchronized(lock) { + @Synchronized + fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) { + kotlin.runCatching { val size = itemCount if (fromPosition in 0 until size && toPosition in 0 until size) { notifyItemRangeChanged( @@ -89,6 +96,7 @@ abstract class DiffRecyclerAdapter(protected val context ) } } + } fun isEmpty() = asyncListDiffer.currentList.isEmpty() diff --git a/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt b/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt index 7ddbaf581..9815a6516 100644 --- a/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt +++ b/app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt @@ -45,42 +45,49 @@ abstract class RecyclerAdapter(protected val context: Co recyclerView.adapter = this } + @Synchronized fun addHeaderView(header: ((parent: ViewGroup) -> ViewBinding)) { - synchronized(lock) { + kotlin.runCatching { val index = headerItems.size() headerItems.put(TYPE_HEADER_VIEW + headerItems.size(), header) notifyItemInserted(index) } } - fun addFooterView(footer: ((parent: ViewGroup) -> ViewBinding)) = - synchronized(lock) { + @Synchronized + fun addFooterView(footer: ((parent: ViewGroup) -> ViewBinding)) { + kotlin.runCatching { val index = getActualItemCount() + footerItems.size() footerItems.put(TYPE_FOOTER_VIEW + footerItems.size(), footer) notifyItemInserted(index) } + } - - fun removeHeaderView(header: ((parent: ViewGroup) -> ViewBinding)) = - synchronized(lock) { + @Synchronized + fun removeHeaderView(header: ((parent: ViewGroup) -> ViewBinding)) { + kotlin.runCatching { val index = headerItems.indexOfValue(header) if (index >= 0) { headerItems.remove(index) notifyItemRemoved(index) } } + } - fun removeFooterView(footer: ((parent: ViewGroup) -> ViewBinding)) = - synchronized(lock) { + @Synchronized + fun removeFooterView(footer: ((parent: ViewGroup) -> ViewBinding)) { + kotlin.runCatching { val index = footerItems.indexOfValue(footer) if (index >= 0) { footerItems.remove(index) notifyItemRemoved(getActualItemCount() + index - 2) } } + } + @Synchronized fun setItems(items: List?) { - synchronized(lock) { + kotlin.runCatching { if (this.items.isNotEmpty()) { this.items.clear() } @@ -92,8 +99,9 @@ abstract class RecyclerAdapter(protected val context: Co } } + @Synchronized fun setItems(items: List?, diffResult: DiffUtil.DiffResult) { - synchronized(lock) { + kotlin.runCatching { if (this.items.isNotEmpty()) { this.items.clear() } @@ -105,8 +113,9 @@ abstract class RecyclerAdapter(protected val context: Co } } + @Synchronized fun setItem(position: Int, item: ITEM) { - synchronized(lock) { + kotlin.runCatching { val oldSize = getActualItemCount() if (position in 0 until oldSize) { this.items[position] = item @@ -116,8 +125,9 @@ abstract class RecyclerAdapter(protected val context: Co } } + @Synchronized fun addItem(item: ITEM) { - synchronized(lock) { + kotlin.runCatching { val oldSize = getActualItemCount() if (this.items.add(item)) { notifyItemInserted(oldSize + getHeaderCount()) @@ -126,8 +136,9 @@ abstract class RecyclerAdapter(protected val context: Co } } + @Synchronized fun addItems(position: Int, newItems: List) { - synchronized(lock) { + kotlin.runCatching { if (this.items.addAll(position, newItems)) { notifyItemRangeInserted(position + getHeaderCount(), newItems.size) } @@ -135,8 +146,9 @@ abstract class RecyclerAdapter(protected val context: Co } } + @Synchronized fun addItems(newItems: List) { - synchronized(lock) { + kotlin.runCatching { val oldSize = getActualItemCount() if (this.items.addAll(newItems)) { if (oldSize == 0 && getHeaderCount() == 0) { @@ -149,8 +161,9 @@ abstract class RecyclerAdapter(protected val context: Co } } + @Synchronized fun removeItem(position: Int) { - synchronized(lock) { + kotlin.runCatching { if (this.items.removeAt(position) != null) { notifyItemRemoved(position + getHeaderCount()) } @@ -158,8 +171,9 @@ abstract class RecyclerAdapter(protected val context: Co } } + @Synchronized fun removeItem(item: ITEM) { - synchronized(lock) { + kotlin.runCatching { if (this.items.remove(item)) { notifyItemRemoved(this.items.indexOf(item) + getHeaderCount()) } @@ -167,8 +181,9 @@ abstract class RecyclerAdapter(protected val context: Co } } + @Synchronized fun removeItems(items: List) { - synchronized(lock) { + kotlin.runCatching { if (this.items.removeAll(items)) { notifyDataSetChanged() } @@ -176,8 +191,9 @@ abstract class RecyclerAdapter(protected val context: Co } } + @Synchronized fun swapItem(oldPosition: Int, newPosition: Int) { - synchronized(lock) { + kotlin.runCatching { val size = getActualItemCount() if (oldPosition in 0 until size && newPosition in 0 until size) { val srcPosition = oldPosition + getHeaderCount() @@ -189,8 +205,9 @@ abstract class RecyclerAdapter(protected val context: Co } } - fun updateItem(item: ITEM) = - synchronized(lock) { + @Synchronized + fun updateItem(item: ITEM) { + kotlin.runCatching { val index = this.items.indexOf(item) if (index >= 0) { this.items[index] = item @@ -198,17 +215,21 @@ abstract class RecyclerAdapter(protected val context: Co } onCurrentListChanged() } + } - fun updateItem(position: Int, payload: Any) = - synchronized(lock) { + @Synchronized + fun updateItem(position: Int, payload: Any) { + kotlin.runCatching { val size = getActualItemCount() if (position in 0 until size) { notifyItemChanged(position + getHeaderCount(), payload) } } + } - fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) = - synchronized(lock) { + @Synchronized + fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) { + kotlin.runCatching { val size = getActualItemCount() if (fromPosition in 0 until size && toPosition in 0 until size) { notifyItemRangeChanged( @@ -218,11 +239,15 @@ abstract class RecyclerAdapter(protected val context: Co ) } } + } - fun clearItems() = synchronized(lock) { - this.items.clear() - notifyDataSetChanged() - onCurrentListChanged() + @Synchronized + fun clearItems() { + kotlin.runCatching { + this.items.clear() + notifyDataSetChanged() + onCurrentListChanged() + } } fun isEmpty() = items.isEmpty()