Performance optimization

This commit is contained in:
2dust
2024-08-13 21:17:27 +08:00
parent 47166b937f
commit 9983ea25d2
3 changed files with 94 additions and 21 deletions

View File

@@ -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)

View File

@@ -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)
}
}
}
}

View File

@@ -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<AngApplication>(),
serverListCopy
)
launch(Dispatchers.Main) {
if (ret == 0)
getApplication<AngApplication>().toast(R.string.toast_success)
else
getApplication<AngApplication>().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<Pair<String, ServerConfig>>()
for (it in serversCache) {
val config = MmkvManager.decodeServerConfig(it.guid) ?: continue
serversCacheCopy.add(Pair(it.guid, config))
}
val deleteServer = mutableListOf<String>()
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<AngApplication>())
viewModelScope.launch(Dispatchers.Default) {