From 3cb4df474be61216ab63046ab01e6a00ad265735 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Wed, 27 May 2026 19:53:18 +0800 Subject: [PATCH] Keep selected server when replacing subscription https://github.com/2dust/v2rayNG/pull/5676 --- .../com/v2ray/ang/handler/AngConfigManager.kt | 41 +++++++++++-------- .../com/v2ray/ang/ui/MainRecyclerAdapter.kt | 8 +++- 2 files changed, 31 insertions(+), 18 deletions(-) diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/handler/AngConfigManager.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/handler/AngConfigManager.kt index c964a665..b17f10ee 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/handler/AngConfigManager.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/handler/AngConfigManager.kt @@ -234,15 +234,8 @@ object AngConfigManager { if (servers == null) { return 0 } - // Find the currently selected server that matches the subscription ID - val removedSelected = if (subid.isNotBlank() && !append) { - MmkvManager.getSelectServer() - .takeIf { it?.isNotBlank() == true } - ?.let { MmkvManager.decodeServerConfig(it) } - ?.takeIf { it.subscriptionId == subid } - } else { - null - } + // Find the currently selected server that belongs to the same subscription before replacement. + val removedSelected = getRemovedSelectedProfile(subid, append) val subItem = MmkvManager.decodeSubscription(subid) @@ -288,7 +281,6 @@ object AngConfigManager { // Read serverList once val serverList = MmkvManager.decodeServerList(subid) - var needSetSelected = MmkvManager.getSelectServer().isNullOrBlank() configs.forEach { config -> val key = Utils.getUuid() @@ -297,10 +289,6 @@ object AngConfigManager { if (!serverList.contains(key)) { serverList.add(0, key) - if (needSetSelected) { - MmkvManager.setSelectServer(key) - needSetSelected = false - } } keyToProfile[key] = config } @@ -323,7 +311,8 @@ object AngConfigManager { * @return Matched key or null */ private fun findMatchedProfileKey(keyToProfile: Map, target: ProfileItem?): String? { - if (keyToProfile.isEmpty() || target == null) return null + if (keyToProfile.isEmpty()) return null + if (target == null) return null // Level 0: Full match (remarks + server + port + password) if (target.remarks.isNotBlank()) { @@ -360,7 +349,20 @@ object AngConfigManager { isSameText(saved.server, target.server) }?.key?.let { return it } - return null + // If old selected node cannot be matched, fall back to the first imported config. + return keyToProfile.keys.firstOrNull() + } + + /** + * Returns the currently selected profile if it belongs to the target subscription and will be replaced. + */ + private fun getRemovedSelectedProfile(subid: String, append: Boolean): ProfileItem? { + if (subid.isBlank() || append) return null + + return MmkvManager.getSelectServer() + .takeIf { it?.isNotBlank() == true } + ?.let { MmkvManager.decodeServerConfig(it) } + ?.takeIf { it.subscriptionId == subid } } /** @@ -396,18 +398,25 @@ object AngConfigManager { JsonUtil.fromJson(server, Array::class.java) ?: arrayOf() if (serverList.isNotEmpty()) { + val removedSelected = getRemovedSelectedProfile(subid, append) if (!append) { MmkvManager.removeServerViaSubid(subid) } var count = 0 + val keyToProfile = mutableMapOf() for (srv in serverList.reversed()) { val config = CustomFmt.parse(JsonUtil.toJson(srv)) ?: continue config.subscriptionId = subid config.description = generateDescription(config) val key = MmkvManager.encodeServerConfig("", config) MmkvManager.encodeServerRaw(key, JsonUtil.toJsonPretty(srv) ?: "") + keyToProfile[key] = config count += 1 } + if (count > 0) { + val matchKey = findMatchedProfileKey(keyToProfile, removedSelected) + matchKey?.let { MmkvManager.setSelectServer(it) } + } return count } } catch (e: Exception) { diff --git a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt index 7e927858..51dab4b2 100644 --- a/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt +++ b/V2rayNG/app/src/main/java/com/v2ray/ang/ui/MainRecyclerAdapter.kt @@ -15,6 +15,7 @@ import com.v2ray.ang.databinding.ItemRecyclerFooterBinding import com.v2ray.ang.databinding.ItemRecyclerMainBinding import com.v2ray.ang.dto.entities.ProfileItem import com.v2ray.ang.dto.entities.ServersCache +import com.v2ray.ang.extension.isComplexType import com.v2ray.ang.extension.nullIfBlank import com.v2ray.ang.handler.AngConfigManager import com.v2ray.ang.handler.MmkvManager @@ -142,8 +143,11 @@ class MainRecyclerAdapter( } private fun getProtocolDescription(profile: ProfileItem): String { - val parts = mutableListOf() + if (profile.configType.isComplexType()) { + return profile.configType.name + } + val parts = mutableListOf() parts.add(profile.configType.name) // Transport: hide tcp or blank @@ -160,7 +164,7 @@ class MainRecyclerAdapter( } } - return parts.joinToString("/") + return parts.joinToString(" / ") } fun removeServerSub(guid: String, position: Int) {