From b1c57637b99836b5c4cc961469556b99b1881a86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Mon, 3 Jun 2024 09:36:56 +0200 Subject: [PATCH] Remove by custom DNS by index --- .../mullvadvpn/compose/dialog/DnsDialogTest.kt | 4 ++-- .../mullvad/mullvadvpn/compose/dialog/DnsDialog.kt | 12 ++++++------ .../mullvadvpn/repository/SettingsRepository.kt | 2 +- .../mullvadvpn/viewmodel/DnsDialogViewModel.kt | 9 +++++---- .../mullvadvpn/lib/daemon/grpc/ManagementService.kt | 6 ++++-- 5 files changed, 18 insertions(+), 15 deletions(-) diff --git a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/dialog/DnsDialogTest.kt b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/dialog/DnsDialogTest.kt index c35a4f25341e..5a2d1e7e3b12 100644 --- a/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/dialog/DnsDialogTest.kt +++ b/android/app/src/androidTest/kotlin/net/mullvad/mullvadvpn/compose/dialog/DnsDialogTest.kt @@ -23,7 +23,7 @@ class DnsDialogTest { validationResult = DnsDialogViewState.ValidationResult.Success, isLocal = false, isAllowLanEnabled = false, - isNewEntry = true + index = null ) @SuppressLint("ComposableNaming") @@ -32,7 +32,7 @@ class DnsDialogTest { state: DnsDialogViewState = defaultState, onDnsInputChange: (String) -> Unit = { _ -> }, onSaveDnsClick: () -> Unit = {}, - onRemoveDnsClick: () -> Unit = {}, + onRemoveDnsClick: (Int) -> Unit = {}, onDismiss: () -> Unit = {} ) { DnsDialog(state, onDnsInputChange, onSaveDnsClick, onRemoveDnsClick, onDismiss) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/DnsDialog.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/DnsDialog.kt index 5b76023a7e4d..163d19f4b540 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/DnsDialog.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/DnsDialog.kt @@ -41,7 +41,7 @@ private fun PreviewDnsDialogNew() { DnsDialogViewState.ValidationResult.Success, false, false, - true + null ), {}, {}, @@ -61,7 +61,7 @@ private fun PreviewDnsDialogEdit() { DnsDialogViewState.ValidationResult.Success, false, false, - false + 0 ), {}, {}, @@ -81,7 +81,7 @@ private fun PreviewDnsDialogEditAllowLanDisabled() { DnsDialogViewState.ValidationResult.Success, true, false, - true + 0 ), {}, {}, @@ -125,7 +125,7 @@ fun DnsDialog( state: DnsDialogViewState, onDnsInputChange: (String) -> Unit, onSaveDnsClick: () -> Unit, - onRemoveDnsClick: () -> Unit, + onRemoveDnsClick: (Int) -> Unit, onDismiss: () -> Unit ) { AlertDialog( @@ -185,10 +185,10 @@ fun DnsDialog( text = stringResource(id = R.string.submit_button), ) - if (!state.isNewEntry) { + if (state.index != null) { NegativeButton( modifier = Modifier.fillMaxWidth(), - onClick = onRemoveDnsClick, + onClick = { onRemoveDnsClick(state.index) }, text = stringResource(id = R.string.remove_button) ) } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/SettingsRepository.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/SettingsRepository.kt index e2469f626f67..b7214421973c 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/SettingsRepository.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/repository/SettingsRepository.kt @@ -45,7 +45,7 @@ class SettingsRepository( state: DnsState, ) = managementService.setDnsState(state) - suspend fun deleteCustomDns(address: InetAddress) = managementService.deleteCustomDns(address) + suspend fun deleteCustomDns(index: Int) = managementService.deleteCustomDns(index) suspend fun setCustomDns(index: Int, address: InetAddress) = managementService.setCustomDns(index, address) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/DnsDialogViewModel.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/DnsDialogViewModel.kt index cc377b0bab39..4b3a6d5d6b58 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/DnsDialogViewModel.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/DnsDialogViewModel.kt @@ -39,8 +39,9 @@ data class DnsDialogViewState( val validationResult: ValidationResult = ValidationResult.Success, val isLocal: Boolean, val isAllowLanEnabled: Boolean, - val isNewEntry: Boolean + val index: Int?, ) { + val isNewEntry = index == null fun isValid() = (validationResult is ValidationResult.Success) @@ -90,7 +91,7 @@ class DnsDialogViewModel( ipAddress.validateDnsEntry(index, vmState.customDnsList), ipAddress.isLocalAddress(), isAllowLanEnabled = vmState.isAllowLanEnabled, - index == null + index ) private fun String.validateDnsEntry( @@ -128,10 +129,10 @@ class DnsDialogViewModel( ) } - fun onRemoveDnsClick() = + fun onRemoveDnsClick(index: Int) = viewModelScope.launch(dispatcher) { repository - .deleteCustomDns(InetAddress.getByName(uiState.value.ipAddress)) + .deleteCustomDns(index) .fold( { _uiSideEffect.send(DnsDialogSideEffect.Error) }, { _uiSideEffect.send(DnsDialogSideEffect.Complete) } diff --git a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt index 80b79b707a5c..67838632534d 100644 --- a/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt +++ b/android/lib/daemon-grpc/src/main/kotlin/net/mullvad/mullvadvpn/lib/daemon/grpc/ManagementService.kt @@ -343,11 +343,13 @@ class ManagementService( .mapLeft(SetDnsOptionsError::Unknown) .mapEmpty() - suspend fun deleteCustomDns(address: InetAddress): Either = + suspend fun deleteCustomDns(index: Int): Either = Either.catch { val currentDnsOptions = getSettings().tunnelOptions.dnsOptions val updatedDnsOptions = - DnsOptions.customOptions.addresses.modify(currentDnsOptions) { it - address } + DnsOptions.customOptions.addresses.modify(currentDnsOptions) { + it.filterIndexed { i, _ -> i != index } + } grpc.setDnsOptions(updatedDnsOptions.fromDomain()) } .mapLeft(SetDnsOptionsError::Unknown)