Fix scrolling by cache server config in ViewModel
This commit is contained in:
@@ -47,7 +47,7 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
|
|||||||
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
|
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
|
||||||
if (holder is MainViewHolder) {
|
if (holder is MainViewHolder) {
|
||||||
val guid = mActivity.mainViewModel.serverList.getOrNull(position) ?: return
|
val guid = mActivity.mainViewModel.serverList.getOrNull(position) ?: return
|
||||||
val config = MmkvManager.decodeServerConfig(guid) ?: return
|
val config = mActivity.mainViewModel.serversCache.getOrElse(guid, { MmkvManager.decodeServerConfig(guid) })?: return
|
||||||
val outbound = config.getProxyOutbound()
|
val outbound = config.getProxyOutbound()
|
||||||
val aff = MmkvManager.decodeServerAffiliationInfo(guid)
|
val aff = MmkvManager.decodeServerAffiliationInfo(guid)
|
||||||
|
|
||||||
|
|||||||
@@ -21,13 +21,15 @@ import com.v2ray.ang.util.*
|
|||||||
import com.v2ray.ang.util.MmkvManager.KEY_ANG_CONFIGS
|
import com.v2ray.ang.util.MmkvManager.KEY_ANG_CONFIGS
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
|
|
||||||
class MainViewModel(application: Application) : AndroidViewModel(application) {
|
class MainViewModel(application: Application) : AndroidViewModel(application) {
|
||||||
private val mainStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_MAIN, MMKV.MULTI_PROCESS_MODE) }
|
private val mainStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_MAIN, MMKV.MULTI_PROCESS_MODE) }
|
||||||
private val serverRawStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_SERVER_RAW, MMKV.MULTI_PROCESS_MODE) }
|
private val serverRawStorage by lazy { MMKV.mmkvWithID(MmkvManager.ID_SERVER_RAW, MMKV.MULTI_PROCESS_MODE) }
|
||||||
|
|
||||||
var serverList= MmkvManager.decodeServerList()
|
var serverList = MmkvManager.decodeServerList()
|
||||||
private set
|
private set
|
||||||
|
val serversCache = ConcurrentHashMap<String, ServerConfig>()
|
||||||
val isRunning by lazy { MutableLiveData<Boolean>() }
|
val isRunning by lazy { MutableLiveData<Boolean>() }
|
||||||
val updateListAction by lazy { MutableLiveData<Int>() }
|
val updateListAction by lazy { MutableLiveData<Int>() }
|
||||||
val updateTestResultAction by lazy { MutableLiveData<String>() }
|
val updateTestResultAction by lazy { MutableLiveData<String>() }
|
||||||
@@ -50,6 +52,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
|||||||
|
|
||||||
fun reloadServerList() {
|
fun reloadServerList() {
|
||||||
serverList = MmkvManager.decodeServerList()
|
serverList = MmkvManager.decodeServerList()
|
||||||
|
updateCache()
|
||||||
updateListAction.value = -1
|
updateListAction.value = -1
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -65,6 +68,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
|||||||
val key = MmkvManager.encodeServerConfig("", config)
|
val key = MmkvManager.encodeServerConfig("", config)
|
||||||
serverRawStorage?.encode(key, server)
|
serverRawStorage?.encode(key, server)
|
||||||
serverList.add(key)
|
serverList.add(key)
|
||||||
|
serversCache[key] = config
|
||||||
}
|
}
|
||||||
|
|
||||||
fun swapServer(fromPosition: Int, toPosition: Int) {
|
fun swapServer(fromPosition: Int, toPosition: Int) {
|
||||||
@@ -72,6 +76,17 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
|||||||
mainStorage?.encode(KEY_ANG_CONFIGS, Gson().toJson(serverList))
|
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() {
|
fun testAllTcping() {
|
||||||
tcpingTestScope.coroutineContext[Job]?.cancelChildren()
|
tcpingTestScope.coroutineContext[Job]?.cancelChildren()
|
||||||
Utils.closeAllTcpSockets()
|
Utils.closeAllTcpSockets()
|
||||||
@@ -80,7 +95,7 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
|
|||||||
|
|
||||||
getApplication<AngApplication>().toast(R.string.connection_test_testing)
|
getApplication<AngApplication>().toast(R.string.connection_test_testing)
|
||||||
for (guid in serverList) {
|
for (guid in serverList) {
|
||||||
MmkvManager.decodeServerConfig(guid)?.getProxyOutbound()?.let { outbound ->
|
serversCache.getOrElse(guid, { MmkvManager.decodeServerConfig(guid) })?.getProxyOutbound()?.let { outbound ->
|
||||||
val serverAddress = outbound.getServerAddress()
|
val serverAddress = outbound.getServerAddress()
|
||||||
val serverPort = outbound.getServerPort()
|
val serverPort = outbound.getServerPort()
|
||||||
if (serverAddress != null && serverPort != null) {
|
if (serverAddress != null && serverPort != null) {
|
||||||
|
|||||||
Reference in New Issue
Block a user