From 9983ea25d20016b5eb5635e157aa92ffe8e6cd53 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 13 Aug 2024 21:17:27 +0800 Subject: [PATCH] Performance optimization --- .../kotlin/com/v2ray/ang/ui/MainActivity.kt | 15 +--- .../kotlin/com/v2ray/ang/util/MmkvManager.kt | 16 +++- .../com/v2ray/ang/viewmodel/MainViewModel.kt | 84 +++++++++++++++++-- 3 files changed, 94 insertions(+), 21 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt index 023d2b72..25c7784b 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainActivity.kt @@ -291,11 +291,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList } R.id.export_all -> { - if (AngConfigManager.shareNonCustomConfigsToClipboard(this, mainViewModel.serverList) == 0) { - toast(R.string.toast_success) - } else { - toast(R.string.toast_failure) - } + mainViewModel.exportAllServer() true } @@ -317,8 +313,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList R.id.del_all_config -> { AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm) .setPositiveButton(android.R.string.ok) { _, _ -> - MmkvManager.removeAllServer() - mainViewModel.reloadServerList() + mainViewModel.removeAllServer() } .setNegativeButton(android.R.string.no) {_, _ -> //do noting @@ -340,8 +335,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList R.id.del_invalid_config -> { AlertDialog.Builder(this).setMessage(R.string.del_config_comfirm) .setPositiveButton(android.R.string.ok) { _, _ -> - MmkvManager.removeInvalidServer() - mainViewModel.reloadServerList() + mainViewModel.removeInvalidServer() } .setNegativeButton(android.R.string.no) {_, _ -> //do noting @@ -350,8 +344,7 @@ class MainActivity : BaseActivity(), NavigationView.OnNavigationItemSelectedList true } R.id.sort_by_test_results -> { - MmkvManager.sortByTestResults() - mainViewModel.reloadServerList() + mainViewModel.sortByTestResults() true } else -> super.onOptionsItemSelected(item) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MmkvManager.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MmkvManager.kt index b81c56fa..97ab05f6 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MmkvManager.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/util/MmkvManager.kt @@ -191,11 +191,19 @@ object MmkvManager { serverAffStorage?.clearAll() } - fun removeInvalidServer() { - serverAffStorage?.allKeys()?.forEach { key -> - decodeServerAffiliationInfo(key)?.let { aff -> + fun removeInvalidServer(guid: String) { + if (guid.isNotEmpty()) { + decodeServerAffiliationInfo(guid)?.let { aff -> if (aff.testDelayMillis < 0L) { - removeServer(key) + removeServer(guid) + } + } + } else { + serverAffStorage?.allKeys()?.forEach { key -> + decodeServerAffiliationInfo(key)?.let { aff -> + if (aff.testDelayMillis < 0L) { + removeServer(key) + } } } } diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt index d0d4e589..04bd836b 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/viewmodel/MainViewModel.kt @@ -23,6 +23,7 @@ import com.v2ray.ang.dto.ServerConfig import com.v2ray.ang.dto.ServersCache import com.v2ray.ang.dto.V2rayConfig import com.v2ray.ang.extension.toast +import com.v2ray.ang.util.AngConfigManager import com.v2ray.ang.util.MessageUtil import com.v2ray.ang.util.MmkvManager import com.v2ray.ang.util.MmkvManager.KEY_ANG_CONFIGS @@ -170,6 +171,29 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { } } + + fun exportAllServer() { + viewModelScope.launch(Dispatchers.Default) { + val serverListCopy = + if (subscriptionId.isNullOrEmpty()) { + serverList + } else { + serversCache.map { it.guid }.toList() + } + + val ret = AngConfigManager.shareNonCustomConfigsToClipboard( + getApplication(), + serverListCopy + ) + launch(Dispatchers.Main) { + if (ret == 0) + getApplication().toast(R.string.toast_success) + else + getApplication().toast(R.string.toast_failure) + } + } + } + fun testAllTcping() { tcpingTestScope.coroutineContext[Job]?.cancelChildren() SpeedtestUtil.closeAllTcpSockets() @@ -256,14 +280,20 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { fun removeDuplicateServer() { viewModelScope.launch(Dispatchers.Default) { + val serversCacheCopy = mutableListOf>() + for (it in serversCache) { + val config = MmkvManager.decodeServerConfig(it.guid) ?: continue + serversCacheCopy.add(Pair(it.guid, config)) + } + val deleteServer = mutableListOf() - serversCache.forEachIndexed { index, it -> - val outbound = MmkvManager.decodeServerConfig(it.guid)?.getProxyOutbound() - serversCache.forEachIndexed { index2, it2 -> + serversCacheCopy.forEachIndexed { index, it -> + val outbound = it.second.getProxyOutbound() + serversCacheCopy.forEachIndexed { index2, it2 -> if (index2 > index) { - val outbound2 = MmkvManager.decodeServerConfig(it2.guid)?.getProxyOutbound() - if (outbound == outbound2 && !deleteServer.contains(it2.guid)) { - deleteServer.add(it2.guid) + val outbound2 = it2.second.getProxyOutbound() + if (outbound == outbound2 && !deleteServer.contains(it2.first)) { + deleteServer.add(it2.first) } } } @@ -284,6 +314,48 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { } } + fun removeAllServer() { + viewModelScope.launch(Dispatchers.Default) { + if (subscriptionId.isNullOrEmpty()) { + MmkvManager.removeAllServer() + } else { + val serversCopy = serversCache.toList() + for (item in serversCopy) { + MmkvManager.removeServer(item.guid) + } + } + launch(Dispatchers.Main) { + reloadServerList() + } + } + } + + fun removeInvalidServer() { + viewModelScope.launch(Dispatchers.Default) { + if (subscriptionId.isNullOrEmpty()) { + MmkvManager.removeInvalidServer("") + } else { + val serversCopy = serversCache.toList() + for (item in serversCopy) { + MmkvManager.removeInvalidServer(item.guid) + } + } + launch(Dispatchers.Main) { + reloadServerList() + } + } + } + + fun sortByTestResults() { + viewModelScope.launch(Dispatchers.Default) { + MmkvManager.sortByTestResults() + launch(Dispatchers.Main) { + reloadServerList() + } + } + } + + fun copyAssets(assets: AssetManager) { val extFolder = Utils.userAssetPath(getApplication()) viewModelScope.launch(Dispatchers.Default) {