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. 17
      app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt

@ -66,6 +66,9 @@ class ReplaceEditDialog : DialogFragment(),
override fun onMenuItemClick(item: MenuItem?): Boolean { override fun onMenuItemClick(item: MenuItem?): Boolean {
when (item?.itemId) { when (item?.itemId) {
R.id.menu_save -> { R.id.menu_save -> {
viewModel.save(getReplaceRule()) {
dismiss()
}
} }
} }
return true return true
@ -80,4 +83,14 @@ class ReplaceEditDialog : DialogFragment(),
et_scope.setText(replaceRule.scope) 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) { fun save(replaceRule: ReplaceRule, success: () -> Unit) {
execute { execute {
if (replaceRule.order == 0) {
replaceRule.order = App.db.replaceRuleDao().maxOrder + 1
}
App.db.replaceRuleDao().insert(replaceRule) App.db.replaceRuleDao().insert(replaceRule)
}.onSuccess { }.onSuccess {
success() success()

@ -49,10 +49,19 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) :
val srcItem = getItem(srcPosition) val srcItem = getItem(srcPosition)
val targetItem = getItem(targetPosition) val targetItem = getItem(targetPosition)
if (srcItem != null && targetItem != null) { if (srcItem != null && targetItem != null) {
val srcOrder = srcItem.order if (srcItem.order == targetItem.order) {
srcItem.order = targetItem.order if (targetPosition > srcPosition) {
targetItem.order = srcOrder srcItem.order = srcItem.order + 1
callBack.update(srcItem, targetItem) } 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 return true
} }

Loading…
Cancel
Save