Skip to content

Commit

Permalink
fix: 添加规则 key 处理错误 (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Nov 17, 2023
1 parent 5a03ce5 commit 1e84805
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions app/src/main/java/li/songe/gkd/ui/SubsPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ fun SubsPage(
ToastUtils.showShort("非法规则${e.message}")
return@TextButton
}
if (newAppRaw.groups.isEmpty()) {
ToastUtils.showShort("不允许添加空规则组")
return@TextButton
}
if (newAppRaw.groups.any { s -> s.name.isBlank() }) {
ToastUtils.showShort("不允许添加空白名规则组,请先命名")
return@TextButton
Expand All @@ -252,20 +256,29 @@ fun SubsPage(
}
}
}
// 重写添加的规则的 key
val initKey =
((oldAppRaw?.groups ?: emptyList()).maxByOrNull { g -> g.key }?.key ?: -1) + 1
val finalAppRaw = if (oldAppRaw != null) {
newAppRaw.copy(groups = oldAppRaw.groups + newAppRaw.groups.mapIndexed { i, g ->
g.copy(
key = initKey + i
)
})
} else {
newAppRaw.copy(groups = newAppRaw.groups.mapIndexed { i, g ->
g.copy(
key = initKey + i
)
})
}
val newApps = if (oldAppRaw != null) {
val oldKey = oldAppRaw.groups.maxBy { g -> g.key }.key + 1
val finalAppRaw =
newAppRaw.copy(groups = oldAppRaw.groups + newAppRaw.groups.mapIndexed { i, g ->
g.copy(
key = oldKey + i
)
})
subsRaw.apps.toMutableList().apply {
set(oldAppRawIndex, finalAppRaw)
}
} else {
subsRaw.apps.toMutableList().apply {
add(newAppRaw)
add(finalAppRaw)
}
}
vm.viewModelScope.launchTry {
Expand Down

0 comments on commit 1e84805

Please sign in to comment.