From 335d8eeb162f767b6238792af180637374e78dee Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 18 Oct 2022 20:52:31 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ui/book/manage/BookshelfManageActivity.kt | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt index 4e61be780..d1da8ae63 100644 --- a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt @@ -58,6 +58,7 @@ class BookshelfManageActivity : private var booksFlowJob: Job? = null private var menu: Menu? = null private var searchView: SearchView? = null + private var books: List? = null override fun onActivityCreated(savedInstanceState: Bundle?) { viewModel.groupId = intent.getLongExtra("groupId", -1) @@ -71,7 +72,7 @@ class BookshelfManageActivity : initRecyclerView() initOtherView() initGroupData() - upBookData() + upBookDataByGroupId() } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { @@ -120,7 +121,7 @@ class BookshelfManageActivity : } override fun onQueryTextChange(newText: String?): Boolean { - upBookData(newText) + upBookData() return false } @@ -174,7 +175,7 @@ class BookshelfManageActivity : } } - private fun upBookData(searchKey: String? = null) { + private fun upBookDataByGroupId() { booksFlowJob?.cancel() booksFlowJob = launch { when (viewModel.groupId) { @@ -187,34 +188,43 @@ class BookshelfManageActivity : AppConst.bookGroupErrorId -> appDb.bookDao.flowUpdateError() else -> appDb.bookDao.flowByGroup(viewModel.groupId) }.conflate().map { list -> - val books = if (searchKey.isNullOrBlank()) { - list - } else { - list.filter { - it.contains(searchKey) - } - } when (AppConfig.bookshelfSort) { - 1 -> books.sortedByDescending { + 1 -> list.sortedByDescending { it.latestChapterTime } - 2 -> books.sortedWith { o1, o2 -> + 2 -> list.sortedWith { o1, o2 -> o1.name.cnCompare(o2.name) } - 3 -> books.sortedBy { + 3 -> list.sortedBy { it.order } - else -> books.sortedByDescending { + else -> list.sortedByDescending { it.durChapterTime } } }.flowOn(IO) - .conflate().collect { books -> - adapter.setItems(books) + .conflate().collect { + books = it + upBookData() } } } + private fun upBookData() { + books?.let { books -> + val searchKey = searchView?.query + if (searchKey.isNullOrEmpty()) { + adapter.setItems(books) + } else { + books.filter { + it.contains(searchKey.toString()) + }.let { + adapter.setItems(it) + } + } + } + } + override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_group_manage -> showDialogFragment() @@ -223,7 +233,7 @@ class BookshelfManageActivity : binding.titleBar.subtitle = item.title viewModel.groupId = appDb.bookGroupDao.getByName(item.title.toString())?.groupId ?: 0 - upBookData() + upBookDataByGroupId() } } return super.onCompatOptionsItemSelected(item)