From 233b34bda629f9d8660f03c74d31defbe3b98b03 Mon Sep 17 00:00:00 2001 From: yuhan6665 <1588741+yuhan6665@users.noreply.github.com> Date: Fri, 30 Apr 2021 20:17:41 -0400 Subject: [PATCH] Fix scrolling by cache server config in ViewModel --- .../com/v2ray/ang/ui/MainRecyclerAdapter.kt | 2 +- .../com/v2ray/ang/viewmodel/MainViewModel.kt | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainRecyclerAdapter.kt b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainRecyclerAdapter.kt index 7146a217..9880e251 100644 --- a/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainRecyclerAdapter.kt +++ b/V2rayNG/app/src/main/kotlin/com/v2ray/ang/ui/MainRecyclerAdapter.kt @@ -47,7 +47,7 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter() val isRunning by lazy { MutableLiveData() } val updateListAction by lazy { MutableLiveData() } val updateTestResultAction by lazy { MutableLiveData() } @@ -50,6 +52,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { fun reloadServerList() { serverList = MmkvManager.decodeServerList() + updateCache() updateListAction.value = -1 } @@ -65,6 +68,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { val key = MmkvManager.encodeServerConfig("", config) serverRawStorage?.encode(key, server) serverList.add(key) + serversCache[key] = config } fun swapServer(fromPosition: Int, toPosition: Int) { @@ -72,6 +76,17 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { mainStorage?.encode(KEY_ANG_CONFIGS, Gson().toJson(serverList)) } + fun updateCache() { + serversCache.clear() + GlobalScope.launch(Dispatchers.Default) { + serverList.forEach { guid -> + MmkvManager.decodeServerConfig(guid)?.let { + serversCache[guid] = it + } + } + } + } + fun testAllTcping() { tcpingTestScope.coroutineContext[Job]?.cancelChildren() Utils.closeAllTcpSockets() @@ -80,7 +95,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) { getApplication().toast(R.string.connection_test_testing) for (guid in serverList) { - MmkvManager.decodeServerConfig(guid)?.getProxyOutbound()?.let { outbound -> + serversCache.getOrElse(guid, { MmkvManager.decodeServerConfig(guid) })?.getProxyOutbound()?.let { outbound -> val serverAddress = outbound.getServerAddress() val serverPort = outbound.getServerPort() if (serverAddress != null && serverPort != null) {