pull/1814/head
kunfei 3 years ago
parent 1cb35a75d0
commit 73dd586edc
  1. 17
      app/src/main/java/io/legado/app/ui/book/manage/BookAdapter.kt
  2. 29
      app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageActivity.kt
  3. 10
      app/src/main/java/io/legado/app/ui/book/manage/BookshelfManageViewModel.kt

@ -23,6 +23,13 @@ class BookAdapter(context: Context, val callBack: CallBack) :
private val selectedBooks: HashSet<Book> = hashSetOf()
var actionItem: Book? = null
val selection: List<Book>
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<Book> {
val books = arrayListOf<Book>()
selectedBooks.forEach {
if (getItems().contains(it)) {
books.add(it)
}
}
return books.toTypedArray()
}
private fun getGroupList(groupId: Long): List<String> {
val groupNames = arrayListOf<String>()
callBack.groupList.forEach {

@ -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<SourcePickerDialog>()
}
@ -213,30 +213,29 @@ class BookshelfManageActivity :
override fun upGroup(requestCode: Int, groupId: Long) {
when (requestCode) {
groupRequestCode -> {
val books = arrayListOf<Book>()
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<Book>()
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
}

@ -18,12 +18,12 @@ class BookshelfManageViewModel(application: Application) : BaseViewModel(applica
val batchChangeSourcePosition = mutableStateOf(0)
var batchChangeSourceCoroutine: Coroutine<Unit>? = null
fun upCanUpdate(books: Array<Book>, canUpdate: Boolean) {
fun upCanUpdate(books: List<Book>, 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<Book>, source: BookSource) {
fun changeSource(books: List<Book>, source: BookSource) {
batchChangeSourceCoroutine?.cancel()
batchChangeSourceCoroutine = execute {
batchChangeSourceSize.value = books.size

Loading…
Cancel
Save