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

Commit

Permalink
Optimize Storage
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust authored and AnGgIt86 committed Sep 26, 2024
1 parent 78c5f76 commit f70cdcb
Show file tree
Hide file tree
Showing 10 changed files with 107 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ import com.neko.v2ray.dto.ServerConfig
import com.neko.v2ray.dto.V2rayConfig
import com.neko.v2ray.extension.toast
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.serverRawStorage
import com.neko.v2ray.util.Utils
import com.neko.v2ray.util.SoftInputAssist
import com.neko.v2ray.util.Utils
import me.drakeet.support.toast.ToastCompat

import com.neko.imageslider.ImageSlider
Expand Down Expand Up @@ -109,7 +108,7 @@ class ServerCustomConfigActivity : BaseActivity() {
*/
private fun bindingServer(config: ServerConfig): Boolean {
binding.etRemarks.text = Utils.getEditable(config.remarks)
val raw = serverRawStorage?.decodeString(editGuid)
val raw = MmkvManager.decodeServerRaw(editGuid)
if (raw.isNullOrBlank()) {
binding.editor.setTextContent(Utils.getEditable(config.fullConfig?.toPrettyPrinting().orEmpty()))
} else {
Expand Down Expand Up @@ -148,7 +147,7 @@ class ServerCustomConfigActivity : BaseActivity() {
config.fullConfig = v2rayConfig

MmkvManager.encodeServerConfig(editGuid, config)
serverRawStorage?.encode(editGuid, binding.editor.text.toString())
MmkvManager.encodeServerRaw(editGuid, binding.editor.text.toString())
toast(R.string.toast_success)
finish()
return true
Expand Down
18 changes: 5 additions & 13 deletions app/src/main/kotlin/com/neko/v2ray/ui/SubEditActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import android.view.Menu
import android.view.MenuItem
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.appbar.MaterialToolbar
import androidx.lifecycle.lifecycleScope
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.google.gson.Gson
import androidx.lifecycle.lifecycleScope
import com.neko.v2ray.R
import com.neko.v2ray.databinding.ActivitySubEditBinding
import com.neko.v2ray.dto.SubscriptionItem
import com.neko.v2ray.extension.toast
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.subStorage
import com.neko.v2ray.util.SoftInputAssist
import com.neko.v2ray.util.Utils
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -48,9 +46,9 @@ class SubEditActivity : BaseActivity() {
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

val json = subStorage?.decodeString(editSubId)
if (!json.isNullOrBlank()) {
bindingServer(Gson().fromJson(json, SubscriptionItem::class.java))
val subItem = MmkvManager.decodeSubscription(editSubId)
if (subItem != null) {
bindingServer(subItem)
} else {
clearServer()
}
Expand Down Expand Up @@ -121,13 +119,7 @@ class SubEditActivity : BaseActivity() {
* save server config
*/
private fun saveServer(): Boolean {
val subItem: SubscriptionItem
val json = subStorage?.decodeString(editSubId)
if (!json.isNullOrBlank()) {
subItem = Gson().fromJson(json, SubscriptionItem::class.java)
} else {
subItem = SubscriptionItem()
}
val subItem = MmkvManager.decodeSubscription(editSubId)?:SubscriptionItem()

subItem.remarks = binding.etRemarks.text.toString()
subItem.url = binding.etUrl.text.toString()
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/com/neko/v2ray/ui/TaskerActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class TaskerActivity : BaseActivity() {
lstData.add("Default")
lstGuid.add(AppConfig.TASKER_DEFAULT_GUID)

MmkvManager.serverStorage?.allKeys()?.forEach { key ->
MmkvManager.decodeServerList()?.forEach { key ->
MmkvManager.decodeServerConfig(key)?.let { config ->
lstData.add(config.remarks)
lstGuid.add(key)
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/kotlin/com/neko/v2ray/ui/UserAssetActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import com.google.android.material.appbar.CollapsingToolbarLayout
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.gson.Gson
import com.tbruyelle.rxpermissions3.RxPermissions
import com.neko.v2ray.AppConfig
import com.neko.v2ray.R
Expand All @@ -32,7 +31,6 @@ import com.neko.v2ray.dto.AssetUrlItem
import com.neko.v2ray.extension.toTrafficString
import com.neko.v2ray.extension.toast
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.MmkvManager.assetStorage
import com.neko.v2ray.util.MmkvManager.settingsStorage
import com.neko.v2ray.util.Utils
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -143,7 +141,7 @@ class UserAssetActivity : BaseActivity() {
toast(R.string.msg_remark_is_duplicate)
return@registerForActivityResult
}
assetStorage?.encode(assetId, Gson().toJson(assetItem))
MmkvManager.encodeAsset(assetId, assetItem)
copyFile(uri)
} catch (e: Exception) {
toast(R.string.toast_asset_copy_failed)
Expand Down
18 changes: 7 additions & 11 deletions app/src/main/kotlin/com/neko/v2ray/ui/UserAssetUrlActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import android.view.MenuItem
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.appbar.CollapsingToolbarLayout
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.gson.Gson
// import androidx.appcompat.app.AlertDialog
import com.neko.v2ray.R
import com.neko.v2ray.databinding.ActivityUserAssetUrlBinding
import com.neko.v2ray.dto.AssetUrlItem
import com.neko.v2ray.extension.toast
import com.neko.v2ray.util.MmkvManager
import com.neko.v2ray.util.SoftInputAssist
import com.neko.v2ray.util.MmkvManager.assetStorage
import com.neko.v2ray.util.Utils
import java.io.File

Expand Down Expand Up @@ -47,9 +46,9 @@ class UserAssetUrlActivity : BaseActivity() {
setSupportActionBar(toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

val json = assetStorage?.decodeString(editAssetId)
if (!json.isNullOrBlank()) {
bindingAsset(Gson().fromJson(json, AssetUrlItem::class.java))
val assetItem = MmkvManager.decodeAsset(editAssetId)
if (assetItem != null) {
bindingAsset(assetItem)
} else {
clearAsset()
}
Expand Down Expand Up @@ -113,12 +112,9 @@ class UserAssetUrlActivity : BaseActivity() {
* save asset config
*/
private fun saveServer(): Boolean {
val assetItem: AssetUrlItem
val json = assetStorage?.decodeString(editAssetId)
var assetItem = MmkvManager.decodeAsset(editAssetId)
var assetId = editAssetId
if (!json.isNullOrBlank()) {
assetItem = Gson().fromJson(json, AssetUrlItem::class.java)

if (assetItem != null) {
// remove file associated with the asset
val file = extDir.resolve(assetItem.remarks)
if (file.exists()) {
Expand Down Expand Up @@ -149,7 +145,7 @@ class UserAssetUrlActivity : BaseActivity() {
return false
}

assetStorage?.encode(assetId, Gson().toJson(assetItem))
MmkvManager.encodeAsset(assetId, assetItem)
toast(R.string.toast_success)
finish()
return true
Expand Down
40 changes: 3 additions & 37 deletions app/src/main/kotlin/com/neko/v2ray/util/AngConfigManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import com.google.gson.reflect.TypeToken
import com.neko.v2ray.AppConfig
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.fmt.ShadowsocksFmt
import com.neko.v2ray.util.fmt.SocksFmt
Expand Down Expand Up @@ -177,39 +176,6 @@ object AngConfigManager {
return 0
}

// /**
// * upgrade
// */
// private fun upgradeServerVersion(vmess: AngConfig.VmessBean): Int {
// try {
// if (vmess.configVersion == 2) {
// return 0
// }
//
// when (vmess.network) {
// "ws", "h2" -> {
// var path = ""
// var host = ""
// val lstParameter = vmess.requestHost.split(";")
// if (lstParameter.isNotEmpty()) {
// path = lstParameter[0].trim()
// }
// if (lstParameter.size > 1) {
// path = lstParameter[0].trim()
// host = lstParameter[1].trim()
// }
// vmess.path = path
// vmess.requestHost = host
// }
// }
// vmess.configVersion = 2
// return 0
// } catch (e: Exception) {
// e.printStackTrace()
// return -1
// }
// }

fun importBatchConfig(server: String?, subid: String, append: Boolean): Pair<Int, Int> {
var count = parseBatchConfig(Utils.decode(server), subid, append)
if (count <= 0) {
Expand Down Expand Up @@ -324,7 +290,7 @@ object AngConfigManager {
.toString())
config.subscriptionId = subid
val key = MmkvManager.encodeServerConfig("", config)
serverRawStorage?.encode(key, gson.toJson(srv))
MmkvManager.encodeServerRaw(key, gson.toJson(srv))
count += 1
}
return count
Expand All @@ -339,14 +305,14 @@ object AngConfigManager {
config.fullConfig = Gson().fromJson(server, V2rayConfig::class.java)
config.remarks = config.fullConfig?.remarks ?: System.currentTimeMillis().toString()
val key = MmkvManager.encodeServerConfig("", config)
serverRawStorage?.encode(key, server)
MmkvManager.encodeServerRaw(key, server)
return 1
} else if (server.startsWith("[Interface]") && server.contains("[Peer]")) {
val config = WireguardFmt.parseWireguardConfFile(server)
?: return R.string.toast_incorrect_protocol
config.fullConfig?.remarks ?: System.currentTimeMillis().toString()
val key = MmkvManager.encodeServerConfig("", config)
serverRawStorage?.encode(key, server)
MmkvManager.encodeServerRaw(key, server)
return 1
} else {
return 0
Expand Down
Loading

0 comments on commit f70cdcb

Please sign in to comment.