pull/32/head
kunfei 5 years ago
parent bc31589bdf
commit 2414f38afc
  1. 3
      app/src/main/java/io/legado/app/data/dao/ReplaceRuleDao.kt
  2. 12
      app/src/main/java/io/legado/app/ui/replacerule/DiffCallBack.kt
  3. 2
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt
  4. 28
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt
  5. 6
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt
  6. 49
      app/src/main/res/menu/replace_rule.xml
  7. 3
      app/src/main/res/values/strings.xml

@ -32,6 +32,9 @@ interface ReplaceRuleDao {
@Query("SELECT * FROM replace_rules WHERE id in (:ids)") @Query("SELECT * FROM replace_rules WHERE id in (:ids)")
fun findByIds(vararg ids: Long): List<ReplaceRule> fun findByIds(vararg ids: Long): List<ReplaceRule>
@Query("update replace_rules set isEnabled = 1 where id in (:ids)")
fun enableSection(vararg ids: Long)
@Query( @Query(
"""SELECT * FROM replace_rules WHERE isEnabled = 1 """SELECT * FROM replace_rules WHERE isEnabled = 1
AND (scope LIKE '%' || :scope || '%' or scope = null or scope = '')""" AND (scope LIKE '%' || :scope || '%' or scope = null or scope = '')"""

@ -25,8 +25,18 @@ class DiffCallBack(
val oldItem = oldItems[oldItemPosition] val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition] val newItem = newItems[newItemPosition]
return oldItem.name == newItem.name return oldItem.name == newItem.name
&& oldItem.group == newItem.group
&& oldItem.isEnabled == newItem.isEnabled && oldItem.isEnabled == newItem.isEnabled
} }
override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
val oldItem = oldItems[oldItemPosition]
val newItem = newItems[newItemPosition]
return when {
oldItem.name == newItem.name
&& oldItem.group == newItem.group
&& oldItem.isEnabled != newItem.isEnabled -> 2
else -> null
}
}
} }

@ -61,6 +61,8 @@ class ReplaceRuleActivity : VMBaseActivity<ReplaceRuleViewModel>(R.layout.activi
R.id.menu_group_manage -> R.id.menu_group_manage ->
GroupManageDialog().show(supportFragmentManager, "groupManage") GroupManageDialog().show(supportFragmentManager, "groupManage")
R.id.menu_select_all -> adapter.selectAll() R.id.menu_select_all -> adapter.selectAll()
R.id.menu_revert_selection -> adapter.revertSelection()
R.id.menu_enable_selection -> viewModel.enableSelection(adapter.selectedIds)
} }
return super.onCompatOptionsItemSelected(item) return super.onCompatOptionsItemSelected(item)
} }

@ -29,6 +29,17 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
} }
} }
fun revertSelection() {
getItems().forEach {
if (selectedIds.contains(it.id)) {
selectedIds.remove(it.id)
} else {
selectedIds.add(it.id)
}
}
notifyItemRangeChanged(0, itemCount, 1)
}
override fun convert(holder: ItemViewHolder, item: ReplaceRule, payloads: MutableList<Any>) { override fun convert(holder: ItemViewHolder, item: ReplaceRule, payloads: MutableList<Any>) {
with(holder.itemView) { with(holder.itemView) {
if (payloads.isEmpty()) { if (payloads.isEmpty()) {
@ -42,6 +53,14 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
iv_edit.onClick { iv_edit.onClick {
callBack.edit(item) callBack.edit(item)
} }
cb_name.isChecked = selectedIds.contains(item.id)
cb_name.onClick {
if (cb_name.isChecked) {
selectedIds.add(item.id)
} else {
selectedIds.remove(item.id)
}
}
iv_menu_more.onClick { iv_menu_more.onClick {
val popupMenu = PopupMenu(context, it) 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_top, Menu.NONE, R.string.to_top)
@ -55,17 +74,10 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
} }
popupMenu.show() popupMenu.show()
} }
cb_name.onClick {
if (cb_name.isChecked) {
selectedIds.add(item.id)
} else {
selectedIds.remove(item.id)
}
}
cb_name.isChecked = selectedIds.contains(item.id)
} else { } else {
when (payloads[0]) { when (payloads[0]) {
1 -> cb_name.isChecked = selectedIds.contains(item.id) 1 -> cb_name.isChecked = selectedIds.contains(item.id)
2 -> swt_enabled.isChecked = item.isEnabled
} }
} }
} }

@ -39,6 +39,12 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application
} }
} }
fun enableSelection(ids: LinkedHashSet<Long>) {
execute {
App.db.replaceRuleDao().enableSection(*ids.toLongArray())
}
}
fun addGroup(group: String) { fun addGroup(group: String) {
execute { execute {
val sources = App.db.replaceRuleDao().noGroup val sources = App.db.replaceRuleDao().noGroup

@ -4,11 +4,42 @@
xmlns:tools="http://schemas.android.com/tools"> xmlns:tools="http://schemas.android.com/tools">
<item <item
android:id="@+id/menu_select_all" android:id="@+id/menu_select_action"
android:icon="@drawable/ic_select_all" android:icon="@drawable/ic_select_all"
android:title="@string/select_all" android:title="@string/select_action"
app:showAsAction="always" app:showAsAction="always"
tools:ignore="AlwaysShowAction" /> tools:ignore="AlwaysShowAction">
<menu>
<item
android:id="@+id/menu_select_all"
android:title="@string/select_all"
app:showAsAction="never" />
<item
android:id="@+id/menu_revert_selection"
android:title="@string/revert_selection"
app:showAsAction="never" />
<item
android:id="@+id/menu_enable_selection"
android:title="@string/enable_selection"
app:showAsAction="never" />
<item
android:id="@+id/menu_disable_selection"
android:title="@string/disable_selection"
app:showAsAction="never" />
<item
android:id="@+id/menu_del_select"
android:title="@string/del_select"
app:showAsAction="never" />
</menu>
</item>
<item <item
android:id="@+id/menu_group" android:id="@+id/menu_group"
@ -55,16 +86,4 @@
android:title="@string/import_by_qr_code" android:title="@string/import_by_qr_code"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/menu_revert_selection"
android:icon="@drawable/ic_swap_outline_24dp"
android:title="@string/revert_selection"
app:showAsAction="never" />
<item
android:id="@+id/menu_del_select"
android:icon="@drawable/ic_clear_all"
android:title="@string/del_select"
app:showAsAction="never" />
</menu> </menu>

@ -180,6 +180,7 @@
<string name="clear_all_content">清除缓存会删除所有已保存章节,是否确认删除?</string> <string name="clear_all_content">清除缓存会删除所有已保存章节,是否确认删除?</string>
<string name="book_source_share_url">书源共享</string> <string name="book_source_share_url">书源共享</string>
<string name="replace_rule_summary">替换规则名称</string> <string name="replace_rule_summary">替换规则名称</string>
<string name="select_action">选择操作</string>
<string name="select_all">全选</string> <string name="select_all">全选</string>
<string name="night_theme">夜间模式</string> <string name="night_theme">夜间模式</string>
<string name="welcome">启动页</string> <string name="welcome">启动页</string>
@ -496,5 +497,7 @@
<string name="add_group">添加分组</string> <string name="add_group">添加分组</string>
<string name="add_replace_rule">新建替换</string> <string name="add_replace_rule">新建替换</string>
<string name="group">分组</string> <string name="group">分组</string>
<string name="enable_selection">启用所选</string>
<string name="disable_selection">禁用所选</string>
</resources> </resources>

Loading…
Cancel
Save