From fa73b91d1db0d502d3ea99fc1e97358d3236e1b6 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 7 Sep 2019 23:45:52 +0800 Subject: [PATCH] up --- .../io/legado/app/ui/replacerule/ReplaceRuleActivity.kt | 4 ++++ .../io/legado/app/ui/replacerule/ReplaceRuleAdapter.kt | 8 ++------ .../io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt | 9 +++++++++ 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt index 8515aa180..bf942b36d 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleActivity.kt @@ -144,4 +144,8 @@ class ReplaceRuleActivity : VMBaseActivity(R.layout.activi override fun toTop(rule: ReplaceRule) { viewModel.toTop(rule) } + + override fun upOrder(rules: List) { + viewModel.upOrder(rules) + } } \ No newline at end of file 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 9d251fb01..6f41abc5a 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 @@ -50,12 +50,7 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : 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) + callBack.upOrder(getItems()) } else { val srcOrder = srcItem.order srcItem.order = targetItem.order @@ -75,5 +70,6 @@ class ReplaceRuleAdapter(context: Context, var callBack: CallBack) : fun delete(rule: ReplaceRule) fun edit(rule: ReplaceRule) fun toTop(rule: ReplaceRule) + fun upOrder(rules: List) } } diff --git a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt index 4674ab415..755befb75 100644 --- a/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/replacerule/ReplaceRuleViewModel.kt @@ -29,6 +29,15 @@ class ReplaceRuleViewModel(application: Application) : BaseViewModel(application } } + fun upOrder(rules: List) { + execute { + for ((index: Int, rule: ReplaceRule) in rules.withIndex()) { + rule.order = index + 1 + } + App.db.replaceRuleDao().update(*rules.toTypedArray()) + } + } + fun addGroup(group: String) { execute { val sources = App.db.replaceRuleDao().noGroup