Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove by custom DNS by index
Browse files Browse the repository at this point in the history
Rawa committed Jun 3, 2024
1 parent 80bbc27 commit b1c5763
Showing 5 changed files with 18 additions and 15 deletions.
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,13 @@ 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) {
it.filterIndexed { i, _ -> i != index }
}
grpc.setDnsOptions(updatedDnsOptions.fromDomain())
}
.mapLeft(SetDnsOptionsError::Unknown)

0 comments on commit b1c5763

Please sign in to comment.