feat: 优化代码

pull/95/head
kunfei 5 years ago
parent 1620967826
commit 5c5a444a2f
  1. 9
      app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt
  2. 81
      app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt
  3. 15
      app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt
  4. 44
      app/src/main/res/layout/activity_import_book.xml
  5. 8
      app/src/main/res/menu/import_book_sel.xml

@ -31,7 +31,14 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) :
} }
fun revertSelection() { fun revertSelection() {
getItems().forEach {
if (selectedBooks.contains(it)) {
selectedBooks.remove(it)
} else {
selectedBooks.add(it)
}
}
callBack.upSelectCount()
} }
override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: Book, payloads: MutableList<Any>) {

@ -7,6 +7,7 @@ import android.net.Uri
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import androidx.appcompat.widget.PopupMenu
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
@ -16,6 +17,7 @@ import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.help.AppConfig import io.legado.app.help.AppConfig
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.ui.widget.SelectActionBar
import io.legado.app.utils.DocumentUtils import io.legado.app.utils.DocumentUtils
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import kotlinx.android.synthetic.main.activity_import_book.* import kotlinx.android.synthetic.main.activity_import_book.*
@ -28,6 +30,7 @@ import java.io.File
class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity_import_book), class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity_import_book),
PopupMenu.OnMenuItemClickListener,
ImportBookAdapter.CallBack { ImportBookAdapter.CallBack {
private val requestCodeSelectFolder = 342 private val requestCodeSelectFolder = 342
private var rootDoc: DocumentFile? = null private var rootDoc: DocumentFile? = null
@ -50,37 +53,52 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
return super.onCompatCreateOptionsMenu(menu) return super.onCompatCreateOptionsMenu(menu)
} }
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.menu_select_folder -> selectImportFolder()
}
return super.onCompatOptionsItemSelected(item)
}
private fun initView() { private fun initView() {
recycler_view.layoutManager = LinearLayoutManager(this) recycler_view.layoutManager = LinearLayoutManager(this)
adapter = ImportBookAdapter(this, this) adapter = ImportBookAdapter(this, this)
recycler_view.adapter = adapter recycler_view.adapter = adapter
rotate_loading.loadingColor = accentColor rotate_loading.loadingColor = accentColor
select_action_bar.setMainActionText(R.string.add_to_shelf)
select_action_bar.inflateMenu(R.menu.import_book_sel)
select_action_bar.setOnMenuItemClickListener(this)
select_action_bar.setCallBack(object : SelectActionBar.CallBack {
override fun selectAll(selectAll: Boolean) {
adapter.selectAll(selectAll)
}
override fun revertSelection() {
adapter.revertSelection()
}
override fun onClickMainAction() {
viewModel.addToBookshelf(adapter.selectedUris) {
upPath()
}
}
})
} }
private fun initEvent() { private fun initEvent() {
tv_go_back.onClick { tv_go_back.onClick {
goBackDir() goBackDir()
} }
btn_add_book.onClick { }
viewModel.addToBookshelf(adapter.selectedUris) {
upPath() override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
} when (item.itemId) {
R.id.menu_select_folder -> selectImportFolder()
} }
btn_delete.onClick { return super.onCompatOptionsItemSelected(item)
viewModel.deleteDoc(adapter.selectedUris) { }
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_del_selection ->
viewModel.deleteDoc(adapter.selectedUris) {
upPath() upPath()
} }
} }
cb_selected_all.onClick { return false
adapter.selectAll(cb_selected_all.isChecked)
}
} }
private fun initData() { private fun initData() {
@ -186,36 +204,7 @@ class ImportBookActivity : VMBaseActivity<ImportBookViewModel>(R.layout.activity
} }
override fun upCountView() { override fun upCountView() {
if (adapter.checkableCount == 0) { select_action_bar.upCountView(adapter.selectedUris.size, adapter.checkableCount)
cb_selected_all.isChecked = false
} else {
cb_selected_all.isChecked = adapter.selectedUris.size >= adapter.checkableCount
}
//重置全选的文字
if (cb_selected_all.isChecked) {
cb_selected_all.text = getString(
R.string.select_cancel_count,
adapter.selectedUris.size,
adapter.checkableCount
)
} else {
cb_selected_all.text = getString(
R.string.select_all_count,
adapter.selectedUris.size,
adapter.checkableCount
)
}
setMenuClickable(adapter.selectedUris.isNotEmpty())
}
private fun setMenuClickable(isClickable: Boolean) {
//设置是否可删除
btn_delete.isEnabled = isClickable
btn_delete.isClickable = isClickable
//设置是否可添加书籍
btn_add_book.isEnabled = isClickable
btn_add_book.isClickable = isClickable
} }
} }

@ -13,7 +13,7 @@ import org.jetbrains.anko.sdk27.listeners.onClick
class ImportBookAdapter(context: Context, val callBack: CallBack) : class ImportBookAdapter(context: Context, val callBack: CallBack) :
SimpleRecyclerAdapter<DocItem>(context, R.layout.item_import_book) { SimpleRecyclerAdapter<DocItem>(context, R.layout.item_import_book) {
var selectedUris = linkedSetOf<String>() var selectedUris = hashSetOf<String>()
var checkableCount = 0 var checkableCount = 0
private var bookshelf = arrayListOf<String>() private var bookshelf = arrayListOf<String>()
@ -55,6 +55,19 @@ class ImportBookAdapter(context: Context, val callBack: CallBack) :
} }
} }
fun revertSelection() {
getItems().forEach {
if (!it.isDir) {
if (selectedUris.contains(it.uri.toString())) {
selectedUris.remove(it.uri.toString())
} else {
selectedUris.add(it.uri.toString())
}
}
}
callBack.upCountView()
}
override fun convert(holder: ItemViewHolder, item: DocItem, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: DocItem, payloads: MutableList<Any>) {
holder.itemView.apply { holder.itemView.apply {
if (item.isDir) { if (item.isDir) {

@ -68,47 +68,9 @@
</FrameLayout> </FrameLayout>
<!--文件点击按钮--> <io.legado.app.ui.widget.SelectActionBar
<LinearLayout android:id="@+id/select_action_bar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" 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_add_book"
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/add_to_shelf" />
<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" />
</LinearLayout>
</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