Просмотр исходного кода

[修复] 修复了文件夹内没有书籍显示的问题 #509

HapeLee 10 месяцев назад
Родитель
Сommit
841c3df172
1 измененных файлов с 45 добавлено и 46 удалено
  1. 45 46
      app/src/main/java/io/legado/app/ui/main/bookshelf/books/BookshelfFragment2.kt

+ 45 - 46
app/src/main/java/io/legado/app/ui/main/bookshelf/books/BookshelfFragment2.kt

@@ -249,16 +249,14 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
     }
     }
 
 
     private fun initBooksData() {
     private fun initBooksData() {
-        if (groupId == BookGroup.Companion.IdRoot) {
+        if (groupId == BookGroup.IdRoot) {
             if (isAdded) {
             if (isAdded) {
                 binding.collTopBar.title = getString(R.string.bookshelf)
                 binding.collTopBar.title = getString(R.string.bookshelf)
                 binding.refreshLayout.isEnabled = true
                 binding.refreshLayout.isEnabled = true
                 enableRefresh = true
                 enableRefresh = true
             }
             }
         } else {
         } else {
-            bookGroups.firstOrNull {
-                groupId == it.groupId
-            }?.let {
+            bookGroups.firstOrNull { groupId == it.groupId }?.let {
                 binding.collTopBar.title = it.groupName
                 binding.collTopBar.title = it.groupName
                 binding.refreshLayout.isEnabled = it.enableRefresh
                 binding.refreshLayout.isEnabled = it.enableRefresh
                 enableRefresh = it.enableRefresh
                 enableRefresh = it.enableRefresh
@@ -266,48 +264,49 @@ class BookshelfFragment2() : BaseBookshelfFragment(R.layout.fragment_bookshelf2)
         }
         }
         booksFlowJob?.cancel()
         booksFlowJob?.cancel()
         booksFlowJob = viewLifecycleOwner.lifecycleScope.launch {
         booksFlowJob = viewLifecycleOwner.lifecycleScope.launch {
-            appDb.bookDao.flowByGroup(groupId).map { list ->
-                //排序
-                val isDescending = AppConfig.bookshelfSortOrder == 1
-
-                val sortType = AppConfig.getBookSortByGroupId(groupId)
-                when (sortType) {
-                    1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime }
-                    else list.sortedBy { it.latestChapterTime }
-
-                    2 -> if (isDescending)
-                        list.sortedWith { o1, o2 -> o2.name.cnCompare(o1.name) }
-                    else
-                        list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) }
-
-                    3 -> if (isDescending) list.sortedByDescending { it.order }
-                    else list.sortedBy { it.order }
-
-                    4 -> if (isDescending) list.sortedByDescending {
-                        max(it.latestChapterTime, it.durChapterTime)
-                    } else list.sortedBy { max(it.latestChapterTime, it.durChapterTime) }
-
-                    5 -> if (isDescending)
-                        list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) }
-                    else
-                        list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) }
-
-                    else -> if (isDescending) list.sortedByDescending { it.durChapterTime }
-                    else list.sortedBy { it.durChapterTime }
-                }
-
-            }.flowWithLifecycleAndDatabaseChangeFirst(
-                viewLifecycleOwner.lifecycle,
-                Lifecycle.State.RESUMED,
-                AppDatabase.Companion.BOOK_TABLE_NAME
-            ).catch {
-                AppLog.put("书架更新出错", it)
-            }.conflate().flowOn(Dispatchers.Default).collect { list ->
-                if (view == null) return@collect
-                binding.emptyView.isGone = list.isNotEmpty()
-                binding.refreshLayout.isEnabled = enableRefresh && list.isNotEmpty()
-                booksAdapter.updateItems()
-                delay(500)
+            viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
+                appDb.bookDao.flowByGroup(groupId)
+                    .map { list ->
+                        val isDescending = AppConfig.bookshelfSortOrder == 1
+                        val sortType = AppConfig.getBookSortByGroupId(groupId)
+
+                        when (sortType) {
+                            1 -> if (isDescending) list.sortedByDescending { it.latestChapterTime }
+                            else list.sortedBy { it.latestChapterTime }
+
+                            2 -> if (isDescending)
+                                list.sortedWith { o1, o2 -> o2.name.cnCompare(o1.name) }
+                            else
+                                list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) }
+
+                            3 -> if (isDescending) list.sortedByDescending { it.order }
+                            else list.sortedBy { it.order }
+
+                            4 -> if (isDescending)
+                                list.sortedByDescending { max(it.latestChapterTime, it.durChapterTime) }
+                            else
+                                list.sortedBy { max(it.latestChapterTime, it.durChapterTime) }
+
+                            5 -> if (isDescending)
+                                list.sortedWith { o1, o2 -> o2.author.cnCompare(o1.author) }
+                            else
+                                list.sortedWith { o1, o2 -> o1.author.cnCompare(o2.author) }
+
+                            else ->
+                                if (isDescending) list.sortedByDescending { it.durChapterTime }
+                                else list.sortedBy { it.durChapterTime }
+                        }
+                    }
+                    .flowOn(Dispatchers.Default)
+                    .catch { AppLog.put("书架更新出错", it) }
+                    .collect { list ->
+
+                        books = list
+                        booksAdapter.updateItems()
+                        val empty = getItemCount() > 0
+                        binding.emptyView.isGone = empty
+                        binding.refreshLayout.isEnabled = enableRefresh && empty
+                    }
             }
             }
         }
         }
     }
     }