pull/32/head
kunfei 5 years ago
parent e6f66f23c3
commit ded07def0e
  1. 13
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditDialog.kt
  2. 3
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditViewModel.kt
  3. 9
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt

@ -66,6 +66,9 @@ class ReplaceEditDialog : DialogFragment(),
override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) {
R.id.menu_save -> {
viewModel.save(getReplaceRule()) {
dismiss()
}
}
}
return true
@ -80,4 +83,14 @@ class ReplaceEditDialog : DialogFragment(),
et_scope.setText(replaceRule.scope)
}
private fun getReplaceRule(): ReplaceRule {
val replaceRule: ReplaceRule = viewModel.replaceRuleData.value ?: ReplaceRule()
replaceRule.name = et_name.text.toString()
replaceRule.group = et_group.text.toString()
replaceRule.pattern = et_group.text.toString()
replaceRule.isRegex = cb_use_regex.isChecked
replaceRule.replacement = et_replace_to.text.toString()
replaceRule.scope = et_scope.text.toString()
return replaceRule
}
}

@ -26,6 +26,9 @@ class ReplaceEditViewModel(application: Application) : BaseViewModel(application
fun save(replaceRule: ReplaceRule, success: () -> Unit) {
execute {
if (replaceRule.order == 0) {
replaceRule.order = App.db.replaceRuleDao().maxOrder + 1
}
App.db.replaceRuleDao().insert(replaceRule)
}.onSuccess {
success()

@ -49,11 +49,20 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
val srcItem = getItem(srcPosition)
val targetItem = getItem(targetPosition)
if (srcItem != null && targetItem != null) {
if (srcItem.order == targetItem.order) {
if (targetPosition > srcPosition) {
srcItem.order = srcItem.order + 1
} else {
srcItem.order = srcItem.order - 1
}
callBack.update(srcItem)
} else {
val srcOrder = srcItem.order
srcItem.order = targetItem.order
targetItem.order = srcOrder
callBack.update(srcItem, targetItem)
}
}
return true
}

Loading…
Cancel
Save