From ded07def0eee956ef4163fa31090ff11ac13e25f Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 7 Sep 2019 23:27:00 +0800 Subject: [PATCH] up --- .../app/ui/replacerule/ReplaceEditDialog.kt | 13 +++++++++++++ .../app/ui/replacerule/ReplaceEditViewModel.kt | 3 +++ .../app/ui/replacerule/ReplaceRuleAdapter.kt | 17 +++++++++++++---- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditDialog.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditDialog.kt index 64a84744f..0759f0a2a 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditDialog.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditDialog.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 + } } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditViewModel.kt index a13502fa4..79ab10c0c 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceEditViewModel.kt @@ -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() diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt index 92483c7d8..9d251fb01 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt @@ -49,10 +49,19 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : val srcItem = getItem(srcPosition) val targetItem = getItem(targetPosition) if (srcItem != null && targetItem != null) { - val srcOrder = srcItem.order - srcItem.order = targetItem.order - targetItem.order = srcOrder - callBack.update(srcItem, targetItem) + 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 }