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 39dc91264..334bb7a82 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 @@ -11,7 +11,7 @@ interface BookSourceDao { @Query("select * from book_sources order by customOrder asc") fun liveDataAll(): LiveData> - @Query("select * from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceUrl like :searchKey order by customOrder asc") + @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") @@ -23,7 +23,7 @@ interface BookSourceDao { @Query("select bookSourceGroup from book_sources where bookSourceGroup is not null and bookSourceGroup <> ''") fun liveGroup(): LiveData> - @Query("select distinct enabled from book_sources where bookSourceName like :searchKey or `bookSourceGroup` like :searchKey or bookSourceUrl like :searchKey") + @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 = 1 where bookSourceUrl in (:sourceUrls)") diff --git a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt index e931f25d7..9390b04ac 100644 --- a/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt +++ b/app/src/main/java/io/legado/app/data/dao/RssSourceDao.kt @@ -16,11 +16,14 @@ interface RssSourceDao { @Query("SELECT * FROM rssSources") fun liveAll(): LiveData> + @Query("SELECT * FROM rssSources where sourceName like :key or sourceUrl like :key or sourceGroup like :key") + fun liveSearch(key: String): LiveData> + @Query("SELECT * FROM rssSources where enabled = 1") fun liveEnabled(): LiveData> - @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insert(vararg rssSource: RssSource) + @Query("select sourceGroup from rssSources where sourceGroup is not null and sourceGroup <> ''") + fun liveGroup(): LiveData> @Query("update rssSources set enabled = 1 where sourceUrl in (:sourceUrls)") fun enableSection(vararg sourceUrls: String) @@ -31,6 +34,9 @@ interface RssSourceDao { @Query("delete from rssSources where sourceUrl in (:sourceUrls)") fun delSection(vararg sourceUrls: String) + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insert(vararg rssSource: RssSource) + @Update fun update(vararg rssSource: RssSource) diff --git a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt index b2d996fb2..4ef9c9a0f 100644 --- a/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/source/manage/RssSourceActivity.kt @@ -3,6 +3,7 @@ package io.legado.app.ui.rss.source.manage import android.os.Bundle import android.view.Menu import android.view.MenuItem +import android.view.SubMenu import androidx.appcompat.widget.SearchView import androidx.core.content.ContextCompat import androidx.lifecycle.LiveData @@ -20,6 +21,7 @@ import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.primaryTextColor import io.legado.app.ui.rss.source.edit.RssSourceEditActivity import io.legado.app.utils.getViewModel +import io.legado.app.utils.splitNotBlank import kotlinx.android.synthetic.main.activity_rss_source.* import kotlinx.android.synthetic.main.view_search.* import kotlinx.android.synthetic.main.view_title_bar.* @@ -34,12 +36,15 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r private lateinit var adapter: RssSourceAdapter private var sourceLiveData: LiveData>? = null + private var groups = hashSetOf() + private var groupMenu: SubMenu? = null override fun onActivityCreated(savedInstanceState: Bundle?) { setSupportActionBar(toolbar) initRecyclerView() initSearchView() - initData() + initLiveDataGroup() + initLiveDataSource() } override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { @@ -47,6 +52,12 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r return super.onCompatCreateOptionsMenu(menu) } + override fun onPrepareOptionsMenu(menu: Menu?): Boolean { + groupMenu = menu?.findItem(R.id.menu_group)?.subMenu + upGroupMenu() + return super.onPrepareOptionsMenu(menu) + } + override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { when (item.itemId) { R.id.menu_add -> startActivity() @@ -92,9 +103,31 @@ class RssSourceActivity : VMBaseActivity(R.layout.activity_r }) } - private fun initData() { + private fun initLiveDataGroup() { + App.db.rssSourceDao().liveGroup().observe(this, Observer { + groups.clear() + it.map { group -> + groups.addAll(group.splitNotBlank(",", ";")) + } + upGroupMenu() + }) + } + + private fun upGroupMenu() { + groupMenu?.removeGroup(R.id.source_group) + groups.map { + groupMenu?.add(R.id.source_group, Menu.NONE, Menu.NONE, it) + } + } + + private fun initLiveDataSource(key: String? = null) { sourceLiveData?.removeObservers(this) - sourceLiveData = App.db.rssSourceDao().liveAll() + sourceLiveData = + if (key.isNullOrBlank()) { + App.db.rssSourceDao().liveAll() + } else { + App.db.rssSourceDao().liveSearch("%$key%") + } sourceLiveData?.observe(this, Observer { val diffResult = DiffUtil .calculateDiff(DiffCallBack(adapter.getItems(), it))