feat: 优化代码

pull/95/head
kunfei 5 years ago
parent a5621c8b11
commit 1620967826
  1. 73
      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
  3. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  4. 2
      app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt
  5. 43
      app/src/main/res/layout/activity_arrange_book.xml
  6. 8
      app/src/main/res/menu/arrange_book_sel.xml

@ -3,6 +3,7 @@ package io.legado.app.ui.book.arrange
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.widget.PopupMenu
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
@ -12,19 +13,20 @@ import io.legado.app.base.VMBaseActivity
import io.legado.app.data.entities.Book
import io.legado.app.data.entities.BookGroup
import io.legado.app.lib.dialogs.alert
import io.legado.app.lib.dialogs.noButton
import io.legado.app.lib.dialogs.okButton
import io.legado.app.lib.theme.ATH
import io.legado.app.ui.book.group.GroupManageDialog
import io.legado.app.ui.book.group.GroupSelectDialog
import io.legado.app.ui.widget.SelectActionBar
import io.legado.app.utils.applyTint
import io.legado.app.utils.getVerticalDivider
import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_arrange_book.*
import org.jetbrains.anko.sdk27.listeners.onClick
import org.jetbrains.anko.toast
class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activity_arrange_book),
PopupMenu.OnMenuItemClickListener,
ArrangeBookAdapter.CallBack, GroupSelectDialog.CallBack {
override val viewModel: ArrangeBookViewModel
get() = getViewModel(ArrangeBookViewModel::class.java)
@ -87,33 +89,39 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
return super.onCompatOptionsItemSelected(item)
}
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_del_selection ->
alert(titleResource = R.string.sure, messageResource = R.string.sure_del) {
okButton { viewModel.deleteBook(*adapter.selectedBooks.toTypedArray()) }
noButton { }
}.show().applyTint()
}
return false
}
private fun initView() {
ATH.applyEdgeEffectColor(recycler_view)
recycler_view.layoutManager = LinearLayoutManager(this)
recycler_view.addItemDecoration(recycler_view.getVerticalDivider())
adapter = ArrangeBookAdapter(this, this)
recycler_view.adapter = adapter
cb_selected_all.onClick {
adapter.selectAll(!adapter.isSelectAll())
select_action_bar.setMainActionText(R.string.move_to_group)
select_action_bar.inflateMenu(R.menu.arrange_book)
select_action_bar.setOnMenuItemClickListener(this)
select_action_bar.setCallBack(object : SelectActionBar.CallBack {
override fun selectAll(selectAll: Boolean) {
adapter.selectAll(selectAll)
}
btn_delete.onClick {
if (adapter.selectedBooks.isEmpty()) {
toast(R.string.non_select)
return@onClick
}
alert(titleResource = R.string.sure, messageResource = R.string.sure_del) {
okButton {
viewModel.deleteBook(*adapter.selectedBooks.toTypedArray())
}
}.show().applyTint()
}
btn_to_group.onClick {
if (adapter.selectedBooks.isEmpty()) {
toast(R.string.non_select)
return@onClick
override fun revertSelection() {
adapter.revertSelection()
}
override fun onClickMainAction() {
selectGroup(groupRequestCode)
}
})
}
private fun initGroupData() {
@ -172,31 +180,7 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
}
override fun upSelectCount() {
cb_selected_all.isChecked = adapter.isSelectAll()
//重置全选的文字
if (cb_selected_all.isChecked) {
cb_selected_all.text = getString(
R.string.select_cancel_count,
adapter.selectedBooks.size,
adapter.getItems().size
)
} else {
cb_selected_all.text = getString(
R.string.select_all_count,
adapter.selectedBooks.size,
adapter.getItems().size
)
}
setMenuClickable(adapter.selectedBooks.isNotEmpty())
}
private fun setMenuClickable(isClickable: Boolean) {
//设置是否可删除
btn_delete.isEnabled = isClickable
btn_delete.isClickable = isClickable
//设置是否可添加书籍
btn_to_group.isEnabled = isClickable
btn_to_group.isClickable = isClickable
select_action_bar.upCountView(adapter.selectedBooks.size, adapter.getItems().size)
}
override fun deleteBook(book: Book) {
@ -206,4 +190,5 @@ class ArrangeBookActivity : VMBaseActivity<ArrangeBookViewModel>(R.layout.activi
}
}.show().applyTint()
}
}

@ -16,14 +16,6 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
val selectedBooks: HashSet<Book> = hashSetOf()
var actionItem: Book? = null
fun isSelectAll(): Boolean {
return if (selectedBooks.isEmpty()) {
false
} else {
selectedBooks.size >= itemCount
}
}
fun selectAll(selectAll: Boolean) {
if (selectAll) {
getItems().forEach {
@ -38,6 +30,10 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
}
}
fun revertSelection() {
}
override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList<Any>) {
with(holder.itemView) {
tv_name.text = item.name

@ -175,7 +175,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
okButton { viewModel.delSelection(adapter.getSelection()) }
noButton { }
}
.show()
.show().applyTint()
}
})

@ -165,7 +165,7 @@ class RssSourceActivity : VMBaseActivity<RssSourceViewModel>(R.layout.activity_r
okButton { viewModel.delSelection(adapter.getSelection()) }
noButton { }
}
.show()
.show().applyTint()
}
})
}

@ -17,46 +17,9 @@
android:layout_height="0dp"
android:layout_weight="1" />
<!--文件点击按钮-->
<LinearLayout
<io.legado.app.ui.widget.SelectActionBar
android:id="@+id/select_action_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/background_menu"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="6dp"
android:paddingBottom="6dp"
android:elevation="2dp">
<io.legado.app.lib.theme.view.ATECheckBox
android:id="@+id/cb_selected_all"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="5dp"
android:text="@string/select_all_count"
android:textColor="@color/tv_text_default" />
<io.legado.app.lib.theme.view.ATEAccentStrokeTextView
android:id="@+id/btn_to_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:gravity="center"
android:minWidth="80dp"
android:text="@string/move_to_group" />
<io.legado.app.lib.theme.view.ATEAccentStrokeTextView
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:minWidth="80dp"
android:gravity="center"
android:text="@string/delete" />
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_del_selection"
android:title="@string/delete" />
</menu>
Loading…
Cancel
Save