Revert "Use ruleset IDs for routing edits"

This reverts commit c548e5f615.
This commit is contained in:
2dust committed 2026-09-05 14:50:28 +08:00
1 parent 4d8ee1a916
commit 1af8f4677c
3 files changed
+31 -47

No files matched your search

@@ -123,14 +123,17 @@ object SettingsManager {
}
/**
* Get a routing ruleset by id.
* @param id The id of the ruleset.
* Get a routing ruleset by index.
* @param index The index of the ruleset.
* @return The RulesetItem.
*/
fun getRoutingRuleset(index: String?): RulesetItem? {
if (index.isNullOrEmpty()) return null
fun getRoutingRuleset(index: Int): RulesetItem? {
if (index < 0) return null
return MmkvManager.decodeRoutingRulesets()?.firstOrNull { it.id == index }
val rulesetList = MmkvManager.decodeRoutingRulesets()
if (rulesetList.isNullOrEmpty()) return null
return rulesetList[index]
}
/**
@@ -154,32 +157,17 @@ object SettingsManager {
MmkvManager.encodeRoutingRulesets(rulesetList)
}
fun saveRoutingRuleset(index: String?, ruleset: RulesetItem?) {
if (ruleset == null) return
if (ruleset.id.isBlank()) {
ruleset.id = java.util.UUID.randomUUID().toString()
}
/**
* Remove a routing ruleset by index.
* @param index The index of the ruleset.
*/
fun removeRoutingRuleset(index: Int) {
if (index < 0) return
val rulesetList = MmkvManager.decodeRoutingRulesets()?.toMutableList() ?: mutableListOf()
val targetId = if (index.isNullOrEmpty()) ruleset.id else index
val index = rulesetList.indexOfFirst { it.id == targetId }
val rulesetList = MmkvManager.decodeRoutingRulesets()
if (rulesetList.isNullOrEmpty()) return
if (index >= 0) {
rulesetList[index] = ruleset
} else {
rulesetList.add(0, ruleset)
}
MmkvManager.encodeRoutingRulesets(rulesetList)
}
fun removeRoutingRuleset(index: String?) {
if (index.isNullOrEmpty()) return
val rulesetList = MmkvManager.decodeRoutingRulesets() ?: return
val targetIndex = rulesetList.indexOfFirst { it.id == index }
if (targetIndex < 0) return
rulesetList.removeAt(targetIndex)
rulesetList.removeAt(index)
MmkvManager.encodeRoutingRulesets(rulesetList)
}
@@ -57,7 +57,7 @@ import java.util.UUID
private val ROUTING_NETWORK_OPTIONS = listOf("tcp", "udp", "tcp,udp")
class RoutingEditActivity : BaseComponentActivity() {
private val rulesetId by lazy { intent.getStringExtra("ruleset_id") }
private val position by lazy { intent.getIntExtra("position", -1) }
private var initial: RulesetItem? = null
private lateinit var outboundSuggestions: List<String>
@@ -65,11 +65,7 @@ class RoutingEditActivity : BaseComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initial = rulesetId?.let { SettingsManager.getRoutingRuleset(it) }
if (initial == null) {
finish()
return
}
initial = SettingsManager.getRoutingRuleset(position)
val profileRemarks = SettingsManager.getProfileRemarks()
outboundSuggestions = (BUILTIN_OUTBOUND_TAGS.toList() + profileRemarks).distinct()
canUseProcess = SettingsManager.canUseProcessRouting()
@@ -78,7 +74,7 @@ class RoutingEditActivity : BaseComponentActivity() {
@Composable
override fun ScreenContent() {
RoutingEditScreen(
rulesetId = rulesetId,
position = position,
initial = initial,
outboundSuggestions = outboundSuggestions,
canUseProcess = canUseProcess,
@@ -93,19 +89,19 @@ class RoutingEditActivity : BaseComponentActivity() {
toast(R.string.sub_setting_remarks)
return false
}
if (rulesetItem.id.isEmpty()) {
if (position < 0 && rulesetItem.id.isEmpty()) {
rulesetItem.id = UUID.randomUUID().toString()
}
SettingsManager.saveRoutingRuleset(rulesetId, rulesetItem)
SettingsManager.saveRoutingRuleset(position, rulesetItem)
toastSuccess(R.string.toast_success)
finish()
return true
}
private fun deleteServer(): Boolean {
if (!rulesetId.isNullOrEmpty()) {
if (position >= 0) {
lifecycleScope.launch(Dispatchers.IO) {
SettingsManager.removeRoutingRuleset(rulesetId)
SettingsManager.removeRoutingRuleset(position)
withContext(Dispatchers.Main) { finish() }
}
}
@@ -115,7 +111,7 @@ class RoutingEditActivity : BaseComponentActivity() {
@Composable
fun RoutingEditScreen(
rulesetId: String?,
position: Int,
initial: RulesetItem?,
outboundSuggestions: List<String>,
canUseProcess: Boolean,
@@ -151,7 +147,7 @@ fun RoutingEditScreen(
}
fun buildRuleset(): RulesetItem {
val rulesetItem = SettingsManager.getRoutingRuleset(rulesetId) ?: RulesetItem()
val rulesetItem = SettingsManager.getRoutingRuleset(position) ?: RulesetItem()
rulesetItem.apply {
this.remarks = remarks
this.locked = locked
@@ -189,7 +185,7 @@ fun RoutingEditScreen(
title = stringResource(R.string.routing_settings_rule_title),
onBackClick = onBackClick,
actions = {
if (initial != null) {
if (position >= 0) {
IconButton(onClick = { showDeleteConfirm = true }) {
Icon(
painterResource(R.drawable.ic_delete_24dp),
@@ -101,8 +101,8 @@ class RoutingSettingActivity : HelperBaseComponentActivity() {
domainStrategyState = domainStrategyState,
onBackClick = { finish() },
onAddRule = { startActivity(Intent(this, RoutingEditActivity::class.java)) },
onEditRule = { rulesetId ->
startActivity(Intent(this, RoutingEditActivity::class.java).putExtra("ruleset_id", rulesetId))
onEditRule = { position ->
startActivity(Intent(this, RoutingEditActivity::class.java).putExtra("position", position))
},
onDomainStrategySelected = { value ->
MmkvManager.encodeSettings(AppConfig.PREF_ROUTING_DOMAIN_STRATEGY, value)
@@ -194,7 +194,7 @@ fun RoutingSettingScreen(
domainStrategyState: MutableStateFlow<String>,
onBackClick: () -> Unit,
onAddRule: () -> Unit,
onEditRule: (String) -> Unit,
onEditRule: (Int) -> Unit,
onDomainStrategySelected: (String) -> Unit,
onImportPredefined: (RoutingType) -> Unit,
onImportClipboard: () -> Unit,
@@ -291,7 +291,7 @@ fun RoutingSettingScreen(
) {
RoutingRulesetItem(
ruleset = ruleset,
onEdit = { onEditRule(ruleset.id) },
onEdit = { onEditRule(index) },
onEnabledChange = { checked ->
val updated = ruleset.copy(enabled = checked)
viewModel.update(index, updated)