Skip to content

Commit 533ecb0

Browse files
committed
Fixed some things + unit tests
1 parent 5778649 commit 533ecb0

17 files changed

+126
-144
lines changed

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/cell/RelayLocationCell.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import net.mullvad.mullvadvpn.relaylist.RelayItem
4545

4646
@Composable
4747
@Preview
48-
private fun PreviewNormalRelayLocationCell() {
48+
private fun PreviewStatusRelayLocationCell() {
4949
AppTheme {
5050
Column(Modifier.background(color = MaterialTheme.colorScheme.background)) {
5151
val countryActive =
@@ -138,11 +138,11 @@ private fun PreviewNormalRelayLocationCell() {
138138
)
139139
)
140140
// Active relay list not expanded
141-
NormalRelayLocationCell(countryActive)
141+
StatusRelayLocationCell(countryActive)
142142
// Not Active Relay
143-
NormalRelayLocationCell(countryNotActive)
143+
StatusRelayLocationCell(countryNotActive)
144144
// Relay expanded country and city
145-
NormalRelayLocationCell(countryActive.copy(expanded = true))
145+
StatusRelayLocationCell(countryActive.copy(expanded = true))
146146
}
147147
}
148148
}
@@ -252,7 +252,7 @@ private fun PreviewCheckableRelayLocationCell() {
252252
}
253253

254254
@Composable
255-
fun NormalRelayLocationCell(
255+
fun StatusRelayLocationCell(
256256
relay: RelayItem,
257257
modifier: Modifier = Modifier,
258258
activeColor: Color = MaterialTheme.colorScheme.selected,
@@ -273,7 +273,7 @@ fun NormalRelayLocationCell(
273273
.background(
274274
color =
275275
when {
276-
selected -> Color.Transparent
276+
selected -> Color.Unspecified
277277
relayItem is RelayItem.CustomList && !relayItem.hasChildren ->
278278
disabledColor
279279
relayItem.active -> activeColor
@@ -330,7 +330,7 @@ fun CheckableRelayLocationCell(
330330
leadingContentStartPadding = Dimens.cellStartPaddingInteractive,
331331
modifier = modifier,
332332
onClick = { onRelayCheckedChange(it, !selectedRelays.contains(it)) },
333-
onLongClick = {},
333+
onLongClick = null,
334334
depth = 0
335335
)
336336
}
@@ -345,7 +345,7 @@ private fun RelayLocationCell(
345345
leadingContentStarPaddingModifier: Dp = Dimens.mediumPadding,
346346
specialBackgroundColor: @Composable (relayItem: RelayItem) -> Color? = { null },
347347
onClick: (item: RelayItem) -> Unit,
348-
onLongClick: (item: RelayItem) -> Unit,
348+
onLongClick: ((item: RelayItem) -> Unit)?,
349349
depth: Int
350350
) {
351351
val startPadding = leadingContentStartPadding + leadingContentStarPaddingModifier * depth
@@ -382,7 +382,7 @@ private fun RelayLocationCell(
382382
.combinedClickable(
383383
enabled = relay.active,
384384
onClick = { onClick(relay) },
385-
onLongClick = { onLongClick(relay) },
385+
onLongClick = { onLongClick?.invoke(relay) },
386386
)
387387
) {
388388
Box(

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/CreateCustomListDialog.kt

+10-11
Original file line numberDiff line numberDiff line change
@@ -124,17 +124,7 @@ fun CreateCustomListDialog(
124124
supportingText = {
125125
if (uiState.error != null) {
126126
Text(
127-
text =
128-
stringResource(
129-
id =
130-
if (
131-
uiState.error == CustomListsError.CustomListExists
132-
) {
133-
R.string.custom_list_error_list_exists
134-
} else {
135-
R.string.error_occurred
136-
}
137-
),
127+
text = uiState.error.errorString(),
138128
color = MaterialTheme.colorScheme.error,
139129
style = MaterialTheme.typography.bodySmall
140130
)
@@ -166,3 +156,12 @@ fun CreateCustomListDialog(
166156
}
167157
)
168158
}
159+
160+
@Composable
161+
private fun CustomListsError.errorString() =
162+
stringResource(
163+
when (this) {
164+
CustomListsError.CustomListExists -> R.string.custom_list_error_list_exists
165+
CustomListsError.OtherError -> R.string.error_occurred
166+
}
167+
)

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/DeleteCustomListConfirmationDialog.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fun DeleteCustomList(
5757
DeleteCustomListConfirmationDialog(
5858
name = name,
5959
onDelete = viewModel::deleteCustomList,
60-
onBack = { navigator.navigateBack() }
60+
onBack = navigator::navigateBack
6161
)
6262
}
6363

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/DiscardChangesDialog.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ import net.mullvad.mullvadvpn.compose.button.PrimaryButton
1818
@Composable
1919
fun DiscardChangesDialog(resultBackNavigator: ResultBackNavigator<Boolean>) {
2020
AlertDialog(
21-
onDismissRequest = { resultBackNavigator.navigateBack() },
21+
onDismissRequest = resultBackNavigator::navigateBack,
2222
title = { Text(text = stringResource(id = R.string.discard_changes)) },
2323
dismissButton = {
2424
PrimaryButton(
2525
modifier = Modifier.focusRequester(FocusRequester()),
26-
onClick = { resultBackNavigator.navigateBack() },
26+
onClick = resultBackNavigator::navigateBack,
2727
text = stringResource(id = R.string.cancel)
2828
)
2929
},

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/dialog/EditCustomListNameDialog.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import androidx.compose.material3.Text
77
import androidx.compose.runtime.Composable
88
import androidx.compose.runtime.LaunchedEffect
99
import androidx.compose.runtime.collectAsState
10+
import androidx.compose.runtime.getValue
1011
import androidx.compose.runtime.mutableStateOf
1112
import androidx.compose.runtime.remember
1213
import androidx.compose.ui.Modifier
@@ -57,7 +58,7 @@ fun EditCustomListName(
5758
}
5859
}
5960

60-
val uiState = vm.uiState.collectAsState().value
61+
val uiState by vm.uiState.collectAsState()
6162
EditCustomListNameDialog(
6263
uiState = uiState,
6364
updateName = vm::updateCustomListName,

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/CustomListLocationsScreen.kt

+8-10
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,16 @@ fun CustomListLocations(
6767
parameters = { parametersOf(customListId, newList) }
6868
)
6969

70-
discardChangesResultRecipient.onNavResult(
71-
listener = {
72-
when (it) {
73-
NavResult.Canceled -> {}
74-
is NavResult.Value -> {
75-
if (it.value) {
76-
backNavigator.navigateBack()
77-
}
70+
discardChangesResultRecipient.onNavResult {
71+
when (it) {
72+
NavResult.Canceled -> {}
73+
is NavResult.Value -> {
74+
if (it.value) {
75+
backNavigator.navigateBack()
7876
}
7977
}
8078
}
81-
)
79+
}
8280

8381
LaunchedEffect(Unit) {
8482
customListsViewModel.uiSideEffect.collect { sideEffect ->
@@ -96,7 +94,7 @@ fun CustomListLocations(
9694
onSaveClick = customListsViewModel::save,
9795
onRelaySelectionClick = customListsViewModel::onRelaySelectionClick,
9896
onBackClick = {
99-
if (state.willDiscardChanges) {
97+
if (state.hasUnsavedChanges) {
10098
navigator.navigate(DiscardChangesDialogDestination) { launchSingleTop = true }
10199
} else {
102100
backNavigator.navigateBack()

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/CustomListsScreen.kt

+19-26
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import com.ramcosta.composedestinations.result.ResultRecipient
3434
import kotlinx.coroutines.launch
3535
import net.mullvad.mullvadvpn.R
3636
import net.mullvad.mullvadvpn.compose.cell.NavigationComposeCell
37-
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
3837
import net.mullvad.mullvadvpn.compose.communication.CustomListResult
3938
import net.mullvad.mullvadvpn.compose.component.MullvadCircularProgressIndicatorLarge
4039
import net.mullvad.mullvadvpn.compose.component.NavigateBackIconButton
@@ -72,34 +71,28 @@ fun CustomLists(
7271
val context = LocalContext.current
7372
val snackbarHostState = remember { SnackbarHostState() }
7473

75-
editCustomListResultRecipient.onNavResult(
76-
listener = { result ->
77-
when (result) {
78-
NavResult.Canceled -> {
79-
/* Do nothing */
80-
}
81-
is NavResult.Value -> {
82-
scope.launch {
83-
snackbarHostState.currentSnackbarData?.dismiss()
84-
snackbarHostState.showSnackbar(
85-
message =
86-
context.getString(
87-
R.string.delete_custom_list_message,
88-
result.value.name
89-
),
90-
actionLabel = context.getString(R.string.undo),
91-
duration = SnackbarDuration.Long,
92-
onAction = {
93-
viewModel.undoDeleteCustomList(
94-
result.value.undo as CustomListAction.Create
95-
)
96-
}
97-
)
98-
}
74+
editCustomListResultRecipient.onNavResult { result ->
75+
when (result) {
76+
NavResult.Canceled -> {
77+
/* Do nothing */
78+
}
79+
is NavResult.Value -> {
80+
scope.launch {
81+
snackbarHostState.currentSnackbarData?.dismiss()
82+
snackbarHostState.showSnackbar(
83+
message =
84+
context.getString(
85+
R.string.delete_custom_list_message,
86+
result.value.name
87+
),
88+
actionLabel = context.getString(R.string.undo),
89+
duration = SnackbarDuration.Long,
90+
onAction = { viewModel.undoDeleteCustomList(result.value.undo) }
91+
)
9992
}
10093
}
10194
}
102-
)
95+
}
10396

10497
CustomListsScreen(
10598
uiState = uiState,

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/EditCustomListScreen.kt

+13-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package net.mullvad.mullvadvpn.compose.screen
22

33
import androidx.compose.foundation.background
4+
import androidx.compose.foundation.layout.padding
45
import androidx.compose.material3.DropdownMenu
56
import androidx.compose.material3.DropdownMenuItem
67
import androidx.compose.material3.Icon
@@ -37,6 +38,7 @@ import net.mullvad.mullvadvpn.compose.destinations.EditCustomListNameDestination
3738
import net.mullvad.mullvadvpn.compose.state.EditCustomListState
3839
import net.mullvad.mullvadvpn.compose.transitions.SlideInFromRightTransition
3940
import net.mullvad.mullvadvpn.lib.theme.AppTheme
41+
import net.mullvad.mullvadvpn.lib.theme.Dimens
4042
import net.mullvad.mullvadvpn.model.GeographicLocationConstraint
4143
import net.mullvad.mullvadvpn.relaylist.RelayItem
4244
import net.mullvad.mullvadvpn.viewmodel.EditCustomListViewModel
@@ -113,7 +115,7 @@ fun EditCustomList(
113115
launchSingleTop = true
114116
}
115117
},
116-
onBackClick = { backNavigator.navigateBack() }
118+
onBackClick = backNavigator::navigateBack
117119
)
118120
}
119121

@@ -127,7 +129,8 @@ fun EditCustomListScreen(
127129
) {
128130
val title =
129131
when (uiState) {
130-
is EditCustomListState.Loading -> ""
132+
EditCustomListState.Loading,
133+
EditCustomListState.NotFound -> ""
131134
is EditCustomListState.Content -> uiState.name
132135
}
133136
ScaffoldWithMediumTopBar(
@@ -140,6 +143,14 @@ fun EditCustomListScreen(
140143
EditCustomListState.Loading -> {
141144
MullvadCircularProgressIndicatorLarge()
142145
}
146+
EditCustomListState.NotFound -> {
147+
Text(
148+
text = stringResource(id = R.string.not_found),
149+
modifier = Modifier.padding(Dimens.screenVerticalMargin),
150+
style = MaterialTheme.typography.labelMedium,
151+
color = MaterialTheme.colorScheme.onSecondary
152+
)
153+
}
143154
is EditCustomListState.Content -> {
144155
// Name cell
145156
TwoRowCell(

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/screen/SelectLocationScreen.kt

+7-10
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ import net.mullvad.mullvadvpn.R
5454
import net.mullvad.mullvadvpn.compose.cell.FilterCell
5555
import net.mullvad.mullvadvpn.compose.cell.HeaderCell
5656
import net.mullvad.mullvadvpn.compose.cell.IconCell
57-
import net.mullvad.mullvadvpn.compose.cell.NormalRelayLocationCell
57+
import net.mullvad.mullvadvpn.compose.cell.StatusRelayLocationCell
5858
import net.mullvad.mullvadvpn.compose.cell.SwitchComposeSubtitleCell
5959
import net.mullvad.mullvadvpn.compose.cell.ThreeDotCell
6060
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
@@ -286,9 +286,7 @@ fun SelectLocationScreen(
286286
}
287287
}
288288
}
289-
var bottomSheetState by remember {
290-
mutableStateOf<BottomSheetState>(BottomSheetState.Hidden)
291-
}
289+
var bottomSheetState by remember { mutableStateOf<BottomSheetState?>(null) }
292290
LazyColumn(
293291
modifier =
294292
Modifier.fillMaxSize()
@@ -349,7 +347,7 @@ fun SelectLocationScreen(
349347
onEditCustomListName = onEditCustomListName,
350348
onEditLocationsCustomList = onEditLocationsCustomList,
351349
onDeleteCustomList = onDeleteCustomList,
352-
onHideBottomSheet = { bottomSheetState = BottomSheetState.Hidden }
350+
onHideBottomSheet = { bottomSheetState = null }
353351
)
354352
}
355353
}
@@ -383,7 +381,7 @@ private fun LazyListScope.customLists(
383381
key = { item -> item.code },
384382
contentType = { ContentType.ITEM },
385383
) { customList ->
386-
NormalRelayLocationCell(
384+
StatusRelayLocationCell(
387385
relay = customList,
388386
// Do not show selection for locations in custom lists
389387
selectedItem = selectedItem as? RelayItem.CustomList,
@@ -425,7 +423,7 @@ private fun LazyListScope.relayList(
425423
key = { item -> item.code },
426424
contentType = { ContentType.ITEM },
427425
) { country ->
428-
NormalRelayLocationCell(
426+
StatusRelayLocationCell(
429427
relay = country,
430428
selectedItem = selectedItem,
431429
onSelectRelay = onSelectRelay,
@@ -437,7 +435,7 @@ private fun LazyListScope.relayList(
437435

438436
@Composable
439437
private fun BottomSheets(
440-
bottomSheetState: BottomSheetState,
438+
bottomSheetState: BottomSheetState?,
441439
onCreateCustomList: (RelayItem?) -> Unit,
442440
onEditCustomLists: () -> Unit,
443441
onAddLocationToList: (RelayItem, RelayItem.CustomList) -> Unit,
@@ -473,7 +471,7 @@ private fun BottomSheets(
473471
closeBottomSheet = onHideBottomSheet
474472
)
475473
}
476-
BottomSheetState.Hidden -> {
474+
null -> {
477475
/* Do nothing */
478476
}
479477
}
@@ -710,7 +708,6 @@ private const val EXTRA_ITEMS_LOCATION = 3
710708
private const val EXTRA_ITEM_CUSTOM_LIST = 1
711709

712710
sealed interface BottomSheetState {
713-
data object Hidden : BottomSheetState
714711

715712
data class ShowCustomListsBottomSheet(val editListEnabled: Boolean) : BottomSheetState
716713

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/state/CustomListLocationsUiState.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import net.mullvad.mullvadvpn.relaylist.RelayItem
55
sealed interface CustomListLocationsUiState {
66
val newList: Boolean
77
val saveEnabled: Boolean
8-
val willDiscardChanges: Boolean
8+
val hasUnsavedChanges: Boolean
99

1010
data class Loading(override val newList: Boolean = false) : CustomListLocationsUiState {
1111
override val saveEnabled: Boolean = false
12-
override val willDiscardChanges: Boolean = false
12+
override val hasUnsavedChanges: Boolean = false
1313
}
1414

1515
sealed interface Content : CustomListLocationsUiState {
1616
val searchTerm: String
1717

1818
data class Empty(override val newList: Boolean, override val searchTerm: String) : Content {
1919
override val saveEnabled: Boolean = false
20-
override val willDiscardChanges: Boolean = false
20+
override val hasUnsavedChanges: Boolean = false
2121
}
2222

2323
data class Data(
@@ -26,7 +26,7 @@ sealed interface CustomListLocationsUiState {
2626
val selectedLocations: Set<RelayItem> = emptySet(),
2727
override val searchTerm: String = "",
2828
override val saveEnabled: Boolean = false,
29-
override val willDiscardChanges: Boolean = false
29+
override val hasUnsavedChanges: Boolean = false
3030
) : Content
3131
}
3232
}

0 commit comments

Comments
 (0)