kunfei 4 жил өмнө
parent
commit
f461c4eba2

+ 7 - 8
app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt

@@ -42,12 +42,11 @@ import io.legado.app.ui.widget.recycler.VerticalDivider
 import io.legado.app.utils.*
 import io.legado.app.utils.viewbindingdelegate.viewBinding
 import kotlinx.coroutines.*
-import kotlinx.coroutines.flow.catch
-import kotlinx.coroutines.flow.collectLatest
-import kotlinx.coroutines.flow.flow
-import kotlinx.coroutines.flow.mapLatest
+import kotlinx.coroutines.flow.*
 
-@ExperimentalCoroutinesApi
+/**
+ * 书源管理界面
+ */
 class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceViewModel>(),
     PopupMenu.OnMenuItemClickListener,
     BookSourceAdapter.CallBack,
@@ -233,9 +232,7 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
                 else -> {
                     appDb.bookSourceDao.flowSearch("%$searchKey%")
                 }
-            }.catch {
-                AppLog.put("书源界面更新书源出错", it)
-            }.mapLatest { data ->
+            }.conflate().map { data ->
                 if (sortAscending) when (sort) {
                     Sort.Weight -> data.sortedBy { it.weight }
                     Sort.Name -> data.sortedWith { o1, o2 ->
@@ -270,6 +267,8 @@ class BookSourceActivity : VMBaseActivity<ActivityBookSourceBinding, BookSourceV
                     }
                     else -> data.reversed()
                 }
+            }.catch {
+                AppLog.put("书源界面更新书源出错", it)
             }.collectLatest { data ->
                 adapter.setItems(data, adapter.diffItemCallback)
             }

+ 7 - 7
app/src/main/java/io/legado/app/ui/main/bookshelf/style1/books/BooksFragment.kt

@@ -23,17 +23,17 @@ import io.legado.app.ui.book.read.ReadBookActivity
 import io.legado.app.ui.main.MainViewModel
 import io.legado.app.utils.*
 import io.legado.app.utils.viewbindingdelegate.viewBinding
-import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.Job
 import kotlinx.coroutines.flow.catch
-import kotlinx.coroutines.flow.mapLatest
+import kotlinx.coroutines.flow.collectLatest
+import kotlinx.coroutines.flow.conflate
+import kotlinx.coroutines.flow.map
 import kotlinx.coroutines.launch
 import kotlin.math.max
 
 /**
  * 书架界面
  */
-@ExperimentalCoroutinesApi
 class BooksFragment() : BaseFragment(R.layout.fragment_books),
     BaseBooksAdapter.CallBack {
 
@@ -110,9 +110,7 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
                 AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
                 AppConst.bookGroupNoneId -> appDb.bookDao.flowNoGroup()
                 else -> appDb.bookDao.flowByGroup(groupId)
-            }.catch {
-                AppLog.put("书架更新出错", it)
-            }.mapLatest { list ->
+            }.conflate().map { list ->
                 when (getPrefInt(PreferKey.bookshelfSort)) {
                     1 -> list.sortedByDescending { it.latestChapterTime }
                     2 -> list.sortedWith { o1, o2 ->
@@ -121,7 +119,9 @@ class BooksFragment() : BaseFragment(R.layout.fragment_books),
                     3 -> list.sortedBy { it.order }
                     else -> list.sortedByDescending { it.durChapterTime }
                 }
-            }.collect { list ->
+            }.catch {
+                AppLog.put("书架更新出错", it)
+            }.collectLatest { list ->
                 binding.tvEmptyMsg.isGone = list.isNotEmpty()
                 booksAdapter.setItems(list)
             }

+ 5 - 6
app/src/main/java/io/legado/app/ui/main/bookshelf/style2/BookshelfFragment2.kt

@@ -25,18 +25,17 @@ import io.legado.app.ui.book.search.SearchActivity
 import io.legado.app.ui.main.bookshelf.BaseBookshelfFragment
 import io.legado.app.utils.*
 import io.legado.app.utils.viewbindingdelegate.viewBinding
-import kotlinx.coroutines.ExperimentalCoroutinesApi
 import kotlinx.coroutines.Job
 import kotlinx.coroutines.flow.catch
 import kotlinx.coroutines.flow.collectLatest
-import kotlinx.coroutines.flow.mapLatest
+import kotlinx.coroutines.flow.conflate
+import kotlinx.coroutines.flow.map
 import kotlinx.coroutines.launch
 import kotlin.math.max
 
 /**
  * 书架界面
  */
-@ExperimentalCoroutinesApi
 class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
     SearchView.OnQueryTextListener,
     BaseBooksAdapter.CallBack {
@@ -124,9 +123,7 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
                 AppConst.bookGroupAudioId -> appDb.bookDao.flowAudio()
                 AppConst.bookGroupNoneId -> appDb.bookDao.flowNoGroup()
                 else -> appDb.bookDao.flowByGroup(groupId)
-            }.catch {
-                AppLog.put("书架更新出错", it)
-            }.mapLatest { list ->
+            }.conflate().map { list ->
                 when (getPrefInt(PreferKey.bookshelfSort)) {
                     1 -> list.sortedByDescending {
                         it.latestChapterTime
@@ -141,6 +138,8 @@ class BookshelfFragment2 : BaseBookshelfFragment(R.layout.fragment_bookshelf1),
                         it.durChapterTime
                     }
                 }
+            }.catch {
+                AppLog.put("书架更新出错", it)
             }.collectLatest { list ->
                 books = list
                 booksAdapter.notifyDataSetChanged()