Skip to content

Commit 4d02537

Browse files
committed
feat: auto fix group key
1 parent e576814 commit 4d02537

File tree

1 file changed

+55
-9
lines changed

1 file changed

+55
-9
lines changed

app/src/main/kotlin/li/songe/gkd/ui/UpsertRuleGroupVm.kt

Lines changed: 55 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class UpsertRuleGroupVm(val route: UpsertRuleGroupRoute) : ViewModel() {
5858
toast("规则无变动")
5959
return
6060
}
61-
val jsonObject = runCatching { Json5.parseToJson5Element(text) }.run {
61+
var jsonObject = runCatching { Json5.parseToJson5Element(text) }.run {
6262
if (isFailure) {
6363
error("非法格式\n${exceptionOrNull()?.message}")
6464
}
@@ -67,12 +67,28 @@ class UpsertRuleGroupVm(val route: UpsertRuleGroupRoute) : ViewModel() {
6767
if (jsonObject !is JsonObject) {
6868
error("规则应为对象格式")
6969
}
70+
// 自动填充 key
71+
if (jsonObject["name"] != null && jsonObject["key"] == null) {
72+
jsonObject = JsonObject(jsonObject + mapOf("key" to JsonPrimitive(groupKey ?: 0)))
73+
}
74+
if (jsonObject["id"] is JsonPrimitive && jsonObject["groups"] is JsonArray) {
75+
val groups = jsonObject["groups"] as JsonArray
76+
val newGroups = groups.map {
77+
if (it is JsonObject && it["name"] != null && it["key"] == null) {
78+
JsonObject(it + mapOf("key" to JsonPrimitive(groupKey ?: 0)))
79+
} else {
80+
it
81+
}
82+
}
83+
jsonObject = JsonObject(mapOf("groups" to JsonArray(newGroups)) + jsonObject)
84+
}
85+
7086
if (jsonObject == initialGroup?.cacheJsonObject) {
7187
toast("规则无变动")
7288
return
7389
}
7490
if (groupKey != null) {
75-
val newGroup = try {
91+
var newGroup = try {
7692
if (appId != null) {
7793
if (jsonObject["groups"] is JsonArray) {
7894
val id = jsonObject["id"] ?: error("缺少id")
@@ -97,7 +113,15 @@ class UpsertRuleGroupVm(val route: UpsertRuleGroupRoute) : ViewModel() {
97113
}
98114
newGroup.errorDesc?.let(::error)
99115
if (newGroup.key != groupKey) {
100-
error("不能更改规则的key")
116+
// 自动修正 key 与原来一致
117+
newGroup = when (newGroup) {
118+
is RawSubscription.RawAppGroup -> newGroup.copy(key = groupKey)
119+
is RawSubscription.RawGlobalGroup -> newGroup.copy(key = groupKey)
120+
}
121+
}
122+
if (newGroup == initialGroup) {
123+
toast("规则无变动")
124+
return
101125
}
102126
val newSubs = if (appId != null) {
103127
newGroup as RawSubscription.RawAppGroup
@@ -122,7 +146,7 @@ class UpsertRuleGroupVm(val route: UpsertRuleGroupRoute) : ViewModel() {
122146
updateSubscription(newSubs)
123147
} else {
124148
if (isAddAnyApp) {
125-
val newApp = try {
149+
var newApp = try {
126150
RawSubscription.parseApp(jsonObject).apply {
127151
if (groups.isEmpty()) {
128152
error("至少输入一个规则")
@@ -137,6 +161,17 @@ class UpsertRuleGroupVm(val route: UpsertRuleGroupRoute) : ViewModel() {
137161
newApp.groups.forEach { g ->
138162
checkGroupKeyName(oldApp.groups, g)
139163
}
164+
// 自动修正 key 与原来不重复
165+
val usedKeys = oldApp.groups.map { it.key }.toHashSet()
166+
newApp = newApp.copy(groups = newApp.groups.map { g ->
167+
if (g.key in usedKeys) {
168+
g.copy(key = usedKeys.max() + 1).also {
169+
usedKeys.add(it.key)
170+
}
171+
} else {
172+
g
173+
}
174+
})
140175
}
141176
val newSubs = subs.copy(apps = subs.apps.toMutableList().apply {
142177
val i = indexOfFirst { a -> a.id == newApp.id }
@@ -153,7 +188,7 @@ class UpsertRuleGroupVm(val route: UpsertRuleGroupRoute) : ViewModel() {
153188
updateSubscription(newSubs)
154189
} else if (appId != null) {
155190
// add specified app group
156-
val newGroups = try {
191+
var newGroups = try {
157192
if (jsonObject["groups"] is JsonArray) {
158193
val id = jsonObject["id"] ?: error("缺少id")
159194
if (!(id is JsonPrimitive && id.isString && id.content == appId)) {
@@ -176,6 +211,17 @@ class UpsertRuleGroupVm(val route: UpsertRuleGroupRoute) : ViewModel() {
176211
checkGroupKeyName(oldApp.groups, g)
177212
g.errorDesc?.let { error(it) }
178213
}
214+
// 自动修正 key 与原来不重复
215+
val usedKeys = oldApp.groups.map { it.key }.toHashSet()
216+
newGroups = newGroups.map { g ->
217+
if (g.key in usedKeys) {
218+
g.copy(key = usedKeys.max() + 1).also {
219+
usedKeys.add(it.key)
220+
}
221+
} else {
222+
g
223+
}
224+
}
179225
val newSubs = subs.copy(apps = subs.apps.toMutableList().apply {
180226
val newApp = oldApp.copy(groups = oldApp.groups + newGroups)
181227
val i = indexOfFirst { a -> a.id == newApp.id }
@@ -191,13 +237,16 @@ class UpsertRuleGroupVm(val route: UpsertRuleGroupRoute) : ViewModel() {
191237
updateSubscription(newSubs)
192238
} else {
193239
// add global group
194-
val newGroup = try {
240+
var newGroup = try {
195241
RawSubscription.parseGlobalGroup(jsonObject)
196242
} catch (e: Exception) {
197243
LogUtils.d(e)
198244
error("非法规则\n${e.message}")
199245
}
200246
checkGroupKeyName(subs.globalGroups, newGroup)
247+
if (subs.globalGroups.any { it.key == newGroup.key }) {
248+
newGroup = newGroup.copy(key = subs.globalGroups.maxOf { it.key } + 1)
249+
}
201250
updateSubscription(
202251
subs.copy(
203252
globalGroups = subs.globalGroups + newGroup
@@ -224,7 +273,4 @@ private fun checkGroupKeyName(
224273
if (groups.any { it.name == newGroup.name }) {
225274
error("已存在同名「${newGroup.name}」规则")
226275
}
227-
if (groups.any { it.key == newGroup.key }) {
228-
error("已存在同 key=${newGroup.key} 规则")
229-
}
230276
}

0 commit comments

Comments
 (0)