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 ce8dfe950..0981a94ea 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 @@ -14,10 +14,10 @@ interface BookSourceDao { @Query("select * from book_sources where bookSourceName like :searchKey or bookSourceGroup like :searchKey or bookSourceUrl like :searchKey order by customOrder asc") fun liveDataSearch(searchKey: String = ""): LiveData> - @Query("select * from book_sources where enabledExplore = 1 and exploreUrl is not null and exploreUrl <> '' order by customOrder asc") + @Query("select * from book_sources where enabled = 1 and enabledExplore = 1 and exploreUrl is not null and exploreUrl <> '' order by customOrder asc") fun liveExplore(): LiveData> - @Query("select * from book_sources where enabledExplore = 1 and exploreUrl is not null and exploreUrl <> '' and (bookSourceGroup like :key or bookSourceName like :key) order by customOrder asc") + @Query("select * from book_sources where enabled = 1 and enabledExplore = 1 and exploreUrl is not null and exploreUrl <> '' and (bookSourceGroup like :key or bookSourceName like :key) order by customOrder asc") fun liveExplore(key: String): LiveData> @Query("select bookSourceGroup from book_sources where bookSourceGroup is not null and bookSourceGroup <> ''") @@ -26,6 +26,9 @@ interface BookSourceDao { @Query("select bookSourceGroup from book_sources where enabled = 1 and bookSourceGroup is not null and bookSourceGroup <> ''") fun liveGroupEnabled(): LiveData> + @Query("select bookSourceGroup from book_sources where enabled = 1 and enabledExplore = 1 and exploreUrl is not null and exploreUrl <> '' and bookSourceGroup is not null and bookSourceGroup <> ''") + fun liveGroupExplore(): LiveData> + @Query("select distinct enabled from book_sources where bookSourceName like :searchKey or bookSourceGroup like :searchKey or bookSourceUrl like :searchKey") fun searchIsEnable(searchKey: String = ""): List diff --git a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt index f4c8bfdf2..b5575c869 100644 --- a/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt @@ -55,7 +55,7 @@ class BookSourceActivity : VMBaseActivity(R.layout.activity private val importSource = 132 private lateinit var adapter: BookSourceAdapter private var bookSourceLiveDate: LiveData>? = null - private var groups = hashSetOf() + private var groups = linkedSetOf() private var groupMenu: SubMenu? = null private lateinit var selMenu: PopupMenu diff --git a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt index 80fb39212..3cdd759cf 100644 --- a/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt +++ b/app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt @@ -1,6 +1,9 @@ package io.legado.app.ui.main.explore import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.view.SubMenu import android.view.View import androidx.appcompat.widget.SearchView import androidx.lifecycle.LiveData @@ -17,6 +20,7 @@ import io.legado.app.lib.theme.primaryTextColor import io.legado.app.ui.book.source.edit.BookSourceEditActivity import io.legado.app.ui.explore.ExploreShowActivity import io.legado.app.utils.getViewModel +import io.legado.app.utils.splitNotBlank import io.legado.app.utils.startActivity import kotlinx.android.synthetic.main.fragment_find_book.* import kotlinx.android.synthetic.main.view_search.* @@ -30,13 +34,24 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_find_ private lateinit var adapter: ExploreAdapter private lateinit var linearLayoutManager: LinearLayoutManager + private val groups = linkedSetOf() + private var liveGroup: LiveData>? = null private var liveExplore: LiveData>? = null + private var groupsMenu: SubMenu? = null override fun onViewCreated(view: View, savedInstanceState: Bundle?) { setSupportToolbar(toolbar) initSearchView() initRecyclerView() - initData() + initGroupData() + initExploreData() + } + + override fun onCompatCreateOptionsMenu(menu: Menu) { + super.onCompatCreateOptionsMenu(menu) + menuInflater.inflate(R.menu.main_explore, menu) + groupsMenu = menu.findItem(R.id.menu_group)?.subMenu + upGroupsMenu() } private fun initSearchView() { @@ -51,7 +66,7 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_find_ } override fun onQueryTextChange(newText: String?): Boolean { - initData(newText) + initExploreData(newText) return false } }) @@ -74,7 +89,19 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_find_ }) } - private fun initData(key: String? = null) { + private fun initGroupData() { + liveGroup?.removeObservers(viewLifecycleOwner) + liveGroup = App.db.bookSourceDao().liveGroupExplore() + liveGroup?.observe(viewLifecycleOwner, Observer { + groups.clear() + it.map { group -> + groups.addAll(group.splitNotBlank(",", ";")) + } + upGroupsMenu() + }) + } + + private fun initExploreData(key: String? = null) { liveExplore?.removeObservers(viewLifecycleOwner) liveExplore = if (key.isNullOrBlank()) { App.db.bookSourceDao().liveExplore() @@ -89,6 +116,22 @@ class ExploreFragment : VMBaseFragment(R.layout.fragment_find_ }) } + private fun upGroupsMenu() { + groupsMenu?.let { subMenu -> + subMenu.removeGroup(R.id.menu_group_text) + groups.forEach { + subMenu.add(R.id.menu_group_text, Menu.NONE, Menu.NONE, it) + } + } + } + + override fun onCompatOptionsItemSelected(item: MenuItem) { + super.onCompatOptionsItemSelected(item) + if (item.groupId == R.id.menu_group_text) { + search_view.setQuery(item.title, true) + } + } + override fun scrollTo(pos: Int) { rv_find.smoothScrollToPosition(pos) } diff --git a/app/src/main/res/menu/main_explore.xml b/app/src/main/res/menu/main_explore.xml new file mode 100644 index 000000000..5d429a443 --- /dev/null +++ b/app/src/main/res/menu/main_explore.xml @@ -0,0 +1,15 @@ + + + + + + + + + + \ No newline at end of file