Skip to content

Commit

Permalink
Remove by custom DNS by index
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Jun 3, 2024
1 parent 80bbc27 commit 2001e35
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class DnsDialogTest {
validationResult = DnsDialogViewState.ValidationResult.Success,
isLocal = false,
isAllowLanEnabled = false,
isNewEntry = true
index = null
)

@SuppressLint("ComposableNaming")
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ private fun PreviewDnsDialogNew() {
DnsDialogViewState.ValidationResult.Success,
false,
false,
true
null
),
{},
{},
Expand All @@ -61,7 +61,7 @@ private fun PreviewDnsDialogEdit() {
DnsDialogViewState.ValidationResult.Success,
false,
false,
false
0
),
{},
{},
Expand All @@ -81,7 +81,7 @@ private fun PreviewDnsDialogEditAllowLanDisabled() {
DnsDialogViewState.ValidationResult.Success,
true,
false,
true
0
),
{},
{},
Expand Down Expand Up @@ -125,7 +125,7 @@ fun DnsDialog(
state: DnsDialogViewState,
onDnsInputChange: (String) -> Unit,
onSaveDnsClick: () -> Unit,
onRemoveDnsClick: () -> Unit,
onRemoveDnsClick: (Int) -> Unit,
onDismiss: () -> Unit
) {
AlertDialog(
Expand Down Expand Up @@ -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)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -90,7 +91,7 @@ class DnsDialogViewModel(
ipAddress.validateDnsEntry(index, vmState.customDnsList),
ipAddress.isLocalAddress(),
isAllowLanEnabled = vmState.isAllowLanEnabled,
index == null
index
)

private fun String.validateDnsEntry(
Expand Down Expand Up @@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 2001e35

Please sign in to comment.