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.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,8 +48,9 @@ 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) {
synchronized(asyncListDiffer) {
kotlin.runCatching { kotlin.runCatching {
if (items == null) { if (items == null) {
asyncListDiffer.submitList(null) asyncListDiffer.submitList(null)
@ -56,18 +59,24 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
} }
} }
} }
}
}
@Synchronized suspend fun setItem(position: Int, item: ITEM) {
fun setItem(position: Int, item: ITEM) { withContext(Dispatchers.Default) {
synchronized(asyncListDiffer) {
kotlin.runCatching { kotlin.runCatching {
val list = ArrayList(asyncListDiffer.currentList) val list = ArrayList(asyncListDiffer.currentList)
list[position] = item list[position] = item
asyncListDiffer.submitList(list) asyncListDiffer.submitList(list)
} }
} }
}
}
@Synchronized suspend fun updateItem(item: ITEM) {
fun updateItem(item: ITEM) { withContext(Dispatchers.Main) {
synchronized(asyncListDiffer) {
kotlin.runCatching { kotlin.runCatching {
val index = asyncListDiffer.currentList.indexOf(item) val index = asyncListDiffer.currentList.indexOf(item)
if (index >= 0) { if (index >= 0) {
@ -76,9 +85,12 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
} }
} }
} }
}
}
@Synchronized suspend fun updateItem(position: Int, payload: Any) {
fun updateItem(position: Int, payload: Any) { withContext(Dispatchers.Main) {
synchronized(asyncListDiffer) {
kotlin.runCatching { kotlin.runCatching {
val size = itemCount val size = itemCount
if (position in 0 until size) { if (position in 0 until size) {
@ -86,9 +98,12 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
} }
} }
} }
}
}
@Synchronized suspend fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) {
fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) { withContext(Dispatchers.Main) {
synchronized(asyncListDiffer) {
kotlin.runCatching { 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) {
@ -100,6 +115,8 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
} }
} }
} }
}
}
fun isEmpty() = asyncListDiffer.currentList.isEmpty() fun isEmpty() = asyncListDiffer.currentList.isEmpty()

Loading…
Cancel
Save