diff --git a/app/src/main/java/io/legado/app/ui/book/manage/BookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/manage/BookAdapter.kt index a7564cf5b..1897327ce 100644 --- a/app/src/main/java/io/legado/app/ui/book/manage/BookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/manage/BookAdapter.kt @@ -23,6 +23,13 @@ class BookAdapter(context: Context, val callBack: CallBack) : private val selectedBooks: HashSet = hashSetOf() var actionItem: Book? = null + val selection: List + get() { + return getItems().filter { + selectedBooks.contains(it) + } + } + override fun getViewBinding(parent: ViewGroup): ItemArrangeBookBinding { return ItemArrangeBookBinding.inflate(inflater, parent, false) } @@ -114,16 +121,6 @@ class BookAdapter(context: Context, val callBack: CallBack) : callBack.upSelectCount() } - fun selectedBooks(): Array { - val books = arrayListOf() - selectedBooks.forEach { - if (getItems().contains(it)) { - books.add(it) - } - } - return books.toTypedArray() - } - private fun getGroupList(groupId: Long): List { val groupNames = arrayListOf() callBack.groupList.forEach { 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 f2dfd4a4f..0d3a3dc3c 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 @@ -183,13 +183,13 @@ class BookshelfManageActivity : when (item?.itemId) { R.id.menu_del_selection -> alert(titleResource = R.string.draw, messageResource = R.string.sure_del) { - okButton { viewModel.deleteBook(*adapter.selectedBooks()) } + okButton { viewModel.deleteBook(*adapter.selection.toTypedArray()) } noButton() } R.id.menu_update_enable -> - viewModel.upCanUpdate(adapter.selectedBooks(), true) + viewModel.upCanUpdate(adapter.selection, true) R.id.menu_update_disable -> - viewModel.upCanUpdate(adapter.selectedBooks(), false) + viewModel.upCanUpdate(adapter.selection, false) R.id.menu_add_to_group -> selectGroup(addToGroupRequestCode, 0) R.id.menu_change_source -> showDialogFragment() } @@ -213,30 +213,29 @@ class BookshelfManageActivity : override fun upGroup(requestCode: Int, groupId: Long) { when (requestCode) { - groupRequestCode -> { - val books = arrayListOf() - adapter.selectedBooks().forEach { - books.add(it.copy(group = groupId)) + groupRequestCode -> adapter.selection.let { books -> + val array = Array(books.size) { + books[it].copy(group = groupId) } - viewModel.updateBook(*books.toTypedArray()) + viewModel.updateBook(*array) } adapter.groupRequestCode -> { adapter.actionItem?.let { viewModel.updateBook(it.copy(group = groupId)) } } - addToGroupRequestCode -> { - val books = arrayListOf() - adapter.selectedBooks().forEach { - books.add(it.copy(group = it.group or groupId)) + addToGroupRequestCode -> adapter.selection.let { books -> + val array = Array(books.size) { index -> + val book = books[index] + book.copy(group = book.group or groupId) } - viewModel.updateBook(*books.toTypedArray()) + viewModel.updateBook(*array) } } } override fun upSelectCount() { - binding.selectActionBar.upCountView(adapter.selectedBooks().size, adapter.getItems().size) + binding.selectActionBar.upCountView(adapter.selection.size, adapter.getItems().size) } override fun updateBook(vararg book: Book) { @@ -252,7 +251,7 @@ class BookshelfManageActivity : } override fun sourceOnClick(source: BookSource) { - viewModel.changeSource(adapter.selectedBooks(), source) + viewModel.changeSource(adapter.selection, source) viewModel.batchChangeSourceState.value = true } diff --git a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt index 66448d1f3..89aa00611 100644 --- a/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt @@ -18,12 +18,12 @@ class BookshelfManageViewModel(application: Application) : BaseViewModel(applica val batchChangeSourcePosition = mutableStateOf(0) var batchChangeSourceCoroutine: Coroutine? = null - fun upCanUpdate(books: Array, canUpdate: Boolean) { + fun upCanUpdate(books: List, canUpdate: Boolean) { execute { - books.forEach { - it.canUpdate = canUpdate + val array = Array(books.size) { + books[it].copy(canUpdate = canUpdate) } - appDb.bookDao.update(*books) + appDb.bookDao.update(*array) } } @@ -39,7 +39,7 @@ class BookshelfManageViewModel(application: Application) : BaseViewModel(applica } } - fun changeSource(books: Array, source: BookSource) { + fun changeSource(books: List, source: BookSource) { batchChangeSourceCoroutine?.cancel() batchChangeSourceCoroutine = execute { batchChangeSourceSize.value = books.size