feat: 优化代码

pull/141/head
kunfei 5 years ago
parent aad32764b6
commit c01e70c6a1
  1. 16
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookActivity.kt
  2. 12
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt

@ -115,25 +115,21 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
R.id.menu_all -> {
title_bar.subtitle = item.title
groupId = -1
adapter.selectedBooks.clear()
initBookData()
}
R.id.menu_local -> {
title_bar.subtitle = item.title
groupId = -2
adapter.selectedBooks.clear()
initBookData()
}
R.id.menu_audio -> {
title_bar.subtitle = item.title
groupId = -3
adapter.selectedBooks.clear()
initBookData()
}
else -> if (item.groupId == R.id.menu_group) {
title_bar.subtitle = item.title
groupId = item.itemId
adapter.selectedBooks.clear()
initBookData()
}
}
@ -144,13 +140,13 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
when (item?.itemId) {
R.id.menu_del_selection ->
alert(titleResource = R.string.draw, messageResource = R.string.sure_del) {
okButton { viewModel.deleteBook(*adapter.selectedBooks.toTypedArray()) }
okButton { viewModel.deleteBook(*adapter.selectedBooks()) }
noButton { }
}.show().applyTint()
R.id.menu_update_enable ->
viewModel.upCanUpdate(adapter.selectedBooks.toTypedArray(), true)
viewModel.upCanUpdate(adapter.selectedBooks(), true)
R.id.menu_update_disable ->
viewModel.upCanUpdate(adapter.selectedBooks.toTypedArray(), false)
viewModel.upCanUpdate(adapter.selectedBooks(), false)
R.id.menu_add_to_group -> selectGroup(0, addToGroupRequestCode)
}
return false
@ -173,7 +169,7 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
when (requestCode) {
groupRequestCode -> {
val books = arrayListOf<Book>()
adapter.selectedBooks.forEach {
adapter.selectedBooks().forEach {
books.add(it.copy(group = groupId))
}
viewModel.updateBook(*books.toTypedArray())
@ -185,7 +181,7 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
}
addToGroupRequestCode -> {
val books = arrayListOf<Book>()
adapter.selectedBooks.forEach {
adapter.selectedBooks().forEach {
books.add(it.copy(group = it.group or groupId))
}
viewModel.updateBook(*books.toTypedArray())
@ -194,7 +190,7 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
}
override fun upSelectCount() {
select_action_bar.upCountView(adapter.selectedBooks.size, adapter.getItems().size)
select_action_bar.upCountView(adapter.selectedBooks().size, adapter.getItems().size)
}
override fun deleteBook(book: Book) {

@ -13,7 +13,7 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<Book>(context, R.layout.item_arrange_book) {
val groupRequestCode = 12
val selectedBooks: HashSet<Book> = hashSetOf()
private val selectedBooks: HashSet<Book> = hashSetOf()
var actionItem: Book? = null
fun selectAll(selectAll: Boolean) {
@ -40,6 +40,16 @@ class ArrangeBookAdapter(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()
}
override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList<Any>) {
with(holder.itemView) {
tv_name.text = if (item.author.isEmpty()) {

Loading…
Cancel
Save