Merge pull request #1038 from yuhan6665/new-storage-fix

New storage fix
This commit is contained in:
2dust
2021-05-01 18:20:11 +08:00
committed by GitHub
3 changed files with 20 additions and 5 deletions

View File

@@ -47,7 +47,7 @@ class MainRecyclerAdapter(val activity: MainActivity) : RecyclerView.Adapter<Mai
override fun onBindViewHolder(holder: BaseViewHolder, position: Int) {
if (holder is MainViewHolder) {
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 aff = MmkvManager.decodeServerAffiliationInfo(guid)

View File

@@ -246,8 +246,8 @@ class ServerActivity : BaseActivity() {
}
private fun saveStreamSettings(streamSetting: V2rayConfig.OutboundBean.StreamSettingsBean, config: ServerConfig) {
val requestHost = et_request_host?.text.toString().trim()
val path = et_path?.text.toString().trim()
val requestHost = if (et_request_host != null) et_request_host.text.toString().trim() else ""
val path = if (et_path != null) et_path.text.toString().trim() else ""
var sni = streamSetting.populateTransportSettings(
if (sp_network != null) networks[sp_network.selectedItemPosition] else DEFAULT_NETWORK,
if (sp_header_type != null) headertypes[sp_header_type.selectedItemPosition] else "",

View File

@@ -21,13 +21,15 @@ import com.v2ray.ang.util.*
import com.v2ray.ang.util.MmkvManager.KEY_ANG_CONFIGS
import kotlinx.coroutines.*
import java.util.*
import java.util.concurrent.ConcurrentHashMap
class MainViewModel(application: Application) : AndroidViewModel(application) {
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) }
var serverList= MmkvManager.decodeServerList()
var serverList = MmkvManager.decodeServerList()
private set
val serversCache = ConcurrentHashMap<String, ServerConfig>()
val isRunning by lazy { MutableLiveData<Boolean>() }
val updateListAction by lazy { MutableLiveData<Int>() }
val updateTestResultAction by lazy { MutableLiveData<String>() }
@@ -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<AngApplication>().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) {