pull/768/head
gedoor 4 years ago
parent b6ee1cc503
commit f259380aaf
  1. 24
      app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt
  2. 75
      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
}
@Synchronized
fun setItems(items: List<ITEM>?) {
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<ITEM, VB : ViewBinding>(protected val context
)
}
}
}
fun isEmpty() = asyncListDiffer.currentList.isEmpty()

@ -45,42 +45,49 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(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<ITEM>?) {
synchronized(lock) {
kotlin.runCatching {
if (this.items.isNotEmpty()) {
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) {
synchronized(lock) {
kotlin.runCatching {
if (this.items.isNotEmpty()) {
this.items.clear()
}
@ -105,8 +113,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(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<ITEM, VB : ViewBinding>(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<ITEM, VB : ViewBinding>(protected val context: Co
}
}
@Synchronized
fun addItems(position: Int, newItems: List<ITEM>) {
synchronized(lock) {
kotlin.runCatching {
if (this.items.addAll(position, newItems)) {
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>) {
synchronized(lock) {
kotlin.runCatching {
val oldSize = getActualItemCount()
if (this.items.addAll(newItems)) {
if (oldSize == 0 && getHeaderCount() == 0) {
@ -149,8 +161,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(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<ITEM, VB : ViewBinding>(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<ITEM, VB : ViewBinding>(protected val context: Co
}
}
@Synchronized
fun removeItems(items: List<ITEM>) {
synchronized(lock) {
kotlin.runCatching {
if (this.items.removeAll(items)) {
notifyDataSetChanged()
}
@ -176,8 +191,9 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(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<ITEM, VB : ViewBinding>(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<ITEM, VB : ViewBinding>(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,12 +239,16 @@ abstract class RecyclerAdapter<ITEM, VB : ViewBinding>(protected val context: Co
)
}
}
}
fun clearItems() = synchronized(lock) {
@Synchronized
fun clearItems() {
kotlin.runCatching {
this.items.clear()
notifyDataSetChanged()
onCurrentListChanged()
}
}
fun isEmpty() = items.isEmpty()

Loading…
Cancel
Save