fix: copy bug

closes #403
This commit is contained in:
Zane Schepke 2024-10-20 16:06:07 -04:00
parent d9f0de2dd4
commit 105c753c66
2 changed files with 32 additions and 10 deletions

View File

@ -19,6 +19,8 @@ import com.zaneschepke.wireguardautotunnel.util.FileReadException
import com.zaneschepke.wireguardautotunnel.util.InvalidFileExtensionException
import com.zaneschepke.wireguardautotunnel.util.NumberUtils
import com.zaneschepke.wireguardautotunnel.util.StringValue
import com.zaneschepke.wireguardautotunnel.util.extensions.extractNameAndNumber
import com.zaneschepke.wireguardautotunnel.util.extensions.hasNumberInParentheses
import com.zaneschepke.wireguardautotunnel.util.extensions.toWgQuickString
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.CoroutineDispatcher
@ -106,7 +108,12 @@ constructor(
var tunnelName = name
var num = 1
while (tunnels.any { it.name == tunnelName }) {
tunnelName = "$name($num)"
tunnelName = if (!tunnelName.hasNumberInParentheses()) {
"$name($num)"
} else {
val pair = tunnelName.extractNameAndNumber()
"${pair?.first}($num)"
}
num++
}
tunnelName
@ -233,14 +240,15 @@ constructor(
private fun saveSettings(settings: Settings) = viewModelScope.launch { appDataRepository.settings.save(settings) }
fun onCopyTunnel(tunnel: TunnelConfig?) = viewModelScope.launch {
tunnel?.let {
saveTunnel(
TunnelConfig(
name = it.name.plus(NumberUtils.randomThree()),
wgQuick = it.wgQuick,
),
)
}
fun onCopyTunnel(tunnel: TunnelConfig) = viewModelScope.launch {
saveTunnel(
tunnel.copy(
id = 0,
isPrimaryTunnel = false,
isMobileDataTunnel = false,
isActive = false,
name = makeTunnelNameUnique(tunnel.name),
),
)
}
}

View File

@ -3,6 +3,8 @@ package com.zaneschepke.wireguardautotunnel.util.extensions
import timber.log.Timber
import java.util.regex.Pattern
val hasNumberInParentheses = """^(.+?)\((\d+)\)$""".toRegex()
fun String.isValidIpv4orIpv6Address(): Boolean {
val ipv4Pattern = Pattern.compile(
"^(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\\.(\\d{1,3})\$",
@ -13,6 +15,18 @@ fun String.isValidIpv4orIpv6Address(): Boolean {
return ipv4Pattern.matcher(this).matches() || ipv6Pattern.matcher(this).matches()
}
fun String.hasNumberInParentheses(): Boolean {
return hasNumberInParentheses.matches(this)
}
// Function to extract name and number
fun String.extractNameAndNumber(): Pair<String, Int>? {
val matchResult = hasNumberInParentheses.matchEntire(this)
return matchResult?.let {
Pair(it.groupValues[1], it.groupValues[2].toInt())
}
}
fun List<String>.isMatchingToWildcardList(value: String): Boolean {
val excludeValues = this.filter { it.startsWith("!") }.map { it.removePrefix("!").toRegexWithWildcards() }
Timber.d("Excluded values: $excludeValues")