Skip to content

Commit

Permalink
fix: copy bug
Browse files Browse the repository at this point in the history
closes #403
  • Loading branch information
zaneschepke committed Oct 20, 2024
1 parent d9f0de2 commit 105c753
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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),
),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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})\$",
Expand All @@ -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")
Expand Down

0 comments on commit 105c753

Please sign in to comment.