pull/1652/head
kunfei 3 years ago
parent 6bc9541188
commit 3a8a51ac7c
  1. 12
      app/src/main/java/io/legado/app/base/adapter/DiffRecyclerAdapter.kt
  2. 3
      app/src/main/java/io/legado/app/ui/book/changecover/ChangeCoverDialog.kt
  3. 3
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceDialog.kt
  4. 3
      app/src/main/java/io/legado/app/ui/book/changesource/ChangeChapterSourceDialog.kt
  5. 3
      app/src/main/java/io/legado/app/ui/book/search/SearchActivity.kt
  6. 5
      app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt

@ -74,8 +74,7 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
} }
} }
suspend fun updateItem(item: ITEM) { fun updateItem(item: ITEM) {
withContext(Dispatchers.Main) {
synchronized(asyncListDiffer) { synchronized(asyncListDiffer) {
kotlin.runCatching { kotlin.runCatching {
val index = asyncListDiffer.currentList.indexOf(item) val index = asyncListDiffer.currentList.indexOf(item)
@ -86,10 +85,8 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
} }
} }
} }
}
suspend fun updateItem(position: Int, payload: Any) { fun updateItem(position: Int, payload: Any) {
withContext(Dispatchers.Main) {
synchronized(asyncListDiffer) { synchronized(asyncListDiffer) {
kotlin.runCatching { kotlin.runCatching {
val size = itemCount val size = itemCount
@ -99,10 +96,8 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
} }
} }
} }
}
suspend fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) { fun updateItems(fromPosition: Int, toPosition: Int, payloads: Any) {
withContext(Dispatchers.Main) {
synchronized(asyncListDiffer) { synchronized(asyncListDiffer) {
kotlin.runCatching { kotlin.runCatching {
val size = itemCount val size = itemCount
@ -116,7 +111,6 @@ abstract class DiffRecyclerAdapter<ITEM, VB : ViewBinding>(protected val context
} }
} }
} }
}
fun isEmpty() = asyncListDiffer.currentList.isEmpty() fun isEmpty() = asyncListDiffer.currentList.isEmpty()

@ -15,7 +15,6 @@ 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
@ -68,7 +67,7 @@ class ChangeCoverDialog() : BaseDialogFragment(R.layout.dialog_change_cover),
} }
private fun initData() { private fun initData() {
launch(Dispatchers.Default) { launch {
whenStarted { whenStarted {
viewModel.dataFlow.conflate().collect { viewModel.dataFlow.conflate().collect {
adapter.setItems(it) adapter.setItems(it)

@ -29,7 +29,6 @@ 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
@ -146,7 +145,7 @@ class ChangeBookSourceDialog() : BaseDialogFragment(R.layout.dialog_book_change_
} }
binding.toolBar.menu.applyTint(requireContext()) binding.toolBar.menu.applyTint(requireContext())
} }
launch(Dispatchers.Default) { launch {
whenStarted { whenStarted {
viewModel.searchDataFlow.conflate().collect { viewModel.searchDataFlow.conflate().collect {
adapter.setItems(it) adapter.setItems(it)

@ -31,7 +31,6 @@ 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
@ -178,7 +177,7 @@ class ChangeChapterSourceDialog() : BaseDialogFragment(R.layout.dialog_chapter_c
} }
binding.toolBar.menu.applyTint(requireContext()) binding.toolBar.menu.applyTint(requireContext())
} }
launch(Dispatchers.Default) { launch {
whenStarted { whenStarted {
viewModel.searchDataFlow.conflate().collect { viewModel.searchDataFlow.conflate().collect {
searchBookAdapter.setItems(it) searchBookAdapter.setItems(it)

@ -29,7 +29,6 @@ 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.Dispatchers.Default
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
@ -199,7 +198,7 @@ class SearchActivity : VMBaseActivity<ActivityBookSearchBinding, SearchViewModel
} }
private fun initData() { private fun initData() {
lifecycleScope.launch(Default) { lifecycleScope.launch {
whenStarted { whenStarted {
viewModel.searchDataFlow.conflate().collect { viewModel.searchDataFlow.conflate().collect {
adapter.setItems(it) adapter.setItems(it)

@ -28,6 +28,7 @@ import kotlinx.coroutines.Job
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.conflate import kotlinx.coroutines.flow.conflate
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlin.math.max import kotlin.math.max
@ -104,7 +105,7 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
private fun upRecyclerData() { private fun upRecyclerData() {
booksFlowJob?.cancel() booksFlowJob?.cancel()
booksFlowJob = launch(Dispatchers.Default) { booksFlowJob = launch {
when (groupId) { when (groupId) {
AppConst.bookGroupAllId -> appDb.bookDao.flowAll() AppConst.bookGroupAllId -> appDb.bookDao.flowAll()
AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal() AppConst.bookGroupLocalId -> appDb.bookDao.flowLocal()
@ -120,7 +121,7 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
3 -> list.sortedBy { it.order } 3 -> list.sortedBy { it.order }
else -> list.sortedByDescending { it.durChapterTime } else -> list.sortedByDescending { it.durChapterTime }
} }
}.catch { }.flowOn(Dispatchers.Default).catch {
AppLog.put("书架更新出错", it) AppLog.put("书架更新出错", it)
}.conflate().collect { list -> }.conflate().collect { list ->
binding.tvEmptyMsg.isGone = list.isNotEmpty() binding.tvEmptyMsg.isGone = list.isNotEmpty()

Loading…
Cancel
Save