From 8b149fb52fff28f1d51e88227c2c9ee28c93b483 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Sat, 28 Mar 2020 12:34:05 -0400 Subject: [PATCH] Fix selected index when update subscription --- .../com/v2ray/ang/util/AngConfigManager.kt | 40 +++++++++++++------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt index 217e3573..06d4f438 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/AngConfigManager.kt @@ -104,16 +104,7 @@ object AngConfigManager { angConfig.vmess.removeAt(index) //移除的是活动的 - if (angConfig.index == index) { - if (angConfig.vmess.count() > 0) { - angConfig.index = 0 - } else { - angConfig.index = -1 - } - } else if (index < angConfig.index)//移除活动之前的 - { - angConfig.index-- - } + adjustIndexForRemovalAt(index) storeConfigFile() } catch (e: Exception) { @@ -123,6 +114,19 @@ object AngConfigManager { return 0 } + private fun adjustIndexForRemovalAt(index: Int) { + if (angConfig.index == index) { + if (angConfig.vmess.count() > 0) { + angConfig.index = 0 + } else { + angConfig.index = -1 + } + } else if (index < angConfig.index)//移除活动之前的 + { + angConfig.index-- + } + } + fun swapServer(fromPosition: Int, toPosition: Int): Int { try { Collections.swap(angConfig.vmess, fromPosition, toPosition) @@ -241,7 +245,7 @@ object AngConfigManager { /** * import config form qrcode or... */ - fun importConfig(server: String?, subid: String): Int { + fun importConfig(server: String?, subid: String, removedSelectedServer: AngConfig.VmessBean?): Int { try { if (server == null || TextUtils.isEmpty(server)) { return R.string.toast_none_data @@ -361,6 +365,12 @@ object AngConfigManager { } else { return R.string.toast_incorrect_protocol } + if (removedSelectedServer != null && + vmess.subid.equals(removedSelectedServer.subid) && + vmess.address.equals(removedSelectedServer.address) && + vmess.port.equals(removedSelectedServer.port)) { + setActiveServer(configs.vmess.count() - 1) + } } catch (e: Exception) { e.printStackTrace() return -1 @@ -728,6 +738,11 @@ object AngConfigManager { if (servers == null) { return 0 } + val removedSelectedServer = + if (!TextUtils.isEmpty(subid) && configs.vmess[configs.index].subid.equals(subid)) + configs.vmess[configs.index] + else + null removeServerViaSubid(subid) // var servers = server @@ -738,7 +753,7 @@ object AngConfigManager { var count = 0 servers.lines() .forEach { - val resId = importConfig(it, subid) + val resId = importConfig(it, subid, removedSelectedServer) if (resId == 0) { count++ } @@ -778,6 +793,7 @@ object AngConfigManager { for (k in configs.vmess.count() - 1 downTo 0) { if (configs.vmess[k].subid.equals(subid)) { angConfig.vmess.removeAt(k) + adjustIndexForRemovalAt(k) } }