优化
This commit is contained in:
@@ -8,8 +8,6 @@ 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
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -48,63 +46,51 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
|
|||||||
recyclerView.adapter = this
|
recyclerView.adapter = this
|
||||||
}
|
}
|
||||||
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setItem(position: Int, item: ITEM) {
|
fun setItem(position: Int, item: ITEM) {
|
||||||
synchronized(asyncListDiffer) {
|
kotlin.runCatching {
|
||||||
kotlin.runCatching {
|
asyncListDiffer.currentList[position] = item
|
||||||
asyncListDiffer.currentList[position] = item
|
notifyItemChanged(position)
|
||||||
notifyItemChanged(position)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateItem(item: ITEM) {
|
fun updateItem(item: ITEM) {
|
||||||
synchronized(asyncListDiffer) {
|
kotlin.runCatching {
|
||||||
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) {
|
fun updateItem(position: Int, payload: Any) {
|
||||||
synchronized(asyncListDiffer) {
|
kotlin.runCatching {
|
||||||
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) {
|
fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) {
|
||||||
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) {
|
notifyItemRangeChanged(
|
||||||
notifyItemRangeChanged(
|
fromPosition,
|
||||||
fromPosition,
|
toPosition - fromPosition + 1,
|
||||||
toPosition - fromPosition + 1,
|
payloads
|
||||||
payloads
|
)
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ import io.legado.app.lib.theme.primaryColor
|
|||||||
import io.legado.app.utils.applyTint
|
import io.legado.app.utils.applyTint
|
||||||
import io.legado.app.utils.setLayout
|
import io.legado.app.utils.setLayout
|
||||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.conflate
|
import kotlinx.coroutines.flow.conflate
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@@ -67,7 +68,7 @@ class ChangeCoverDialog() : BaseDialogFragment(R.layout.dialog_change_cover),
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initData() {
|
private fun initData() {
|
||||||
launch {
|
launch(Dispatchers.Default) {
|
||||||
whenStarted {
|
whenStarted {
|
||||||
viewModel.dataFlow.conflate().collect {
|
viewModel.dataFlow.conflate().collect {
|
||||||
adapter.setItems(it)
|
adapter.setItems(it)
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ import io.legado.app.ui.book.source.manage.BookSourceActivity
|
|||||||
import io.legado.app.ui.widget.recycler.VerticalDivider
|
import io.legado.app.ui.widget.recycler.VerticalDivider
|
||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.*
|
||||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.conflate
|
import kotlinx.coroutines.flow.conflate
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@@ -145,7 +146,7 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_
|
|||||||
}
|
}
|
||||||
binding.toolBar.menu.applyTint(requireContext())
|
binding.toolBar.menu.applyTint(requireContext())
|
||||||
}
|
}
|
||||||
launch {
|
launch(Dispatchers.Default) {
|
||||||
whenStarted {
|
whenStarted {
|
||||||
viewModel.searchDataFlow.conflate().collect {
|
viewModel.searchDataFlow.conflate().collect {
|
||||||
adapter.setItems(it)
|
adapter.setItems(it)
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ import io.legado.app.ui.book.source.manage.BookSourceActivity
|
|||||||
import io.legado.app.ui.widget.recycler.VerticalDivider
|
import io.legado.app.ui.widget.recycler.VerticalDivider
|
||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.*
|
||||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.flow.conflate
|
import kotlinx.coroutines.flow.conflate
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@@ -177,7 +178,7 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c
|
|||||||
}
|
}
|
||||||
binding.toolBar.menu.applyTint(requireContext())
|
binding.toolBar.menu.applyTint(requireContext())
|
||||||
}
|
}
|
||||||
launch {
|
launch(Dispatchers.Default) {
|
||||||
whenStarted {
|
whenStarted {
|
||||||
viewModel.searchDataFlow.conflate().collect {
|
viewModel.searchDataFlow.conflate().collect {
|
||||||
searchBookAdapter.setItems(it)
|
searchBookAdapter.setItems(it)
|
||||||
|
|||||||
@@ -29,12 +29,9 @@ import io.legado.app.ui.book.source.manage.BookSourceActivity
|
|||||||
import io.legado.app.ui.widget.recycler.LoadMoreView
|
import io.legado.app.ui.widget.recycler.LoadMoreView
|
||||||
import io.legado.app.utils.*
|
import io.legado.app.utils.*
|
||||||
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
import io.legado.app.utils.viewbindingdelegate.viewBinding
|
||||||
|
import kotlinx.coroutines.*
|
||||||
import kotlinx.coroutines.Dispatchers.IO
|
import kotlinx.coroutines.Dispatchers.IO
|
||||||
import kotlinx.coroutines.Job
|
|
||||||
import kotlinx.coroutines.delay
|
|
||||||
import kotlinx.coroutines.flow.conflate
|
import kotlinx.coroutines.flow.conflate
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlinx.coroutines.withContext
|
|
||||||
|
|
||||||
class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel>(),
|
class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel>(),
|
||||||
BookAdapter.CallBack,
|
BookAdapter.CallBack,
|
||||||
@@ -198,7 +195,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun initData() {
|
private fun initData() {
|
||||||
lifecycleScope.launch {
|
lifecycleScope.launch(Dispatchers.Default) {
|
||||||
whenStarted {
|
whenStarted {
|
||||||
viewModel.searchDataFlow.conflate().collect {
|
viewModel.searchDataFlow.conflate().collect {
|
||||||
adapter.setItems(it)
|
adapter.setItems(it)
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import android.os.Bundle
|
|||||||
import android.view.View
|
import android.view.View
|
||||||
import androidx.core.view.isGone
|
import androidx.core.view.isGone
|
||||||
import androidx.fragment.app.activityViewModels
|
import androidx.fragment.app.activityViewModels
|
||||||
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.recyclerview.widget.GridLayoutManager
|
import androidx.recyclerview.widget.GridLayoutManager
|
||||||
import androidx.recyclerview.widget.LinearLayoutManager
|
import androidx.recyclerview.widget.LinearLayoutManager
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
@@ -30,7 +31,6 @@ import kotlinx.coroutines.flow.catch
|
|||||||
import kotlinx.coroutines.flow.conflate
|
import kotlinx.coroutines.flow.conflate
|
||||||
import kotlinx.coroutines.flow.flowOn
|
import kotlinx.coroutines.flow.flowOn
|
||||||
import kotlinx.coroutines.flow.map
|
import kotlinx.coroutines.flow.map
|
||||||
import kotlinx.coroutines.launch
|
|
||||||
import kotlin.math.max
|
import kotlin.math.max
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -105,7 +105,7 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
|
|||||||
|
|
||||||
private fun upRecyclerData() {
|
private fun upRecyclerData() {
|
||||||
booksFlowJob?.cancel()
|
booksFlowJob?.cancel()
|
||||||
booksFlowJob = launch {
|
booksFlowJob = lifecycleScope.launchWhenStarted {
|
||||||
when (groupId) {
|
when (groupId) {
|
||||||
AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
|
AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
|
||||||
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
|
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
|
||||||
|
|||||||
Reference in New Issue
Block a user