Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove by custom DNS by index #6308

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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)
)
}
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
@@ -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) }
Original file line number Diff line number Diff line change
@@ -343,11 +343,15 @@ class ManagementService(
.mapLeft(SetDnsOptionsError::Unknown)
.mapEmpty()

suspend fun deleteCustomDns(address: InetAddress): Either<SetDnsOptionsError, Unit> =
suspend fun deleteCustomDns(index: Int): Either<SetDnsOptionsError, Unit> =
Either.catch {
val currentDnsOptions = getSettings().tunnelOptions.dnsOptions
val updatedDnsOptions =
DnsOptions.customOptions.addresses.modify(currentDnsOptions) { it - address }
DnsOptions.customOptions.addresses.modify(currentDnsOptions) {
val mutableAddresses = it.toMutableList()
mutableAddresses.removeAt(index)
mutableAddresses.toList()
}
grpc.setDnsOptions(updatedDnsOptions.fromDomain())
}
.mapLeft(SetDnsOptionsError::Unknown)