pull/32/head
kunfei 5 years ago
parent 033864761f
commit 78cdea0b1b
  1. 2
      app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt
  2. 2
      app/src/main/java/io/legado/app/ui/replacerule/GroupManageDialog.kt
  3. 26
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt

@ -46,7 +46,7 @@ interface ReplaceRuleDao {
fun findEnabledByScope(name: String, origin: String): List<ReplaceRule> fun findEnabledByScope(name: String, origin: String): List<ReplaceRule>
@Query("select `group` from replace_rules") @Query("select `group` from replace_rules")
fun observeGroup(): LiveData<List<String>> fun liveGroup(): LiveData<List<String>>
@Query("select * from replace_rules where `group` like '%' || :group || '%'") @Query("select * from replace_rules where `group` like '%' || :group || '%'")
fun getByGroup(group: String): List<ReplaceRule> fun getByGroup(group: String): List<ReplaceRule>

@ -70,7 +70,7 @@ class GroupManageDialog : DialogFragment(), Toolbar.OnMenuItemClickListener {
DividerItemDecoration(requireContext(), RecyclerView.VERTICAL) DividerItemDecoration(requireContext(), RecyclerView.VERTICAL)
) )
recycler_view.adapter = adapter recycler_view.adapter = adapter
App.db.bookSourceDao().liveGroup().observe(viewLifecycleOwner, Observer { App.db.replaceRuleDao().liveGroup().observe(viewLifecycleOwner, Observer {
val groups = linkedSetOf<String>() val groups = linkedSetOf<String>()
it.map { group -> it.map { group ->
groups.addAll(group.splitNotBlank(",", ";")) groups.addAll(group.splitNotBlank(",", ";"))

@ -3,6 +3,7 @@ package io.legado.app.ui.replacerule
import android.os.Bundle import android.os.Bundle
import android.view.Menu import android.view.Menu
import android.view.MenuItem import android.view.MenuItem
import android.view.SubMenu
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
@ -18,6 +19,7 @@ import io.legado.app.data.entities.ReplaceRule
import io.legado.app.help.ItemTouchCallback import io.legado.app.help.ItemTouchCallback
import io.legado.app.lib.theme.ATH import io.legado.app.lib.theme.ATH
import io.legado.app.utils.getViewModel import io.legado.app.utils.getViewModel
import io.legado.app.utils.splitNotBlank
import kotlinx.android.synthetic.main.activity_replace_rule.* import kotlinx.android.synthetic.main.activity_replace_rule.*
import org.jetbrains.anko.doAsync import org.jetbrains.anko.doAsync
@ -30,6 +32,8 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
private lateinit var adapter: ReplaceRuleAdapter private lateinit var adapter: ReplaceRuleAdapter
private var rulesLiveData: LiveData<PagedList<ReplaceRule>>? = null private var rulesLiveData: LiveData<PagedList<ReplaceRule>>? = null
private var allEnabled = false private var allEnabled = false
private var groups = hashSetOf<String>()
private var groupMenu: SubMenu? = null
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
initRecyclerView() initRecyclerView()
@ -41,6 +45,12 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
return super.onCompatCreateOptionsMenu(menu) 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 { override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.menu_add_replace_rule -> R.id.menu_add_replace_rule ->
@ -68,11 +78,25 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view) ItemTouchHelper(itemTouchCallback).attachToRecyclerView(recycler_view)
} }
private fun initDataObservers() { private fun initDataObservers() {
rulesLiveData?.removeObservers(this) rulesLiveData?.removeObservers(this)
rulesLiveData = LivePagedListBuilder(App.db.replaceRuleDao().observeAll(), 30).build() rulesLiveData = LivePagedListBuilder(App.db.replaceRuleDao().observeAll(), 30).build()
rulesLiveData?.observe(this, Observer<PagedList<ReplaceRule>> { adapter.submitList(it) }) rulesLiveData?.observe(this, Observer<PagedList<ReplaceRule>> { adapter.submitList(it) })
App.db.replaceRuleDao().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 updateEnableAll() { private fun updateEnableAll() {

Loading…
Cancel
Save