pull/1652/head
kunfei 3 years ago
parent 328ede69cc
commit 6bc9541188
  1. 37
      app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt

@ -8,6 +8,8 @@ import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import splitties.views.onLongClick
/**
@ -46,8 +48,9 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
recyclerView.adapter = this
}
@Synchronized
fun setItems(items: List<ITEM>?) {
suspend fun setItems(items: List<ITEM>?) {
withContext(Dispatchers.Default) {
synchronized(asyncListDiffer) {
kotlin.runCatching {
if (items == null) {
asyncListDiffer.submitList(null)
@ -56,18 +59,24 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
}
}
}
}
}
@Synchronized
fun setItem(position: Int, item: ITEM) {
suspend fun setItem(position: Int, item: ITEM) {
withContext(Dispatchers.Default) {
synchronized(asyncListDiffer) {
kotlin.runCatching {
val list = ArrayList(asyncListDiffer.currentList)
list[position] = item
asyncListDiffer.submitList(list)
}
}
}
}
@Synchronized
fun updateItem(item: ITEM) {
suspend fun updateItem(item: ITEM) {
withContext(Dispatchers.Main) {
synchronized(asyncListDiffer) {
kotlin.runCatching {
val index = asyncListDiffer.currentList.indexOf(item)
if (index >= 0) {
@ -76,9 +85,12 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
}
}
}
}
}
@Synchronized
fun updateItem(position: Int, payload: Any) {
suspend fun updateItem(position: Int, payload: Any) {
withContext(Dispatchers.Main) {
synchronized(asyncListDiffer) {
kotlin.runCatching {
val size = itemCount
if (position in 0 until size) {
@ -86,9 +98,12 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
}
}
}
}
}
@Synchronized
fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) {
suspend fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) {
withContext(Dispatchers.Main) {
synchronized(asyncListDiffer) {
kotlin.runCatching {
val size = itemCount
if (fromPosition in 0 until size && toPosition in 0 until size) {
@ -100,6 +115,8 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
}
}
}
}
}
fun isEmpty() = asyncListDiffer.currentList.isEmpty()

Loading…
Cancel
Save