Refactor duplicate server removal logic

This commit is contained in:
2dust
2026-01-29 14:34:15 +08:00
parent be95e6a88e
commit d46bbdc219
@@ -15,7 +15,6 @@ import com.v2ray.ang.AngApplication
import com.v2ray.ang.AppConfig
import com.v2ray.ang.R
import com.v2ray.ang.dto.GroupMapItem
import com.v2ray.ang.dto.ProfileItem
import com.v2ray.ang.dto.ServersCache
import com.v2ray.ang.dto.SubscriptionCache
import com.v2ray.ang.extension.serializable
@@ -310,20 +309,15 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
* @return The number of removed servers.
*/
fun removeDuplicateServer(): Int {
val serversCacheCopy = mutableListOf<Pair<String, ProfileItem>>()
for (it in serversCache) {
val config = MmkvManager.decodeServerConfig(it.guid) ?: continue
serversCacheCopy.add(Pair(it.guid, config))
}
val serversCacheCopy = serversCache.toList().toMutableList()
val deleteServer = mutableListOf<String>()
serversCacheCopy.forEachIndexed { index, it ->
val outbound = it.second
serversCacheCopy.forEachIndexed { index2, it2 ->
serversCacheCopy.forEachIndexed { index, sc ->
val profile = sc.profile
serversCacheCopy.forEachIndexed { index2, sc2 ->
if (index2 > index) {
val outbound2 = it2.second
if (outbound.equals(outbound2) && !deleteServer.contains(it2.first)) {
deleteServer.add(it2.first)
val profile2 = sc2.profile
if (profile == profile2 && !deleteServer.contains(sc2.guid)) {
deleteServer.add(sc2.guid)
}
}
}