From 105c753c66f47cb16978882c68bbcc960a7ac2e0 Mon Sep 17 00:00:00 2001 From: Zane Schepke Date: Sun, 20 Oct 2024 16:06:07 -0400 Subject: [PATCH] fix: copy bug closes #403 --- .../ui/screens/main/MainViewModel.kt | 28 ++++++++++++------- .../util/extensions/StringExtensions.kt | 14 ++++++++++ 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt index 728354e..4733e78 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/ui/screens/main/MainViewModel.kt @@ -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), + ), + ) } } diff --git a/app/src/main/java/com/zaneschepke/wireguardautotunnel/util/extensions/StringExtensions.kt b/app/src/main/java/com/zaneschepke/wireguardautotunnel/util/extensions/StringExtensions.kt index 786e32d..7a617c5 100644 --- a/app/src/main/java/com/zaneschepke/wireguardautotunnel/util/extensions/StringExtensions.kt +++ b/app/src/main/java/com/zaneschepke/wireguardautotunnel/util/extensions/StringExtensions.kt @@ -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? { + val matchResult = hasNumberInParentheses.matchEntire(this) + return matchResult?.let { + Pair(it.groupValues[1], it.groupValues[2].toInt()) + } +} + fun List.isMatchingToWildcardList(value: String): Boolean { val excludeValues = this.filter { it.startsWith("!") }.map { it.removePrefix("!").toRegexWithWildcards() } Timber.d("Excluded values: $excludeValues")