diff --git a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt index a68e7e9e3..36dfc3d22 100644 --- a/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt @@ -23,8 +23,14 @@ interface BookSourceDao { @Query("select distinct enabled from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceUrl like :searchKey") fun searchIsEnable(searchKey: String = ""): List - @Query("UPDATE book_sources SET enabled = :enable where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceUrl like :searchKey") - fun enableAllSearch(searchKey: String = "", enable: String = "1") + @Query("update book_sources set enabled = 1 where bookSourceUrl in (:sourceUrls)") + fun enableSection(vararg sourceUrls: String) + + @Query("update book_sources set enabled = 0 where bookSourceUrl in (:sourceUrls)") + fun disableSection(vararg sourceUrls: String) + + @Query("delete from book_sources where bookSourceUrl in (:sourceUrls)") + fun delSection(vararg sourceUrls: String) @Query("select * from book_sources where enabledExplore = 1 order by customOrder asc") fun observeFind(): DataSource.Factory diff --git a/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt index 6de1926ef..597cbeffa 100644 --- a/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/booksource/BookSourceActivity.kt @@ -28,8 +28,6 @@ import io.legado.app.utils.splitNotBlank import kotlinx.android.synthetic.main.activity_book_source.* import kotlinx.android.synthetic.main.view_search.* import kotlinx.android.synthetic.main.view_title_bar.* -import kotlinx.coroutines.Dispatchers.IO -import kotlinx.coroutines.launch import org.jetbrains.anko.startActivity import org.jetbrains.anko.startActivityForResult @@ -72,22 +70,16 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity R.id.menu_import_book_source_qr -> { this.startActivityForResult(qrRequestCode) } - R.id.menu_select_all -> { - launch(IO) { - val isEnableList = - App.db.bookSourceDao().searchIsEnable("%${search_view.query}%") - if (isEnableList.contains(false)) { - App.db.bookSourceDao().enableAllSearch("%${search_view.query}%", "1") - } else { - App.db.bookSourceDao().enableAllSearch("%${search_view.query}%", "0") - } - } - } R.id.menu_group_manage -> GroupManageDialog().show( supportFragmentManager, "groupManage" ) R.id.menu_import_book_source_local -> Restore.importYueDuData(this) + R.id.menu_select_all -> adapter.selectAll() + R.id.menu_revert_selection -> adapter.revertSelection() + R.id.menu_enable_selection -> viewModel.enableSelection(adapter.getSelectionIds()) + R.id.menu_disable_selection -> viewModel.disableSelection(adapter.getSelectionIds()) + R.id.menu_del_selection -> viewModel.delSelection(adapter.getSelectionIds()) } if (item.groupId == R.id.source_group) { search_view.setQuery(item.title, true) @@ -179,7 +171,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity viewModel.upOrder() } - override fun topSource(bookSource: BookSource) { + override fun toTop(bookSource: BookSource) { viewModel.topSource(bookSource) } diff --git a/app/src/main/java/io/legado/app/ui/booksource/BookSourceAdapter.kt b/app/src/main/java/io/legado/app/ui/booksource/BookSourceAdapter.kt index 4e5100e25..9b4261e4c 100644 --- a/app/src/main/java/io/legado/app/ui/booksource/BookSourceAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/booksource/BookSourceAdapter.kt @@ -1,6 +1,8 @@ package io.legado.app.ui.booksource import android.content.Context +import android.view.Menu +import android.widget.PopupMenu import io.legado.app.R import io.legado.app.base.adapter.ItemViewHolder import io.legado.app.base.adapter.SimpleRecyclerAdapter @@ -14,6 +16,36 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : SimpleRecyclerAdapter(context, R.layout.item_book_source), OnItemTouchCallbackListener { + private val selectedIds = linkedSetOf() + + fun selectAll() { + getItems().forEach { + selectedIds.add(it.bookSourceUrl) + } + notifyItemRangeChanged(0, itemCount, 1) + } + + fun revertSelection() { + getItems().forEach { + if (selectedIds.contains(it.bookSourceUrl)) { + selectedIds.remove(it.bookSourceUrl) + } else { + selectedIds.add(it.bookSourceUrl) + } + } + notifyItemRangeChanged(0, itemCount, 1) + } + + fun getSelectionIds(): LinkedHashSet { + val selection = linkedSetOf() + getItems().map { + if (selectedIds.contains(it.bookSourceUrl)) { + selection.add(it.bookSourceUrl) + } + } + return selection + } + override fun onSwiped(adapterPosition: Int) { } @@ -48,9 +80,21 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : item.enabled = cb_book_source.isChecked callBack.update(item) } - iv_edit_source.onClick { callBack.edit(item) } - iv_top_source.onClick { callBack.topSource(item) } - iv_del_source.onClick { callBack.del(item) } + iv_edit.onClick { callBack.edit(item) } + iv_menu_more.onClick { + val popupMenu = PopupMenu(context, it) + popupMenu.menu.add(Menu.NONE, R.id.menu_top, Menu.NONE, R.string.to_top) + popupMenu.menu.add(Menu.NONE, R.id.menu_del, Menu.NONE, R.string.delete) + popupMenu.setOnMenuItemClickListener { menuItem -> + when (menuItem.itemId) { + R.id.menu_top -> callBack.toTop(item) + R.id.menu_del -> callBack.del(item) + } + true + } + popupMenu.show() + } + } } @@ -58,7 +102,7 @@ class BookSourceAdapter(context: Context, val callBack: CallBack) : fun del(bookSource: BookSource) fun edit(bookSource: BookSource) fun update(vararg bookSource: BookSource) - fun topSource(bookSource: BookSource) + fun toTop(bookSource: BookSource) fun upOrder() } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/booksource/BookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/booksource/BookSourceViewModel.kt index 605fefce1..d2842a579 100644 --- a/app/src/main/java/io/legado/app/ui/booksource/BookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/booksource/BookSourceViewModel.kt @@ -9,7 +9,6 @@ import io.legado.app.utils.splitNotBlank class BookSourceViewModel(application: Application) : BaseViewModel(application) { - fun topSource(bookSource: BookSource) { execute { val minXh = App.db.bookSourceDao().minOrder @@ -36,6 +35,24 @@ class BookSourceViewModel(application: Application) : BaseViewModel(application) } } + fun enableSelection(ids: LinkedHashSet) { + execute { + App.db.bookSourceDao().enableSection(*ids.toTypedArray()) + } + } + + fun disableSelection(ids: LinkedHashSet) { + execute { + App.db.bookSourceDao().disableSection(*ids.toTypedArray()) + } + } + + fun delSelection(ids: LinkedHashSet) { + execute { + App.db.bookSourceDao().delSection(*ids.toTypedArray()) + } + } + fun addGroup(group: String) { execute { val sources = App.db.bookSourceDao().noGroup diff --git a/app/src/main/res/layout/item_book_source.xml b/app/src/main/res/layout/item_book_source.xml index a95216088..53d171ab2 100644 --- a/app/src/main/res/layout/item_book_source.xml +++ b/app/src/main/res/layout/item_book_source.xml @@ -1,6 +1,6 @@ + + + android:tint="@color/tv_text_default" /> - - + android:src="@drawable/ic_more_vert" + android:tint="@color/tv_text_default" + tools:ignore="RtlHardcoded" /> \ No newline at end of file diff --git a/app/src/main/res/menu/book_source.xml b/app/src/main/res/menu/book_source.xml index ce1ab7368..6900e82ae 100644 --- a/app/src/main/res/menu/book_source.xml +++ b/app/src/main/res/menu/book_source.xml @@ -4,11 +4,42 @@ xmlns:tools="http://schemas.android.com/tools"> + tools:ignore="AlwaysShowAction"> + + + + + + + + + + + + + + + + - - - -