Skip to content

Commit

Permalink
Remove the auto-connect switch when vpn settings are available
Browse files Browse the repository at this point in the history
  • Loading branch information
Pururun committed Feb 9, 2024
1 parent f5a7735 commit 8e613bd
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalLifecycleOwner
import androidx.compose.ui.platform.testTag
import androidx.compose.ui.res.stringResource
Expand Down Expand Up @@ -78,7 +77,6 @@ import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_WIREGUARD_CUSTOM_PORT_TEXT_
import net.mullvad.mullvadvpn.compose.test.LAZY_LIST_WIREGUARD_PORT_ITEM_X_TEST_TAG
import net.mullvad.mullvadvpn.compose.transitions.SlideInFromRightTransition
import net.mullvad.mullvadvpn.constant.WIREGUARD_PRESET_PORTS
import net.mullvad.mullvadvpn.lib.common.util.vpnSettingsAvailable
import net.mullvad.mullvadvpn.lib.theme.AppTheme
import net.mullvad.mullvadvpn.lib.theme.Dimens
import net.mullvad.mullvadvpn.model.Constraint
Expand Down Expand Up @@ -293,12 +291,11 @@ fun VpnSettingsScreen(
navigationIcon = { NavigateBackIconButton(onBackClick) },
snackbarHostState = snackbarHostState
) { modifier, lazyListState ->
val context = LocalContext.current
LazyColumn(
modifier = modifier.testTag(LAZY_LIST_TEST_TAG).animateContentSize(),
state = lazyListState
) {
if (context.vpnSettingsAvailable()) {
if (uiState.systemVpnSettingsAvailable) {
item {
Spacer(modifier = Modifier.height(Dimens.cellLabelVerticalPadding))
NavigationComposeCell(
Expand All @@ -311,18 +308,21 @@ fun VpnSettingsScreen(
text = stringResource(id = R.string.auto_connect_and_lockdown_mode_footer)
)
}
}
item {
Spacer(modifier = Modifier.height(Dimens.cellLabelVerticalPadding))
HeaderSwitchComposeCell(
title = stringResource(R.string.auto_connect),
isToggled = uiState.isAutoConnectEnabled,
isEnabled = true,
onCellClicked = { newValue -> onToggleAutoConnect(newValue) }
)
}
item {
SwitchComposeSubtitleCell(text = stringResource(id = R.string.auto_connect_footer))
} else {
item {
Spacer(modifier = Modifier.height(Dimens.cellLabelVerticalPadding))
HeaderSwitchComposeCell(
title = stringResource(R.string.auto_connect),
isToggled = uiState.isAutoConnectEnabled,
isEnabled = true,
onCellClicked = { newValue -> onToggleAutoConnect(newValue) }
)
}
item {
SwitchComposeSubtitleCell(
text = stringResource(id = R.string.auto_connect_footer)
)
}
}
item {
Spacer(modifier = Modifier.height(Dimens.cellLabelVerticalPadding))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data class VpnSettingsUiState(
val selectedWireguardPort: Constraint<Port>,
val customWireguardPort: Constraint<Port>?,
val availablePortRanges: List<PortRange>,
val systemVpnSettingsAvailable: Boolean,
) {

companion object {
Expand All @@ -35,6 +36,7 @@ data class VpnSettingsUiState(
selectedWireguardPort: Constraint<Port> = Constraint.Any(),
customWireguardPort: Constraint.Only<Port>? = null,
availablePortRanges: List<PortRange> = emptyList(),
systemVpnSettingsAvailable: Boolean = false,
) =
VpnSettingsUiState(
mtu,
Expand All @@ -48,6 +50,7 @@ data class VpnSettingsUiState(
selectedWireguardPort,
customWireguardPort,
availablePortRanges,
systemVpnSettingsAvailable
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import net.mullvad.mullvadvpn.usecase.PlayPaymentUseCase
import net.mullvad.mullvadvpn.usecase.PortRangeUseCase
import net.mullvad.mullvadvpn.usecase.RelayListFilterUseCase
import net.mullvad.mullvadvpn.usecase.RelayListUseCase
import net.mullvad.mullvadvpn.usecase.SystemVpnSettingsUseCase
import net.mullvad.mullvadvpn.usecase.TunnelStateNotificationUseCase
import net.mullvad.mullvadvpn.usecase.VersionNotificationUseCase
import net.mullvad.mullvadvpn.util.ChangelogDataProvider
Expand Down Expand Up @@ -109,6 +110,7 @@ val uiModule = module {
single { RelayListUseCase(get(), get()) }
single { OutOfTimeUseCase(get(), get()) }
single { ConnectivityUseCase(get()) }
single { SystemVpnSettingsUseCase(androidContext()) }

single { InAppNotificationController(get(), get(), get(), get(), MainScope()) }

Expand Down Expand Up @@ -151,7 +153,7 @@ val uiModule = module {
viewModel { SettingsViewModel(get(), get(), IS_PLAY_BUILD) }
viewModel { SplashViewModel(get(), get(), get()) }
viewModel { VoucherDialogViewModel(get(), get()) }
viewModel { VpnSettingsViewModel(get(), get(), get(), get()) }
viewModel { VpnSettingsViewModel(get(), get(), get(), get(), get()) }
viewModel { WelcomeViewModel(get(), get(), get(), get(), get(), isPlayBuild = IS_PLAY_BUILD) }
viewModel { ReportProblemViewModel(get(), get()) }
viewModel { ViewLogsViewModel(get()) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.mullvad.mullvadvpn.usecase

import android.content.Context
import android.content.Intent

class SystemVpnSettingsUseCase(val context: Context) {
fun systemVpnSettingsAvailable(): Boolean =
Intent("android.net.vpn.SETTINGS").resolveActivity(context.packageManager) != null
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import net.mullvad.mullvadvpn.model.WireguardConstraints
import net.mullvad.mullvadvpn.repository.SettingsRepository
import net.mullvad.mullvadvpn.usecase.PortRangeUseCase
import net.mullvad.mullvadvpn.usecase.RelayListUseCase
import net.mullvad.mullvadvpn.usecase.SystemVpnSettingsUseCase
import net.mullvad.mullvadvpn.util.isCustom

sealed interface VpnSettingsSideEffect {
Expand All @@ -49,6 +50,7 @@ class VpnSettingsViewModel(
private val resources: Resources,
portRangeUseCase: PortRangeUseCase,
private val relayListUseCase: RelayListUseCase,
private val systemVpnSettingsUseCase: SystemVpnSettingsUseCase,
private val dispatcher: CoroutineDispatcher = Dispatchers.IO
) : ViewModel() {

Expand All @@ -75,7 +77,9 @@ class VpnSettingsViewModel(
quantumResistant = settings?.quantumResistant() ?: QuantumResistantState.Off,
selectedWireguardPort = settings?.getWireguardPort() ?: Constraint.Any(),
customWireguardPort = customWgPort,
availablePortRanges = portRanges
availablePortRanges = portRanges,
systemVpnSettingsAvailable =
systemVpnSettingsUseCase.systemVpnSettingsAvailable()
)
}
.stateIn(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data class VpnSettingsViewModelState(
val selectedWireguardPort: Constraint<Port>,
val customWireguardPort: Constraint<Port>?,
val availablePortRanges: List<PortRange>,
val systemVpnSettingsAvailable: Boolean,
) {
fun toUiState(): VpnSettingsUiState =
VpnSettingsUiState(
Expand All @@ -34,6 +35,7 @@ data class VpnSettingsViewModelState(
selectedWireguardPort,
customWireguardPort,
availablePortRanges,
systemVpnSettingsAvailable
)

companion object {
Expand All @@ -51,7 +53,8 @@ data class VpnSettingsViewModelState(
quantumResistant = QuantumResistantState.Off,
selectedWireguardPort = Constraint.Any(),
customWireguardPort = null,
availablePortRanges = emptyList()
availablePortRanges = emptyList(),
systemVpnSettingsAvailable = false
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,3 @@ fun Context.openVpnSettings() {
val intent = Intent("android.settings.VPN_SETTINGS")
startActivity(intent)
}

fun Context.vpnSettingsAvailable(): Boolean =
Intent("android.net.vpn.SETTINGS").resolveActivity(packageManager) != null

0 comments on commit 8e613bd

Please sign in to comment.