Skip to content
This repository has been archived by the owner on Nov 14, 2024. It is now read-only.

Commit

Permalink
Optimize MmkvManager
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust authored and AnGgIt86 committed Sep 23, 2024
1 parent ce91437 commit 0daea33
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 37 deletions.
19 changes: 18 additions & 1 deletion app/src/main/kotlin/com/neko/v2ray/util/AngConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ import com.neko.v2ray.R
import com.neko.v2ray.dto.*
import com.neko.v2ray.util.MmkvManager.serverRawStorage
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.MmkvManager.subStorage
import com.neko.v2ray.util.fmt.ShadowsocksFmt
import com.neko.v2ray.util.fmt.SocksFmt
import com.neko.v2ray.util.fmt.TrojanFmt
import com.neko.v2ray.util.fmt.VlessFmt
import com.neko.v2ray.util.fmt.VmessFmt
import com.neko.v2ray.util.fmt.WireguardFmt
import java.lang.reflect.Type
import java.net.URI
import java.util.*

object AngConfigManager {
Expand Down Expand Up @@ -396,7 +398,7 @@ object AngConfigManager {
servers.lines()
.forEach { str ->
if (str.startsWith(AppConfig.PROTOCOL_HTTP) || str.startsWith(AppConfig.PROTOCOL_HTTPS)) {
count += MmkvManager.importUrlAsSubscription(str)
count += importUrlAsSubscription(str)
}
}
return count
Expand Down Expand Up @@ -576,4 +578,19 @@ object AngConfigManager {
}
return count
}

private fun importUrlAsSubscription(url: String): Int {
val subscriptions = MmkvManager.decodeSubscriptions()
subscriptions.forEach {
if (it.second.url == url) {
return 0
}
}
val uri = URI(Utils.fixIllegalUrl(url))
val subItem = SubscriptionItem()
subItem.remarks = uri.fragment ?: "import sub"
subItem.url = url
subStorage.encode(Utils.getUuid(), Gson().toJson(subItem))
return 1
}
}
35 changes: 0 additions & 35 deletions app/src/main/kotlin/com/neko/v2ray/util/MmkvManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import com.neko.v2ray.dto.ProfileItem
import com.neko.v2ray.dto.ServerAffiliationInfo
import com.neko.v2ray.dto.ServerConfig
import com.neko.v2ray.dto.SubscriptionItem
import java.net.URI

object MmkvManager {
private const val ID_MAIN = "MAIN"
Expand Down Expand Up @@ -152,21 +151,6 @@ object MmkvManager {
}
}

fun importUrlAsSubscription(url: String): Int {
val subscriptions = decodeSubscriptions()
subscriptions.forEach {
if (it.second.url == url) {
return 0
}
}
val uri = URI(Utils.fixIllegalUrl(url))
val subItem = SubscriptionItem()
subItem.remarks = uri.fragment ?: "import sub"
subItem.url = url
subStorage.encode(Utils.getUuid(), Gson().toJson(subItem))
return 1
}

fun decodeSubscriptions(): List<Pair<String, SubscriptionItem>> {
val subscriptions = mutableListOf<Pair<String, SubscriptionItem>>()
subStorage.allKeys()?.forEach { key ->
Expand Down Expand Up @@ -228,25 +212,6 @@ object MmkvManager {
}
}

fun sortByTestResults() {
data class ServerDelay(var guid: String, var testDelayMillis: Long)

val serverDelays = mutableListOf<ServerDelay>()
val serverList = decodeServerList()
serverList.forEach { key ->
val delay = decodeServerAffiliationInfo(key)?.testDelayMillis ?: 0L
serverDelays.add(ServerDelay(key, if (delay <= 0L) 999999 else delay))
}
serverDelays.sortBy { it.testDelayMillis }

serverDelays.forEach {
serverList.remove(it.guid)
serverList.add(it.guid)
}

encodeServerList(serverList)
}

fun getServerViaRemarks(remarks: String?): ServerConfig? {
if (remarks == null) {
return null
Expand Down
17 changes: 16 additions & 1 deletion app/src/main/kotlin/com/neko/v2ray/viewmodel/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,22 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
}

fun sortByTestResults() {
MmkvManager.sortByTestResults()
data class ServerDelay(var guid: String, var testDelayMillis: Long)

val serverDelays = mutableListOf<ServerDelay>()
val serverList = MmkvManager.decodeServerList()
serverList.forEach { key ->
val delay = MmkvManager.decodeServerAffiliationInfo(key)?.testDelayMillis ?: 0L
serverDelays.add(ServerDelay(key, if (delay <= 0L) 999999 else delay))
}
serverDelays.sortBy { it.testDelayMillis }

serverDelays.forEach {
serverList.remove(it.guid)
serverList.add(it.guid)
}

MmkvManager.encodeServerList(serverList)
}


Expand Down

0 comments on commit 0daea33

Please sign in to comment.