pull/768/head
gedoor 5 years ago
parent b6ee1cc503
commit f259380aaf
  1. 24
      app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt
  2. 81
      app/src/main/java/io/legado/app/base/adapter/RecyclerAdapter.kt

@ -47,39 +47,46 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
recyclerView.adapter = this recyclerView.adapter = this
} }
@Synchronized
fun setItems(items: List<ITEM>?) { fun setItems(items: List<ITEM>?) {
synchronized(lock) { kotlin.runCatching {
asyncListDiffer.submitList(items) asyncListDiffer.submitList(items)
} }
} }
@Synchronized
fun setItem(position: Int, item: ITEM) { fun setItem(position: Int, item: ITEM) {
synchronized(lock) { kotlin.runCatching {
val list = ArrayList(asyncListDiffer.currentList) val list = ArrayList(asyncListDiffer.currentList)
list[position] = item list[position] = item
asyncListDiffer.submitList(list) asyncListDiffer.submitList(list)
} }
} }
fun updateItem(item: ITEM) = @Synchronized
synchronized(lock) { fun updateItem(item: ITEM) {
kotlin.runCatching {
val index = asyncListDiffer.currentList.indexOf(item) val index = asyncListDiffer.currentList.indexOf(item)
if (index >= 0) { if (index >= 0) {
asyncListDiffer.currentList[index] = item asyncListDiffer.currentList[index] = item
notifyItemChanged(index) notifyItemChanged(index)
} }
} }
}
fun updateItem(position: Int, payload: Any) = @Synchronized
synchronized(lock) { fun updateItem(position: Int, payload: Any) {
kotlin.runCatching {
val size = itemCount val size = itemCount
if (position in 0 until size) { if (position in 0 until size) {
notifyItemChanged(position, payload) notifyItemChanged(position, payload)
} }
} }
}
fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) = @Synchronized
synchronized(lock) { fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) {
kotlin.runCatching {
val size = itemCount val size = itemCount
if (fromPosition in 0 until size && toPosition in 0 until size) { if (fromPosition in 0 until size && toPosition in 0 until size) {
notifyItemRangeChanged( notifyItemRangeChanged(
@ -89,6 +96,7 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
) )
} }
} }
}
fun isEmpty() = asyncListDiffer.currentList.isEmpty() fun isEmpty() = asyncListDiffer.currentList.isEmpty()

@ -45,42 +45,49 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
recyclerView.adapter = this recyclerView.adapter = this
} }
@Synchronized
fun addHeaderView(header: ((parent: ViewGroup) -> ViewBinding)) { fun addHeaderView(header: ((parent: ViewGroup) -> ViewBinding)) {
synchronized(lock) { kotlin.runCatching {
val index = headerItems.size() val index = headerItems.size()
headerItems.put(TYPE_HEADER_VIEW + headerItems.size(), header) headerItems.put(TYPE_HEADER_VIEW + headerItems.size(), header)
notifyItemInserted(index) notifyItemInserted(index)
} }
} }
fun addFooterView(footer: ((parent: ViewGroup) -> ViewBinding)) = @Synchronized
synchronized(lock) { fun addFooterView(footer: ((parent: ViewGroup) -> ViewBinding)) {
kotlin.runCatching {
val index = getActualItemCount() + footerItems.size() val index = getActualItemCount() + footerItems.size()
footerItems.put(TYPE_FOOTER_VIEW + footerItems.size(), footer) footerItems.put(TYPE_FOOTER_VIEW + footerItems.size(), footer)
notifyItemInserted(index) notifyItemInserted(index)
} }
}
@Synchronized
fun removeHeaderView(header: ((parent: ViewGroup) -> ViewBinding)) = fun removeHeaderView(header: ((parent: ViewGroup) -> ViewBinding)) {
synchronized(lock) { kotlin.runCatching {
val index = headerItems.indexOfValue(header) val index = headerItems.indexOfValue(header)
if (index >= 0) { if (index >= 0) {
headerItems.remove(index) headerItems.remove(index)
notifyItemRemoved(index) notifyItemRemoved(index)
} }
} }
}
fun removeFooterView(footer: ((parent: ViewGroup) -> ViewBinding)) = @Synchronized
synchronized(lock) { fun removeFooterView(footer: ((parent: ViewGroup) -> ViewBinding)) {
kotlin.runCatching {
val index = footerItems.indexOfValue(footer) val index = footerItems.indexOfValue(footer)
if (index >= 0) { if (index >= 0) {
footerItems.remove(index) footerItems.remove(index)
notifyItemRemoved(getActualItemCount() + index - 2) notifyItemRemoved(getActualItemCount() + index - 2)
} }
} }
}
@Synchronized
fun setItems(items: List<ITEM>?) { fun setItems(items: List<ITEM>?) {
synchronized(lock) { kotlin.runCatching {
if (this.items.isNotEmpty()) { if (this.items.isNotEmpty()) {
this.items.clear() this.items.clear()
} }
@ -92,8 +99,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
} }
@Synchronized
fun setItems(items: List<ITEM>?, diffResult: DiffUtil.DiffResult) { fun setItems(items: List<ITEM>?, diffResult: DiffUtil.DiffResult) {
synchronized(lock) { kotlin.runCatching {
if (this.items.isNotEmpty()) { if (this.items.isNotEmpty()) {
this.items.clear() this.items.clear()
} }
@ -105,8 +113,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
} }
@Synchronized
fun setItem(position: Int, item: ITEM) { fun setItem(position: Int, item: ITEM) {
synchronized(lock) { kotlin.runCatching {
val oldSize = getActualItemCount() val oldSize = getActualItemCount()
if (position in 0 until oldSize) { if (position in 0 until oldSize) {
this.items[position] = item this.items[position] = item
@ -116,8 +125,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
} }
@Synchronized
fun addItem(item: ITEM) { fun addItem(item: ITEM) {
synchronized(lock) { kotlin.runCatching {
val oldSize = getActualItemCount() val oldSize = getActualItemCount()
if (this.items.add(item)) { if (this.items.add(item)) {
notifyItemInserted(oldSize + getHeaderCount()) notifyItemInserted(oldSize + getHeaderCount())
@ -126,8 +136,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
} }
@Synchronized
fun addItems(position: Int, newItems: List<ITEM>) { fun addItems(position: Int, newItems: List<ITEM>) {
synchronized(lock) { kotlin.runCatching {
if (this.items.addAll(position, newItems)) { if (this.items.addAll(position, newItems)) {
notifyItemRangeInserted(position + getHeaderCount(), newItems.size) notifyItemRangeInserted(position + getHeaderCount(), newItems.size)
} }
@ -135,8 +146,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
} }
@Synchronized
fun addItems(newItems: List<ITEM>) { fun addItems(newItems: List<ITEM>) {
synchronized(lock) { kotlin.runCatching {
val oldSize = getActualItemCount() val oldSize = getActualItemCount()
if (this.items.addAll(newItems)) { if (this.items.addAll(newItems)) {
if (oldSize == 0 && getHeaderCount() == 0) { if (oldSize == 0 && getHeaderCount() == 0) {
@ -149,8 +161,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
} }
@Synchronized
fun removeItem(position: Int) { fun removeItem(position: Int) {
synchronized(lock) { kotlin.runCatching {
if (this.items.removeAt(position) != null) { if (this.items.removeAt(position) != null) {
notifyItemRemoved(position + getHeaderCount()) notifyItemRemoved(position + getHeaderCount())
} }
@ -158,8 +171,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
} }
@Synchronized
fun removeItem(item: ITEM) { fun removeItem(item: ITEM) {
synchronized(lock) { kotlin.runCatching {
if (this.items.remove(item)) { if (this.items.remove(item)) {
notifyItemRemoved(this.items.indexOf(item) + getHeaderCount()) notifyItemRemoved(this.items.indexOf(item) + getHeaderCount())
} }
@ -167,8 +181,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
} }
@Synchronized
fun removeItems(items: List<ITEM>) { fun removeItems(items: List<ITEM>) {
synchronized(lock) { kotlin.runCatching {
if (this.items.removeAll(items)) { if (this.items.removeAll(items)) {
notifyDataSetChanged() notifyDataSetChanged()
} }
@ -176,8 +191,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
} }
@Synchronized
fun swapItem(oldPosition: Int, newPosition: Int) { fun swapItem(oldPosition: Int, newPosition: Int) {
synchronized(lock) { kotlin.runCatching {
val size = getActualItemCount() val size = getActualItemCount()
if (oldPosition in 0 until size && newPosition in 0 until size) { if (oldPosition in 0 until size && newPosition in 0 until size) {
val srcPosition = oldPosition + getHeaderCount() val srcPosition = oldPosition + getHeaderCount()
@ -189,8 +205,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
} }
fun updateItem(item: ITEM) = @Synchronized
synchronized(lock) { fun updateItem(item: ITEM) {
kotlin.runCatching {
val index = this.items.indexOf(item) val index = this.items.indexOf(item)
if (index >= 0) { if (index >= 0) {
this.items[index] = item this.items[index] = item
@ -198,17 +215,21 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
} }
onCurrentListChanged() onCurrentListChanged()
} }
}
fun updateItem(position: Int, payload: Any) = @Synchronized
synchronized(lock) { fun updateItem(position: Int, payload: Any) {
kotlin.runCatching {
val size = getActualItemCount() val size = getActualItemCount()
if (position in 0 until size) { if (position in 0 until size) {
notifyItemChanged(position + getHeaderCount(), payload) notifyItemChanged(position + getHeaderCount(), payload)
} }
} }
}
fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) = @Synchronized
synchronized(lock) { fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) {
kotlin.runCatching {
val size = getActualItemCount() val size = getActualItemCount()
if (fromPosition in 0 until size && toPosition in 0 until size) { if (fromPosition in 0 until size && toPosition in 0 until size) {
notifyItemRangeChanged( notifyItemRangeChanged(
@ -218,11 +239,15 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
) )
} }
} }
}
fun clearItems() = synchronized(lock) { @Synchronized
this.items.clear() fun clearItems() {
notifyDataSetChanged() kotlin.runCatching {
onCurrentListChanged() this.items.clear()
notifyDataSetChanged()
onCurrentListChanged()
}
} }
fun isEmpty() = items.isEmpty() fun isEmpty() = items.isEmpty()

Loading…
Cancel
Save