feat: 发现界面分组显示

pull/95/head
kunfei 5 years ago
parent d9f3534eec
commit 790047d609
  1. 7
      app/src/main/java/io/legado/app/data/dao/BookSourceDao.kt
  2. 2
      app/src/main/java/io/legado/app/ui/book/source/manage/BookSourceActivity.kt
  3. 49
      app/src/main/java/io/legado/app/ui/main/explore/ExploreFragment.kt
  4. 15
      app/src/main/res/menu/main_explore.xml

@ -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<List<BookSource>>
@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<List<BookSource>>
@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<List<BookSource>>
@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<List<String>>
@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<List<String>>
@Query("select distinct enabled from book_sources where bookSourceName like :searchKey or bookSourceGroup like :searchKey or bookSourceUrl like :searchKey")
fun searchIsEnable(searchKey: String = ""): List<Boolean>

@ -55,7 +55,7 @@ class BookSourceActivity : VMBaseActivity<BookSourceViewModel>(R.layout.activity
private val importSource = 132
private lateinit var adapter: BookSourceAdapter
private var bookSourceLiveDate: LiveData<List<BookSource>>? = null
private var groups = hashSetOf<String>()
private var groups = linkedSetOf<String>()
private var groupMenu: SubMenu? = null
private lateinit var selMenu: PopupMenu

@ -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<ExploreViewModel>(R.layout.fragment_find_
private lateinit var adapter: ExploreAdapter
private lateinit var linearLayoutManager: LinearLayoutManager
private val groups = linkedSetOf<String>()
private var liveGroup: LiveData<List<String>>? = null
private var liveExplore: LiveData<List<BookSource>>? = 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<ExploreViewModel>(R.layout.fragment_find_
}
override fun onQueryTextChange(newText: String?): Boolean {
initData(newText)
initExploreData(newText)
return false
}
})
@ -74,7 +89,19 @@ class ExploreFragment : VMBaseFragment<ExploreViewModel>(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<ExploreViewModel>(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)
}

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/menu_group"
android:title="@string/group"
android:icon="@drawable/ic_groups"
app:showAsAction="always">
<menu />
</item>
</menu>
Loading…
Cancel
Save