pull/1652/head
kunfei 3 years ago
parent 328ede69cc
commit 6bc9541188
  1. 89
      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.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.viewbinding.ViewBinding import androidx.viewbinding.ViewBinding
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import splitties.views.onLongClick import splitties.views.onLongClick
/** /**
@ -46,57 +48,72 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
recyclerView.adapter = this recyclerView.adapter = this
} }
@Synchronized suspend fun setItems(items: List<ITEM>?) {
fun setItems(items: List<ITEM>?) { withContext(Dispatchers.Default) {
kotlin.runCatching { synchronized(asyncListDiffer) {
if (items == null) { kotlin.runCatching {
asyncListDiffer.submitList(null) if (items == null) {
} else { asyncListDiffer.submitList(null)
asyncListDiffer.submitList(ArrayList(items)) } else {
asyncListDiffer.submitList(ArrayList(items))
}
}
} }
} }
} }
@Synchronized suspend fun setItem(position: Int, item: ITEM) {
fun setItem(position: Int, item: ITEM) { withContext(Dispatchers.Default) {
kotlin.runCatching { synchronized(asyncListDiffer) {
val list = ArrayList(asyncListDiffer.currentList) kotlin.runCatching {
list[position] = item val list = ArrayList(asyncListDiffer.currentList)
asyncListDiffer.submitList(list) list[position] = item
asyncListDiffer.submitList(list)
}
}
} }
} }
@Synchronized suspend fun updateItem(item: ITEM) {
fun updateItem(item: ITEM) { withContext(Dispatchers.Main) {
kotlin.runCatching { synchronized(asyncListDiffer) {
val index = asyncListDiffer.currentList.indexOf(item) kotlin.runCatching {
if (index >= 0) { val index = asyncListDiffer.currentList.indexOf(item)
asyncListDiffer.currentList[index] = item if (index >= 0) {
notifyItemChanged(index) asyncListDiffer.currentList[index] = item
notifyItemChanged(index)
}
}
} }
} }
} }
@Synchronized suspend fun updateItem(position: Int, payload: Any) {
fun updateItem(position: Int, payload: Any) { withContext(Dispatchers.Main) {
kotlin.runCatching { synchronized(asyncListDiffer) {
val size = itemCount kotlin.runCatching {
if (position in 0 until size) { val size = itemCount
notifyItemChanged(position, payload) if (position in 0 until size) {
notifyItemChanged(position, payload)
}
}
} }
} }
} }
@Synchronized suspend fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) {
fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) { withContext(Dispatchers.Main) {
kotlin.runCatching { synchronized(asyncListDiffer) {
val size = itemCount kotlin.runCatching {
if (fromPosition in 0 until size && toPosition in 0 until size) { val size = itemCount
notifyItemRangeChanged( if (fromPosition in 0 until size && toPosition in 0 until size) {
fromPosition, notifyItemRangeChanged(
toPosition - fromPosition + 1, fromPosition,
payloads toPosition - fromPosition + 1,
) payloads
)
}
}
} }
} }
} }

Loading…
Cancel
Save