Skip to content

Commit a115cb8

Browse files
committed
Fix errors
1 parent 05fc895 commit a115cb8

File tree

6 files changed

+61
-53
lines changed

6 files changed

+61
-53
lines changed

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/bottomsheet/CustomListEntryBottomSheet.kt

+4-5
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import com.ramcosta.composedestinations.result.ResultBackNavigator
1414
import net.mullvad.mullvadvpn.R
1515
import net.mullvad.mullvadvpn.compose.cell.HeaderCell
1616
import net.mullvad.mullvadvpn.compose.cell.IconCell
17-
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
17+
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResult
1818
import net.mullvad.mullvadvpn.compose.component.MullvadModalBottomContainer
1919
import net.mullvad.mullvadvpn.compose.util.CollectSideEffectWithLifecycle
2020
import net.mullvad.mullvadvpn.lib.model.CustomListId
@@ -35,14 +35,13 @@ data class CustomListEntrySheetNavArgs(
3535
style = DestinationStyleBottomSheet::class
3636
)
3737
@Composable
38-
fun CustomListEntrySheet(backNavigator: ResultBackNavigator<LocationsChanged>) {
38+
fun CustomListEntrySheet(backNavigator: ResultBackNavigator<CustomListActionResult>) {
3939
val vm = koinViewModel<CustomListEntrySheetViewModel>()
4040
val state = vm.uiState.collectAsStateWithLifecycle()
4141
CollectSideEffectWithLifecycle(vm.uiSideEffect) {
4242
when (it) {
43-
CustomListEntrySheetSideEffect.GenericError -> TODO("How do we handle error?")
44-
is CustomListEntrySheetSideEffect.LocationRemovedFromCustomList ->
45-
backNavigator.navigateBack(it.locationsChanged)
43+
is CustomListEntrySheetSideEffect.LocationRemovedResult ->
44+
backNavigator.navigateBack(it.result)
4645
}
4746
}
4847
MullvadModalBottomContainer {

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/bottomsheet/LocationBottomSheet.kt

+3-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import kotlin.collections.forEach
2121
import net.mullvad.mullvadvpn.R
2222
import net.mullvad.mullvadvpn.compose.cell.HeaderCell
2323
import net.mullvad.mullvadvpn.compose.cell.IconCell
24-
import net.mullvad.mullvadvpn.compose.communication.CustomListSuccess
24+
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResult
2525
import net.mullvad.mullvadvpn.compose.component.MullvadCircularProgressIndicatorMedium
2626
import net.mullvad.mullvadvpn.compose.component.MullvadModalBottomContainer
2727
import net.mullvad.mullvadvpn.compose.state.LocationUiState
@@ -42,18 +42,14 @@ data class LocationNavArgs(val locationName: String, val id: GeoLocationId)
4242
@Composable
4343
fun LocationSheet(
4444
navigator: DestinationsNavigator,
45-
backNavigator: ResultBackNavigator<CustomListSuccess>,
45+
backNavigator: ResultBackNavigator<CustomListActionResult>,
4646
) {
4747
val viewModel = koinViewModel<LocationSheetViewModel>()
4848
val state = viewModel.uiState.collectAsStateWithLifecycle()
4949

5050
CollectSideEffectWithLifecycle(viewModel.uiSideEffect) {
5151
when (it) {
52-
LocationSideEffect.GenericError -> {
53-
TODO("Handle")
54-
}
55-
is LocationSideEffect.LocationAddedToCustomList ->
56-
backNavigator.navigateBack(it.locationsChanged)
52+
is LocationSideEffect.AddLocationResult -> backNavigator.navigateBack(it.result)
5753
}
5854
}
5955

android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/communication/CustomListSuccess.kt

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ import kotlinx.parcelize.Parcelize
55
import net.mullvad.mullvadvpn.lib.model.CustomListId
66
import net.mullvad.mullvadvpn.lib.model.CustomListName
77

8-
sealed interface CustomListSuccess : Parcelable {
8+
@Parcelize sealed interface CustomListActionResult : Parcelable
9+
10+
@Parcelize data object GenericError : CustomListActionResult, Parcelable
11+
12+
sealed interface CustomListSuccess : CustomListActionResult, Parcelable {
913
val undo: CustomListAction
1014
}
1115

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

+23-9
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ import net.mullvad.mullvadvpn.compose.cell.SwitchComposeSubtitleCell
6464
import net.mullvad.mullvadvpn.compose.cell.ThreeDotCell
6565
import net.mullvad.mullvadvpn.compose.communication.Created
6666
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
67+
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResult
6768
import net.mullvad.mullvadvpn.compose.communication.CustomListSuccess
6869
import net.mullvad.mullvadvpn.compose.communication.Deleted
70+
import net.mullvad.mullvadvpn.compose.communication.GenericError
6971
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
7072
import net.mullvad.mullvadvpn.compose.communication.Renamed
7173
import net.mullvad.mullvadvpn.compose.component.LocationsEmptyText
@@ -134,9 +136,9 @@ fun SelectLocation(
134136
deleteCustomListDialogResultRecipient: ResultRecipient<DeleteCustomListDestination, Deleted>,
135137
updateCustomListResultRecipient:
136138
ResultRecipient<CustomListLocationsDestination, LocationsChanged>,
137-
locationSheetResultRecipient: ResultRecipient<LocationSheetDestination, CustomListSuccess>,
139+
locationSheetResultRecipient: ResultRecipient<LocationSheetDestination, CustomListActionResult>,
138140
customListEntryResultRecipient:
139-
ResultRecipient<CustomListEntrySheetDestination, LocationsChanged>
141+
ResultRecipient<CustomListEntrySheetDestination, CustomListActionResult>
140142
) {
141143
val vm = koinViewModel<SelectLocationViewModel>()
142144
val state = vm.uiState.collectAsStateWithLifecycle().value
@@ -519,7 +521,7 @@ private fun CustomListSuccess.message(context: Context): String =
519521
}
520522

521523
@Composable
522-
private fun <D : DestinationSpec, R : CustomListSuccess> ResultRecipient<D, R>
524+
private fun <D : DestinationSpec, R : CustomListActionResult> ResultRecipient<D, R>
523525
.OnCustomListNavResult(
524526
snackbarHostState: SnackbarHostState,
525527
performAction: (action: CustomListAction) -> Unit
@@ -533,12 +535,24 @@ private fun <D : DestinationSpec, R : CustomListSuccess> ResultRecipient<D, R>
533535
}
534536
is NavResult.Value -> {
535537
// Handle result
536-
scope.launch {
537-
snackbarHostState.showResultSnackbar(
538-
context = context,
539-
result = result.value,
540-
onUndo = performAction
541-
)
538+
val customListActionResult = result.value
539+
when (customListActionResult) {
540+
is GenericError -> {
541+
scope.launch {
542+
snackbarHostState.showSnackbarImmediately(
543+
message = context.getString(R.string.error_occurred),
544+
duration = SnackbarDuration.Short
545+
)
546+
}
547+
}
548+
is CustomListSuccess ->
549+
scope.launch {
550+
snackbarHostState.showResultSnackbar(
551+
context = context,
552+
result = customListActionResult,
553+
onUndo = performAction
554+
)
555+
}
542556
}
543557
}
544558
}

android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/CustomListEntrySheetViewModel.kt

+14-21
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import kotlinx.coroutines.flow.StateFlow
1111
import kotlinx.coroutines.flow.receiveAsFlow
1212
import kotlinx.coroutines.launch
1313
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
14-
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
14+
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResult
15+
import net.mullvad.mullvadvpn.compose.communication.GenericError
1516
import net.mullvad.mullvadvpn.compose.state.CustomListEntrySheetUiState
1617
import net.mullvad.mullvadvpn.repository.CustomListsRepository
1718
import net.mullvad.mullvadvpn.usecase.customlists.CustomListActionUseCase
18-
import net.mullvad.mullvadvpn.viewmodel.CustomListEntrySheetSideEffect.GenericError
1919

2020
class CustomListEntrySheetViewModel(
2121
val customListsRepository: CustomListsRepository,
@@ -32,29 +32,22 @@ class CustomListEntrySheetViewModel(
3232

3333
fun removeLocationFromList() =
3434
viewModelScope.launch {
35-
either {
36-
val customList =
37-
customListsRepository.getCustomListById(navArgs.customListId).bind()
38-
val newLocations = (customList.locations - navArgs.location)
39-
customListActionUseCase(
40-
CustomListAction.UpdateLocations(customList.id, newLocations)
41-
)
42-
.bind()
43-
}
44-
.fold(
45-
{ _uiSideEffect.send(GenericError) },
46-
{
47-
_uiSideEffect.send(
48-
CustomListEntrySheetSideEffect.LocationRemovedFromCustomList(it)
49-
)
35+
val result =
36+
either {
37+
val customList =
38+
customListsRepository.getCustomListById(navArgs.customListId).bind()
39+
val newLocations = (customList.locations - navArgs.location)
40+
customListActionUseCase(
41+
CustomListAction.UpdateLocations(customList.id, newLocations)
42+
)
43+
.bind()
5044
}
51-
)
45+
.fold({ GenericError }, { it })
46+
_uiSideEffect.send(CustomListEntrySheetSideEffect.LocationRemovedResult(result))
5247
}
5348
}
5449

5550
sealed interface CustomListEntrySheetSideEffect {
56-
data object GenericError : CustomListEntrySheetSideEffect
57-
58-
data class LocationRemovedFromCustomList(val locationsChanged: LocationsChanged) :
51+
data class LocationRemovedResult(val result: CustomListActionResult) :
5952
CustomListEntrySheetSideEffect
6053
}

android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/LocationSheetViewModel.kt

+12-10
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import kotlinx.coroutines.flow.receiveAsFlow
1212
import kotlinx.coroutines.flow.stateIn
1313
import kotlinx.coroutines.launch
1414
import net.mullvad.mullvadvpn.compose.communication.CustomListAction
15-
import net.mullvad.mullvadvpn.compose.communication.LocationsChanged
15+
import net.mullvad.mullvadvpn.compose.communication.CustomListActionResult
16+
import net.mullvad.mullvadvpn.compose.communication.GenericError
1617
import net.mullvad.mullvadvpn.compose.state.CustomListEntry
1718
import net.mullvad.mullvadvpn.compose.state.LocationUiState
1819
import net.mullvad.mullvadvpn.lib.model.RelayItem
@@ -57,18 +58,19 @@ class LocationSheetViewModel(
5758
viewModelScope.launch {
5859
val newLocations =
5960
(customList.locations + item).filter { it !in item.descendants() }.map { it.id }
60-
customListActionUseCase(CustomListAction.UpdateLocations(customList.id, newLocations))
61-
.fold(
62-
{ _uiSideEffect.send(LocationSideEffect.GenericError) },
63-
{ _uiSideEffect.send(LocationSideEffect.LocationAddedToCustomList(it)) },
64-
)
61+
val result =
62+
customListActionUseCase(
63+
CustomListAction.UpdateLocations(customList.id, newLocations)
64+
)
65+
.fold(
66+
{ GenericError },
67+
{ it },
68+
)
69+
_uiSideEffect.send(LocationSideEffect.AddLocationResult(result))
6570
}
6671
}
6772
}
6873

6974
sealed interface LocationSideEffect {
70-
data object GenericError : LocationSideEffect
71-
72-
data class LocationAddedToCustomList(val locationsChanged: LocationsChanged) :
73-
LocationSideEffect
75+
data class AddLocationResult(val result: CustomListActionResult) : LocationSideEffect
7476
}

0 commit comments

Comments
 (0)