From 5c5a444a2fc336129c1766cf0296cc74e6d298c3 Mon Sep 17 00:00:00 2001 From: kunfei Date: Tue, 11 Feb 2020 14:35:25 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/arrange/ArrangeBookAdapter.kt | 9 ++- .../app/ui/importbook/ImportBookActivity.kt | 81 ++++++++----------- .../app/ui/importbook/ImportBookAdapter.kt | 15 +++- .../main/res/layout/activity_import_book.xml | 44 +--------- app/src/main/res/menu/import_book_sel.xml | 8 ++ 5 files changed, 68 insertions(+), 89 deletions(-) create mode 100644 app/src/main/res/menu/import_book_sel.xml diff --git a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt index 7a0f17a23..2135e2903 100644 --- a/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/arrange/ArrangeBookAdapter.kt @@ -31,7 +31,14 @@ class ArrangeBookAdapter(context: Context, val callBack: CallBack) : } 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) { diff --git a/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt b/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt index bbcdf6954..8afa2793b 100644 --- a/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/importbook/ImportBookActivity.kt @@ -7,6 +7,7 @@ import android.net.Uri import android.os.Bundle import android.view.Menu import android.view.MenuItem +import androidx.appcompat.widget.PopupMenu import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.LiveData import androidx.lifecycle.Observer @@ -16,6 +17,7 @@ import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.help.AppConfig 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.getViewModel import kotlinx.android.synthetic.main.activity_import_book.* @@ -28,6 +30,7 @@ import java.io.File class ImportBookActivity : VMBaseActivity(R.layout.activity_import_book), + PopupMenu.OnMenuItemClickListener, ImportBookAdapter.CallBack { private val requestCodeSelectFolder = 342 private var rootDoc: DocumentFile? = null @@ -50,37 +53,52 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity 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() { recycler_view.layoutManager = LinearLayoutManager(this) adapter = ImportBookAdapter(this, this) recycler_view.adapter = adapter 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() { tv_go_back.onClick { 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 { - viewModel.deleteDoc(adapter.selectedUris) { + return super.onCompatOptionsItemSelected(item) + } + + override fun onMenuItemClick(item: MenuItem?): Boolean { + when (item?.itemId) { + R.id.menu_del_selection -> + viewModel.deleteDoc(adapter.selectedUris) { upPath() } } - cb_selected_all.onClick { - adapter.selectAll(cb_selected_all.isChecked) - } + return false } private fun initData() { @@ -186,36 +204,7 @@ class ImportBookActivity : VMBaseActivity(R.layout.activity } override fun upCountView() { - if (adapter.checkableCount == 0) { - 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 + select_action_bar.upCountView(adapter.selectedUris.size, adapter.checkableCount) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt b/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt index 94deb2b7e..ca98949e2 100644 --- a/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/importbook/ImportBookAdapter.kt @@ -13,7 +13,7 @@ import org.jetbrains.anko.sdk27.listeners.onClick class ImportBookAdapter(context: Context, val callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_import_book) { - var selectedUris = linkedSetOf() + var selectedUris = hashSetOf() var checkableCount = 0 private var bookshelf = arrayListOf() @@ -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) { holder.itemView.apply { if (item.isDir) { diff --git a/app/src/main/res/layout/activity_import_book.xml b/app/src/main/res/layout/activity_import_book.xml index e2b14305b..e3473f202 100644 --- a/app/src/main/res/layout/activity_import_book.xml +++ b/app/src/main/res/layout/activity_import_book.xml @@ -68,47 +68,9 @@ - - - - - - - - - - + android:layout_height="wrap_content" /> \ No newline at end of file diff --git a/app/src/main/res/menu/import_book_sel.xml b/app/src/main/res/menu/import_book_sel.xml new file mode 100644 index 000000000..1985d09cd --- /dev/null +++ b/app/src/main/res/menu/import_book_sel.xml @@ -0,0 +1,8 @@ + + + + + + \ No newline at end of file