Skip to content

Commit

Permalink
Improve DnsDialogViewModel nav arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
kl committed Oct 24, 2024
1 parent d9e7837 commit 98375f9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ class VpnSettingsScreenTest {
fun testClickAddDns() =
composeExtension.use {
// Arrange
val mockedClickHandler: (Int?, String?) -> Unit = mockk(relaxed = true)
val mockedClickHandler: (Int?) -> Unit = mockk(relaxed = true)
setContentWithTheme {
VpnSettingsScreen(
state = VpnSettingsUiState.createDefault(isCustomDnsEnabled = true),
Expand All @@ -390,7 +390,7 @@ class VpnSettingsScreenTest {
onNodeWithText("Add a server").performClick()

// Assert
verify { mockedClickHandler.invoke(null, null) }
verify { mockedClickHandler.invoke(null) }
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private fun PreviewDnsDialogEditAllowLanDisabled() {
AppTheme { DnsDialog(DnsDialogViewState("192.168.1.1", null, true, false, 0), {}, {}, {}, {}) }
}

data class DnsDialogNavArgs(val index: Int? = null, val initialValue: String? = null)
data class DnsDialogNavArgs(val index: Int? = null)

@Destination<RootGraph>(style = DestinationStyle.Dialog::class, navArgs = DnsDialogNavArgs::class)
@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private fun PreviewVpnSettings(
onToggleBlockGambling = {},
onToggleBlockSocialMedia = {},
navigateToMtuDialog = {},
navigateToDns = { _, _ -> },
navigateToDns = { _ -> },
onToggleDnsClick = {},
onBackClick = {},
onSelectObfuscationMode = {},
Expand Down Expand Up @@ -184,7 +184,7 @@ fun VpnSettings(
is VpnSettingsSideEffect.ShowToast ->
launch { snackbarHostState.showSnackbarImmediately(message = it.message(context)) }
VpnSettingsSideEffect.NavigateToDnsDialog ->
navigator.navigate(DnsDestination(null, null)) { launchSingleTop = true }
navigator.navigate(DnsDestination(null)) { launchSingleTop = true }
}
}

Expand Down Expand Up @@ -239,9 +239,7 @@ fun VpnSettings(
navigateToMtuDialog =
dropUnlessResumed { mtu: Mtu? -> navigator.navigate(MtuDestination(mtu)) },
navigateToDns =
dropUnlessResumed { index: Int?, address: String? ->
navigator.navigate(DnsDestination(index, address))
},
dropUnlessResumed { index: Int? -> navigator.navigate(DnsDestination(index)) },
navigateToWireguardPortDialog =
dropUnlessResumed {
navigator.navigate(
Expand Down Expand Up @@ -293,7 +291,7 @@ fun VpnSettingsScreen(
onToggleBlockGambling: (Boolean) -> Unit = {},
onToggleBlockSocialMedia: (Boolean) -> Unit = {},
navigateToMtuDialog: (mtu: Mtu?) -> Unit = {},
navigateToDns: (index: Int?, address: String?) -> Unit = { _, _ -> },
navigateToDns: (index: Int?) -> Unit = { _ -> },
onToggleDnsClick: (Boolean) -> Unit = {},
onBackClick: () -> Unit = {},
onSelectObfuscationMode: (obfuscationMode: ObfuscationMode) -> Unit = {},
Expand Down Expand Up @@ -468,14 +466,14 @@ fun VpnSettingsScreen(
address = item.address,
isUnreachableLocalDnsWarningVisible =
item.isLocal && !state.isLocalNetworkSharingEnabled,
onClick = { navigateToDns(index, item.address) },
onClick = { navigateToDns(index) },
modifier = Modifier.animateItem(),
)
}

itemWithDivider {
BaseCell(
onCellClicked = { navigateToDns(null, null) },
onCellClicked = { navigateToDns(null) },
headlineContent = {
Text(
text = stringResource(id = R.string.add_a_server),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import net.mullvad.mullvadvpn.lib.model.Settings
import net.mullvad.mullvadvpn.repository.SettingsRepository
import net.mullvad.talpid.util.addressString
import org.apache.commons.validator.routines.InetAddressValidator

sealed interface DnsDialogSideEffect {
Expand Down Expand Up @@ -57,20 +58,27 @@ class DnsDialogViewModel(
private val navArgs = DnsDestination.argsFrom(savedStateHandle)

private val settings = MutableStateFlow<Settings?>(null)
private val currentIndex = MutableStateFlow(navArgs.index)
private val _ipAddressInput = MutableStateFlow(navArgs.initialValue ?: EMPTY_STRING)
private val _ipAddressInput = MutableStateFlow<String?>(null)

val uiState: StateFlow<DnsDialogViewState> =
combine(_ipAddressInput, currentIndex, settings.filterNotNull()) {
input,
currentIndex,
settings ->
createViewState(settings.addresses(), currentIndex, settings.allowLan, input)
combine(_ipAddressInput, settings.filterNotNull()) { inputAddress, settings ->
val dnsList = settings.addresses()
val input =
inputAddress
?: navArgs.index?.let { index -> dnsList[index].addressString() }
?: EMPTY_STRING

createViewState(
customDnsList = dnsList,
currentIndex = navArgs.index,
isAllowLanEnabled = settings.allowLan,
input = input,
)
}
.stateIn(
viewModelScope,
SharingStarted.Lazily,
createViewState(emptyList(), null, false, _ipAddressInput.value),
createViewState(emptyList(), null, false, EMPTY_STRING),
)

private val _uiSideEffect = Channel<DnsDialogSideEffect>()
Expand Down

0 comments on commit 98375f9

Please sign in to comment.